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 26 forks source link

Mobx State Tree compatibility layer #156

Open nfour opened 4 years ago

nfour commented 4 years ago

@xaviergonz this is a very cool project. I've been using MST for a year or 2 now and I can appreciate how you've managed to solve quite of few problems with the MST's implementation.

As per a discussion in #141, I realized I'd need some form of interoperability between Keystone & MST models if I were to adopt this in production.

Perhaps helpers like getParentOfType could be the place to begin.

Thoughts?

xaviergonz commented 4 years ago

I think you can mock up getParentOfType kind of easily with findParent

https://mobx-keystone.js.org/public/api/index.html#findparent

plus instanceof / Array.isArray / typeof on the check function.

Or is there something more specific you are looking for?

nfour commented 4 years ago

Specifically I'm interesting in composing the end result of MST models inside Keystone, and Keystone in MST.

Example:

class MyKeystoneModel extends ...

const BazModel = types.model({ foo: 'bar'})

const RootModel = types.model({
  foo: makeMstCompatible(MyKeystoneModel),
  baz: BazModel
})

RootModel.create()

I image a great way for an existing MST user to start using Keystone would be to convert a single MST model to Keystone and expecting that Keystone can retrieve parent nodes, and the root MST model can instantiate any children Keystone models.

Considering the different signatures, I guess I'm looking for guidance in how user like me could begin to make these interfaces compatible.

mikecann commented 4 years ago

I suspect this will be really tricky to get 100% right due to underlying differences in the way the two libraries work. Things like unions, identifiers, custom types various etc are different.

It might be possible to create a code-gen library or something that spits out a "best effort" keystone model from a MST one?