-
-
Save threepointone/43f16389fd96561a8b0b to your computer and use it in GitHub Desktop.
// store.js | |
let {store, handler} = sto(initialState, reduceFn); // where reduceFn: function(currentState, action, ...args){} | |
dispatcher.register(handler); | |
export store; | |
// elsewhere | |
store.get() // -> current state | |
store.toObservable() // -> to be used with .observe() | |
// that's it. |
Fair point, just tried with 500 actions and zero delay and it froze hard for a couple of seconds.
But then stores would need a .setState() equivalent? Again, doable if only for time travel and not used for anything else. I'm open to alternate implementations.
Alternately -
- freeze view renders while replaying and ONLY render the last state. this seems to make it much faster.
- run the replay on requestAnimationFrame/setInterval (fn, 0)/setImmediate(fn) per action. it's not "instantly" instantaneous, but it looks great (if what you're looking for a is a fast visual replay)
- maybe run the dispatcher/stores on a web worker while time travelling? too sleepy to think of an implementation right now, but sure has hipster value.
(will get back to this when I wake, but please feel free to keep adding stuff)
What are the usecases for timetravel in stores? Debugging, surely, but what else? Undo/redo?
But then stores would need a .setState() equivalent?
Why? If they don't own state and only tell how to compute it, maybe “dispatcher” has the power to give components some older state of all stores instead of the current state.
What are the usecases for timetravel in stores?
Debugging, for sure. Now that I'm thinking of it, I also want to support restore-from-JSON and serialize-to-JSON. Which doesn't usually work well with Flux —but— our Stores don't need to own the state. The may just specify calculation. If dispatcher holds state and uses stores to reduce it and “advance” it, it can hold it in a single tree internally and snapshot/restore/rollback to any point.
But that's the thing, they do own it. Specifically, they hide it. Like an observable; you can't set the 'current' value to operate on. To get around this you'll need a .setState()
, at least exposed to the dispatcher.
Alternately, yes, the dispatcher/view controller could bypass all that and load cached state. But then you can't fire further actions until you get back to where you came from.
just read the part where dispatcher holds state. good idea, need to think it though..
export const store = dispatcher.register(initialState, reduceFn);
this changes the register signature, but should be easier to do the above now.
Here's something I played with: https://gist.github.com/gaearon/c02f3eb38724b64ab812
Let me know your thoughts!
Yeah... But. Since we already got that state, why not cache it? (Only in dev, say, for last 500 actions.) It seems to me that one can't achieve a smooth travelling experience if repopulating the stores is
O(n)
wheren
is how far.