How does MST BaseNode inheritance work? #2126
-
I am trying to understand some of the inheritance in the MobX-State-Tree internals. The All of these are JavaScript However, some of the abstract classes have implementations of certain methods. For instance, the private _subpath!: string
get subpath() {
return this._subpath
} As far as I can tell, the const RootStore = t.model({
subStore: t.model({
title: t.string
})
})
const rootStore = RootStore.create({ subStore: { title: "hello" } })
console.log("RootStore subpath: ", RootStore.subpath) // undefined
console.log("RootStore subpath: ", rootStore.subpath) // undefined This does, however, exist on the console.log(rootStore.$treenode.subpath) // Empty string
console.log("subStore $treenode subpath: ", rootStore.subStore.$treenode.subpath) // "subStore" I've tried to trace the code paths myself, and I can't seem to understand:
This CodeSandbox has the confusing code in question, along with an example of three classes where a getter is available on a grandchild from an abstract grandparent: https://codesandbox.io/p/sandbox/loving-star-vr43qg?file=%2Fsrc%2Findex.ts%3A15%2C1 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think I got it, it's because of the I put a breakpoint on
const options = { ...mobxShallow, name: this.name }
return observable.object(childNodes, EMPTY_OBJECT, options) as any
|
Beta Was this translation helpful? Give feedback.
I think I got it, it's because of the
.value
inreturn this.instantiate(null, "", environment, snapshot!).value
inBaseType
'screate
method, which passes backstoredValue
, which is the actual observable, and not the instance of the thing itself. I walked through the debugger on my examples and I think I see it now.I put a breakpoint on
const rootStore = RootStore.create({ subStore: { title: "hello" } })
and wrote some notes. These aren't particularly well-polished, and they're definitely missing some of the fine-grained details, but it helped a lot:create
method on theComplexType
class, and use thesnapshot
, which is what we passed in tocreate
.s…