-
-
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
How to observe deep changes? #214
Comments
Nevermind found a workaround |
Any opinions on how to implement deep observation? Would like to implement it to achieve deep undo/redo functionality. Currently the solution I'm thinking of is manually calling Is there any chance you'll be adding a |
@AriaFallah I currently have no plans to introduce a deep observe. But I think you can achieve the same thing with a construction like |
How would you get the change event from doing that though? |
I think such a deep observe should be an abstraction over observe, maybe in a separate package. Because to be able to use such a feature you have to introduce some constraints on the object graph; probably you need it to be an object tree and have a consistent location, otherwise you cannot reliably determine the target? |
Fair enough, perhaps now I'll consider taking the |
Could we keep this issue open until there is a good solution /pattern found and we've updated the docs? It seems like this will be a common issue and warrants more discussion? |
Agreed. Currently it's hard to do because it would mean that you'd have to do something like using The only way I could imagine achieving something like this would be to extend the current data structures to add the recursive observation, add a separate data structure to hold the functions that unregister the listeners created by That's the only way I've thought of doing it, but I'll reopen since I'm hoping there are perhaps better and more efficient ways to do it. |
@AriaFallah I'm thinking about a feature like this. Under what circumstances should a deep observe be able to work? Should it support only trees or also graphs? Only plain objects or also typed objects? And what events should it fire, something like the current events, that state the changed object but not where that object in the tree is, or something like json patches? See http://jsonpatch.com/ |
I'll have to think about it more, but to be honest, even without my input, I'd trust your judgement on the implementation. I'll get back to you after really examining what I want out of this and what potentially other people want too. |
@AriaFallah in my projects I have done deep observation via diffing new and old state objects. This way it is very efficient to store only the differences between state changes. These diffs are very quick because you never have to diff the whole state tree as the diff is always triggered by the setter that makes the state change in the first place. The state can then be 'played back' to perform a redo by taking the initial snapshot and applying all the changes in order. Undo by playing back from the beginning up to the point where you want to go back to. An optimization is to make a snapshot and playback from that point rather than the beginning. If you're interested this is implemented here, I have not done much with it lately but let me know if you have any use of it. |
Oh wow. I actually managed to do deep observation by using spy to do exactly what you described in this PR AriaFallah/mobx-store#35. Yours is on a whole other level since it doesn't crutch on MobX. |
Yeah it's funny I learned of MobX and stopped working on this project because it performed just as well on my perf tests and the programming model is so simple and elegant. |
See also #374 This won't be fixed within mobx, for the simple reason that for clear semantics a deep observer should operate on trees, and not on arbitrarily object graphs. Stay tuned though, I'm thinking about some abstraction on top of mobx that have this restriction allowing this kind of functionality. |
Hello, one year later is there good solution for deep changes?
my adresses is array of objects in which i have got some standard properties and i can add new one property, using extendObservable
my problem is that this to work i have to trigger for once one of property in adresses[i] by changing value then the input new appear which is observable. How make it appear instantly when i add new property to adresses? |
Mobx by default tracks deeply. It's just that the Also note that adding properties with extendObservable won't be tracked. Either declare your properties up front with empty values, or use maps if you need dynamically keyed objects. |
Thanks for answering, i am new with mobx so i try hard to understand what am i missing my store is this (typescript):
by calling add or remove i have to update one of other properties so can i see the new property i added. |
@Shadowman4205:
MobX does not support dynamically keyed objects. Use observable maps instead. |
Hi, I don't know if someone is still interested by a deep observer for mobx but I build something like that here: https://github.com/dagatsoin/mobx-deep-observer |
This probably works now in v5? |
mobx-utils now exposes a |
Currently if you do
The console.log will not be triggered.
The text was updated successfully, but these errors were encountered: