-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
intercept()
and observe()
don't track nested observables
#302
Comments
Intended unfortunately. Although I would like a way to be able to deeply observe as well. Currently there is |
So, Make sense but again I was a bit surprised. I will try to do a pull request for the docs and add a simple clarifying sentence. |
Ah completely messed up previous comment. I meant to add that I got confused with autorun as well. As for I also wish there was a nested option for |
Doc PR's are more then welcome! If you need to deep observe, I recommend using |
so why this is intentional? is there a limitation in intercepting nested values? you can also listen on: appState.todos.intercept(fn) but maybe there's harm in handling both? edit: nevermind on the intercept example above, that only works on things like push(...) |
From what I know, spy(function(change) {
// references match
if (change.object === myObject) {
// Do stuff with change info
}
}) I experimented a lot with deep observation, and short of adding a non-enumerable field marking the parent of a given observable (which is in my opinion too much work), it didn't work well. A possible alternative is wrapping all the changes you want to intercept inside of const myObject = { a: 1 }
const modifyObject = action('modify', function() {
myObject.a = 5
})
spy(function(change) {
// action which modifies all your objects detected!
if(change.type === 'action' && change.name === 'modify') {
console.log(myObject) // myObject before it's changed!
}
}) |
The reason it doesn't deep observe is that it it cannot be generalized, if you have a cyclic structure deep observing will either utterly crash or be very expensive to prevent that. MobX doesn't make assumptions on your data structures and hence cannot generalize this. In contrast to for example mobx-state-tree, which does support deep observing out of the box, but as a tradeoff you are required to limit yourself to using data trees instead of data graphs. |
ah okay, thanks for the info & links guys. I think I'm definitely leaning more towards what you're doing with mobx-state-tree, basically trying to combine tcomb with mobx, using intercept to reject invalid state updates. |
No output. Bug or feature?
The text was updated successfully, but these errors were encountered: