Preprocessor snapshot is triggered twice #2007
-
Hello MST community, I have question regarding the preProcessor methods. Here is a simple example below. Here is a reproductible example import { types } from 'mobx-state-tree';
const ModelWithoutSnProc = types.model({
someProp: types.string,
});
const Model = types.snapshotProcessor(ModelWithoutSnProc, {
preProcessor(sn) {
console.log('preprocessor', sn); // => two logs show up in the console
return {
someProp: sn.whateverApiProp,
};
},
});
const myModel = Model.create({ whateverApiProp: 'coucou' }); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
It looks like this might be expected/acceptable behavior? It's an old thread, but it seems to suggest this is intended. However, I don't see how your example is modifying anything, so I can't quite explain the double-run. I'm going to dig a little deeper here, but this isn't the first time folks have talked about this. Very interesting. |
Beta Was this translation helpful? Give feedback.
Oh, I think I understand more after reading more carefully.
The original post in that issue says:
And links to the relevant code.
I would guess, since MST typechecks in development but not production that you could get around this by turning off the type checking for your model? Or that you might need to design your preProcess functions to be idempotent, as the responses in that thread conclude.
Does that help you at all?