mobx-state-tree icon indicating copy to clipboard operation
mobx-state-tree copied to clipboard

bugfix: run preprocessor of types.snapshotProcessor before running applySnapshot

Open Jarrku opened this issue 3 years ago • 4 comments

Resolves https://github.com/mobxjs/mobx-state-tree/issues/1317

When using types.snapshotProcessor & using applySnapshot, the snapshot wasn't being processed currently.

Jarrku avatar Oct 27 '21 10:10 Jarrku

@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.

jamonholmgren avatar Dec 04 '21 23:12 jamonholmgren

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 avatar Dec 05 '21 11:12 Jarrku

@Jarrku What's the latest on this? Happy to merge when ready!

jamonholmgren avatar Feb 28 '22 19:02 jamonholmgren

Please merge this!

bkmaibach avatar Aug 09 '22 17:08 bkmaibach

@Jarrku @jamonholmgren Any update on this bug? Can you merge this? (If some more work is needed maybe i could help?)

yossivainshtein avatar Dec 11 '22 10:12 yossivainshtein

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!

lsthornt avatar Jan 08 '23 21:01 lsthornt

Hi @Jarrku @jamonholmgren! I hope you are doing well! Could you please merge this PR? It would be extremely beneficial.

vadimcoder avatar Jan 16 '23 19:01 vadimcoder

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.

jamonholmgren avatar Mar 10 '23 04:03 jamonholmgren

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,
  });

vadimcoder avatar Mar 13 '23 14:03 vadimcoder