xaviergonz / mobx-keystone

A MobX powered state management solution based on data trees with first class support for Typescript, support for snapshots, patches and much more
https://mobx-keystone.js.org
MIT License
549 stars 25 forks source link

Snapshot without meta-information ($modelId and $modelType) #123

Closed ZFail closed 4 years ago

ZFail commented 4 years ago

Is it possible to initialize a model from a snapshot without $modelId and $modelType fields? Like in MST. As I understand it, if you specify the root model, and also if there is information about types of all models, then you can create a model without meta-information.

ckwong90 commented 4 years ago

I've looked into this as well. I think keystone's api is better than mobx-state-trees especially when working with Typescript but the way it encodes $modelType and $modelId bothers me as well.

It's probably designed that way since runtime checking (tprop) is not a requirement, and just uses $modelType to figure out the correct type creator. In addition $modelId for each type is used for reconciliation, I think it would take a major rewrite encode $modelType and $modelId a different way,

Some ideas:

Currently, we can hydrate a model with fromSnapshot() since all of the type information is encoded in the snapshot. We will have to remove this requirement and hydrate models from its constructor:

i.e. new Model(snapshot) or Model.create(snapshot)

I don't know if this is possible without using the require runtime checking method (tprop) since we need to encode the Model shape inside the Model itself. Also we need to have way the resolve union types from snapshot.

At this point I think we're getting so close to mobx-state-tree, it might be better to use that library if you don't want the modelType and modelID encodings.

xaviergonz commented 4 years ago

@ckwong90 that is pretty much spot on.

ZFail commented 4 years ago

@ckwong90 Using mobx-state-tree after the keystone is painful. I think that implementing loading snapshots without meta information is a good idea.

Implemented first steps for loading snapshots. It looks pretty simple if you use a tProp. https://github.com/ZFail/mobx-keystone/commit/c1e9633b83e8b22f7bf44ca98521276f55f8152c

xaviergonz commented 4 years ago

Just wondering, do functional models help?

https://mobx-keystone.js.org/functionalModels

xaviergonz commented 4 years ago

Closing due to inactivity, feel free to reopen if needed