Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

References are bugged when using createReadOnly #110

Open
jrmyio opened this issue Dec 2, 2024 · 0 comments
Open

References are bugged when using createReadOnly #110

jrmyio opened this issue Dec 2, 2024 · 0 comments

Comments

@jrmyio
Copy link

jrmyio commented Dec 2, 2024

I am finally giving mobx-quick-tree a try but ran into several issues.

The biggest one seems to be that whenever you are using types.reference() things start behaving wierd when using createReadOnly.

While using createReadOnly:

  1. It never calls the getter you pass to types.reference. It only seems to do some magic on the MST type you pass in as the first argument.

  2. It seems to share the identifiers of all maps in the tree returning instances of other types.

This is best explained by the following example:
https://codesandbox.io/p/sandbox/dreamy-oskar-forked-wm655g?workspaceId=f29c6df7-7887-4b7e-aac0-96fbcc5d85ea

import { getSnapshot, types, getIdentifier } from "@gadgetinc/mobx-quick-tree";

const Keyword = /*#__PURE__*/ types.model("Keyword", {
  id: types.identifier,
  name: types.string,
});

const Keywords = /*#__PURE__*/ types.model("Keywords", {
  keywords: types.map(Keyword),
});

let rootState: any;

const CarType = /*#__PURE__*/ types.model("CarType", {
  id: types.identifier,
  name: types.string,
  keyword: types.reference(Keyword, {
    get(identifier, parent) {
      console.log("I am never called when using createReadOnly? ");
      return rootState.keywords.keywords.get(identifier);
    },
    set(value) {
      const id = getIdentifier(value);
      rootState.keywords.keywords.set(id, value);
      return id;
    },
  }),
});

const CarTypes = /*#__PURE__*/ types.model("CarTypes", {
  carTypes: types.map(CarType),
});

const RootState = /*#__PURE__*/ types.model("RootState", {
  carTypes: CarTypes,
  keywords: Keywords,
});

rootState = RootState.createReadOnly({
  carTypes: {
    carTypes: {
      "1": {
        id: "1",
        name: "I am a cartype",
        keyword: "1",
      },
      "2": {
        id: "2",
        name: "I am the second cartype",
        keyword: "2",
      },
    },
  },
  keywords: {
    keywords: {
      "1": {
        id: "1",
        name: "I am a keyword",
      },
    },
  },
});

console.log({
  rootSnapshot: getSnapshot(rootState),
  keywordOnFirstCardType: rootState.carTypes.carTypes.get("1").keyword,
  keywordNameOnFirstCardType: rootState.carTypes.carTypes.get("1").keyword.name,
  keywordOnSecondCardType: rootState.carTypes.carTypes.get("2").keyword,

  /**
   * This should never return a name of a car type!
   */
  keywordNameOnSecondCardType:
    rootState.carTypes.carTypes.get("2").keyword.name,
});

Notice how the getter is never called, and at the bottom reading a keyword actually returns a car type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant