Comments (16)
I think this may be a docs and TypeScript issue. You're calling preProcessor
and postProcessor
, but the types.snapshotProcessor
actually wants preProcess
and postProcess
.
You can see it working here: https://codesandbox.io/p/sandbox/issue-2136-7hpyd2?file=%2Fsrc%2Findex.ts%3A21%2C3
But there's TypeScript errors on those actions.
Here's the working code for posterity:
import { types, applySnapshot } from "mobx-state-tree";
const Model = types.array(
types.model({
b: types.string,
c: types.optional(
types.model({
d: types.snapshotProcessor(types.model({ e: "" }), {
// TypeScript errors here, but I think that's incorrect
preProcess(snapshot: { e: string }) {
return { e: snapshot.e };
},
// TypeScript errors here, but I think that's incorrect
postProcess(snapshot: { e: string }) {
return snapshot.e;
},
}),
}),
{ d: { e: "e" } }
),
})
);
const m = Model.create([{ b: "b" }]);
applySnapshot(m, []);
I'll tag this, and please accept our apologies for the bug! We'll close this issue when we fix the types, but please let me know if I've missed anything else.
from mobx-state-tree.
@coolsoftwaretyler In your version preProcess
/postProcess
aren't even called.
Not sure you're right about naming:
https://github.com/mobxjs/mobx-state-tree/blob/master/src/types/utility-types/snapshotProcessor.ts#L60
https://github.com/mobxjs/mobx-state-tree/blob/master/src/types/utility-types/snapshotProcessor.ts#L75
from mobx-state-tree.
Ah, my bad! I had a false positive, was just getting those errors to go away.
There is a documentation issue. We call it preProcess
and postProcess
here: https://mobx-state-tree.js.org/overview/hooks#snapshot-processing-hooks. That's what threw me and got me here.
I'll remove the TypeScript label here. I've been messing around with the code and I think this is coming from postProcessor
. If you remove that action, the error goes away.
from mobx-state-tree.
Seems to be related to applying the snapshot as an empty array. I can get the processed snapshot, modify it in place, and apply it correctly:
import { types, applySnapshot, getSnapshot } from "mobx-state-tree";
const Model = types.array(
types.model({
b: types.string,
c: types.optional(
types.model({
d: types.snapshotProcessor(types.model({ e: "" }), {
preProcessor(snapshot: { e: string }) {
return { e: snapshot.e };
},
postProcessor(snapshot: { e: string }, node) {
return {
...snapshot,
e: "modified",
};
},
}),
}),
{ d: { e: "e" } }
),
})
);
const m = Model.create([{ b: "b" }]);
const snapshot = getSnapshot(m);
console.log("snapshot", snapshot);
const modifiedSnapshot = [...snapshot];
console.log("modifiedSnapshot", modifiedSnapshot);
modifiedSnapshot[0].c.d.e = "modifiedAgain";
applySnapshot(m, modifiedSnapshot);
console.log("snapshot one last time", getSnapshot(m));
Updated example there:
https://codesandbox.io/p/sandbox/issue-2136-7hpyd2?file=%2Fsrc%2Findex.ts%3A41%2C1
But if you try to apply as an empty array, the error comes back.
from mobx-state-tree.
Sorry, I can't open your example:
from mobx-state-tree.
Whoops, getting used to the new CodeSandbox tools. This should work: https://codesandbox.io/p/sandbox/issue-2136-7hpyd2?file=%2Fsrc%2Findex.ts%3A41%2C1
from mobx-state-tree.
I've got some pass-by-reference errors in there, but I gotta run at the moment so I can revisit this later. Sorry 'bout that!
from mobx-state-tree.
It's not related with an empty array, following code throws the same error:
const Model = types.array(
types.model({
b: types.string,
c: types.optional(
types.model({
d: types.snapshotProcessor(types.model({ e: '' }), {
preProcessor(e: string) {
return { e };
},
postProcessor(snapshot) {
return snapshot.e;
}
})
}),
{ d: 'e' }
)
})
);
const m = Model.create([{ b: 'b' }, { b: 'bb' }]);
applySnapshot(m, [{ b: 'b' }]); // error
Interestingly, if I remove types.optional
in the middle then everything works:
const Model = types.array(
types.model({
b: types.string,
c:
types.model({
d: types.snapshotProcessor(types.model({ e: '' }), {
preProcessor(e: string) {
return { e };
},
postProcessor(snapshot) {
return snapshot.e;
}
})
})
})
);
const m = Model.create([{ b: 'b', c: { d: '' } }, { b: 'bb', c: { d: '' } }]);
applySnapshot(m, [{ b: 'b', c: { d: '' } }]);
from mobx-state-tree.
Interesting. Maybe types.optional
is having trouble instantiating the default value as a snapshotProcessor
.
from mobx-state-tree.
Hopefully it's just this: #2137
Once we get a test in there I can merge and deploy a patch, probably early this week.
from mobx-state-tree.
Unfortunately, no (
It just fixes import but exception will be thrown anyway, I've checked it.
from mobx-state-tree.
Ah dang. Do you have the full text of the exception with that patched? That might be helpful
from mobx-state-tree.
Ah dang. Do you have the full text of the exception with that patched? That might be helpful
from mobx-state-tree.
Thank you! I'll see if I can get anyone to investigate. If not, I can at some point in the coming weeks. Would welcome a PR if you trace the error on your own as well. Apologies for the inconvenience!
from mobx-state-tree.
Hi, @coolsoftwaretyler! Is there any progress?
from mobx-state-tree.
Sorry, not yet! Haven't had anyone volunteer to work on it.
from mobx-state-tree.
Related Issues (20)
- RootStore type becomes "any" the moment we call a RootStore model action using yield on a child model action HOT 3
- Unintended behavior when creating new models with falsy first argument HOT 8
- detach can corrupt the identifierCache HOT 2
- Add tests and documentation for debug names in complex types
- Add tests and documentation for new parent argument to `types.optional` HOT 1
- Document how MST lazily creates objects HOT 1
- Use getTime() to check for Date equality HOT 8
- Remove Lerna config and lift MST sub-package up
- When observe() a MST node's primitive property, TypeScript get the wrong type of oldValue & newValue HOT 1
- Add safeMap utility (or something like it)
- Broken Map type in 5.3.0 HOT 8
- TypeError: 0, _mobx.defineProperty is not a function (it is undefined) HOT 4
- applySnapshot is slow HOT 1
- Linters do not recognize `fail` as a required import in library code HOT 4
- REST - create data with server side ID HOT 2
- Better documentation on action/view definitions and their impact on IDE introspection, navigation and refactoring HOT 2
- Missing docs for `types.lazy` HOT 2
- getMembers does not work as expected, behaviour change between 5.1.0 and 5.4.1 HOT 11
- Model constructor modifies descriptor object HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mobx-state-tree.