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
Pass env
into preProcessSnapshot
#858
Comments
tbc, I intend to try to carve out some time to implement this and submit a PR. Interested in thoughts about the feature request though. |
I think that change would be in principle fine, although there is a small risk; if The reason for that is that preProcesssnapshot might be called without actually creating an instance, for example in a |
I second this (for postProcessSnapshot), although I understand the concern that transformations should be "idempotent, pure and cheap", so I wonder if it is a good idea after all. We are working on a feature that relates some models to a store that describes asynchronous tasks. We're not using types.reference (perhaps that's a better solution), but a simple id instead, and the task store is not part of the rootStore, it is injected through So our idea was to access this store on postProcessSnapshot, and depending on the state of the asynchronous task, the serialization would be slightly different. I might take a shot at this, just to see how it would behave. |
Hey folks, This MobX-State-Tree issue is pretty old and inactive, so I'm gonna close it out. But the good news is we implemented something related in #2116, where we now provide the parent node to the post processor. Not exactly the same intention but it should solve similar problems. If anyone wants to discuss, please feel free to open a discussion thread, new issue, or better yet, a PR. Thanks for your time, hope all is well! |
I believe
env
would always be available (if it exists) inapplySnapshot(target, data)
andFoo.create(data, env)
situations. When applying a snapshot, callgetEnv
on target before callingpreProcessSnapshot
, and in create it's either there or it's not (user decides).If I had
env
as a second argument topreProcessSnapshot
I could, for example, use potentially different transformation rules based on env (or the transformation function itself could be injected, etc).Another use case is one I'm currently faced with, which is that our server API routes could potentially return fully populated child objects that actually point to another resource's MST store (all top level stores glued together with a single composition root). So in those situations we want to peel those objects off and put them in their respective stores, replacing that path in the current object with a reference (a normalization step, to allow API routes to be as fat or thin as they want to be). So right now I'm doing this in an action before calling
applySnapshot
orcreate
, accessing the various applicable stores viaenv
. This is logic I would love to generalize in thepreProcessSnapshot
(i.e. a processor that has non-local side effects)The text was updated successfully, but these errors were encountered: