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

onSnapshot and onPatches fail when called from an external module #443

Open
gouldingken opened this issue Mar 24, 2022 · 5 comments
Open
Labels

Comments

@gouldingken
Copy link

I have a working solution in a typescript project. When I watch for changes using onSnapshot and onPatches within that project, it works perfectly. However when I move the exact same code to a library and include it, the method fails with the following error:
Error: subtreeRoot must be a tree node (usually a model or a shallow / deep child part of a model 'data' object)

I thought there might be a version clash between the keystone dependencies, but I have tried used peer dependencies as well as ensuring that the referenced versions are identical.

When I debug I can confirm that the issue is arising because tweakedObjects in the local instance contains the object, but when used as a module, it does not. It looks like these WeakMaps are being declared at the module root. If that's the case then I guess it makes sense why keystone wouldn't work correctly across modules. Does anyone know how to address this issue?

Call stack:

subscribe.ts:239 Error: subtreeRoot must be a tree node (usually a model or a shallow / deep child part of a model 'data' object)
    at failure (mobx-keystone.es.mjs:86:1)
    at assertTweakedObject (mobx-keystone.es.mjs:387:1)
    at onPatches (mobx-keystone.es.mjs:2872:1)
@xaviergonz
Copy link
Owner

are you using yarn 2 or 3 by any chance?

@gouldingken
Copy link
Author

1.22.18

gouldingken pushed a commit to sasakiassociates/collaborate-on-fire-example that referenced this issue Mar 24, 2022
pulled in Keystone Container owing to this issue: xaviergonz/mobx-keystone#443
@xaviergonz
Copy link
Owner

I'm afraid without an example project or something I can't help much.
If you are using yarn v1 then setting mobx-keystone as peer dependency on your dependency and setting it as dependency on the main app should be enough for it to only use one version (you can just check there should be a single instance in the root node_modules.

If you are using yarn v3 then you need to hoist it manually, this is, set it as peer dep in both packages (the lib and the app) and adding it as a dep to the root package.json. Actually you may try to do that with yarn v1 too, maybe it will work.

@gouldingken
Copy link
Author

I can try to put together a simple example to demonstrate the issue. Do you have any examples where the lib approach works?

@xaviergonz
Copy link
Owner

I've seen it working, but on private code.
If you still have this issue a github project / sandbox that shows this would help indeed.

@xaviergonz xaviergonz added the 🤷‍♂️ more info required More info is required label Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants