Use this trackHistory
functionality like so:
const state$ = observable({
name: "Hello",
whatever: [],
})
const { undo, redo, undoable$, redoable$ } = trackHistory(state$)
if (undoable$.get()) undo()
if (redoable$.get()) redo()
The undoable$
and redoable$
observables are useful because you may want to dim out or enable the undo/redo buttons.
When you undo() n times, you can redo() n times as well; however, if you change state before you redo() then it throws away the redo
stack.
Caveat: It's currently unbounded and saves deep copies of every state change, so if you want to avoid that you'll have to build in where it drops state changes after a certain size.