mobxjs / mobx

Simple, scalable state management.
http://mobx.js.org
MIT License
27.52k stars 1.77k forks source link

Undo/redo pattern? #88

Closed skosch closed 8 years ago

skosch commented 8 years ago

Hey,

mobservable looks fanstastic – it certainly promises to be much less verbose than even the most concise Redux app. Still, I often do need to support undo/redo stacks. I'm aware of @mweststrate's experiments in #9, but it seems like that didn't go anywhere. If I needed a domain data store with undo-able actions, how should I go about it?

I've come across other non-Flux approaches that try to do this, e.g. https://github.com/omniscientjs/immstruct ... might there be a way of combining those ideas?

Many thanks again for sharing this project with the community!

mweststrate commented 8 years ago

Efficient undo / redo is very doable by tracking your state using immutable shared data structures. See for example the reactive 2015 demo which features time travelling and the createTransformer pattern that can be used to achieve patterns like these.

(another approach is to record patches, see for example these packages: https://www.npmjs.com/package/mobservable-patch-stream https://www.npmjs.com/package/observable-delta-stream or the extras.trackTransactions api, although that is mainly for debugging)

skosch commented 8 years ago

Aha, you just autorun your own serializer ... makes sense, thank you! Now I'd probably want to be able to diff two stored states so that upon undo, I can send a changeset to the server. Unfortunately the two npm packages you linked to (by @tetsuo) are dead ... any idea what happened to them?

skosch commented 8 years ago

... actually, never mind: https://github.com/chbrown/rfc6902 would do the job just fine for me.

tetsuo commented 8 years ago

@skosch @mweststrate Ah, observable-delta-stream and mobservable-patch-stream are both experimental. delta stream one is very heavy, it walks the entire tree for every change, but I will definitely take a look at the latter.