-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
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
:
-
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.
-
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.
Metadata
Metadata
Assignees
Labels
No labels