For fun and to learn I'm trying to implement an undo system in my app using functional reactive programming. I have a stream of state changes, which need to be saved onto the undo stack. When the user clicks undo, I take a value from the stack and update application state accordingly.
The problem is that this update itself also generates an event in the state change stream. So what I would like is to derive another stream from state changes, which ommits state change right after undo.
A simple diagram:
states ----S----S----S----
undos -------U-----------
save ----S---------S----
The first line is the stream of application state changes, the second line are undo events triggered by the user and the third line is the stream I'd like to implement and listen to instead of the first stream.
What is the best way to express such intent in FRP?
S1-U1-S0-S2
vsS1-S2
-> i.e. when you are doing your Undo_1 (U1), your state would efectively change to State_0 (not shown in your stream #3), and thus you MUST either have that state change in that place, OR show State0, and remove State1 from stream #3. // hope you understand what i mean. – c69[{a:1}, {a:2}, {x: !0}, {a:3}, {a:4}, {a:5}].reduce(function(a,v){ if (v.x) {a.pop()} else a.push(v) ; return a; }, [])
. -->yourMergedStream.reduce(function removePreviousUndo(a,v) {/*...*/})
see: baconjs.github.io/… – c69