mobx-state-tree
mobx-state-tree copied to clipboard
bugfix: run preprocessor of types.snapshotProcessor before running applySnapshot
Resolves https://github.com/mobxjs/mobx-state-tree/issues/1317
When using types.snapshotProcessor & using applySnapshot, the snapshot wasn't being processed currently.
@Jarrku Could you write a test for this that fails on master but is resolved by this change, please? I tried cloning it down and testing it but there are some TypeScript issues.
Hey @jamonholmgren! Provided a test case, also see the TS errors now, but Im unsure on how to resolve them?
In the same way as getSnapshot is defined perhaps?
getSnapshot(node: this["N"], applyPostProcess: boolean = true): this["S"] {
const sn = this._subtype.getSnapshot(node)
return applyPostProcess ? this.postProcessSnapshot(sn) : sn
}
@Jarrku What's the latest on this? Happy to merge when ready!
Please merge this!
@Jarrku @jamonholmgren Any update on this bug? Can you merge this? (If some more work is needed maybe i could help?)
Hi there 👋 just chiming in to say this resolves an issue in my codebase as well, would be happy to help in any way to get this fix merged into master. Seems like a pretty critical omission from snapshotProcessor!
Hi @Jarrku @jamonholmgren! I hope you are doing well! Could you please merge this PR? It would be extremely beneficial.
This has some issues and I had to revert it. @Jarrku can you take a look when you get a chance?
yarn build
yarn run v1.22.19
$ lerna run build --stream
lerna notice cli v4.0.0
lerna info Executing command in 2 packages: "yarn run build"
mobx-state-tree: $ yarn clean && shx cp ../../README.md . && tsc && cpr lib dist --filter=\.js$ && rollup -c
mobx-state-tree: $ shx rm -rf dist && shx rm -rf lib
mobx-state-tree: src/types/utility-types/snapshotProcessor.ts(99,39): error TS2339: Property 'applySnapshot' does not exist on type 'this["N"]'.
mobx-state-tree: src/types/utility-types/snapshotProcessor.ts(100,14): error TS2339: Property 'applySnapshot' does not exist on type 'this["N"]'.
mobx-state-tree: src/types/utility-types/snapshotProcessor.ts(100,31): error TS7006: Parameter 'snapshot' implicitly has an 'any' type.
mobx-state-tree: error Command failed with exit code 2.
mobx-state-tree: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
lerna ERR! yarn run build exited 2 in 'mobx-state-tree'
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
If I use as any to bypass the TypeScript errors, other errors come up with yarn test.
Hi there!
I realized recently that preProcessor actually works successfully with complex stores when one model is inside another model, for instance, preProcessor will work here:
const TwitterStore = types.model("TwitterStore", {
tweets: types.array(TweetProcessed); // where TweetProcessed was built by types.snapshotProcessor
});
BUT it won't work with simple models like this:
const Tweet = types.model("Tweet", {
body: types.string,
});