Simple data structure for and undo/redo stack, using ImmutableJS. The structure holds a stack of undo states, and a stack of redo states. It does not keep a reference to the current state to avoid duplicating data if your state is already stored in another structure.
npm install --save immutable-undo
{Object} [opts]
Option object{Number} [opts.maxUndos=500]
Maximum number of undos stored. Beyond that, some undos are dropped, according to the specified drop strategy.{ (History) => History } [opts.strategy=History.lru]
The drop strategy. See strategies.return {History}
An empty history
var history = History.create({
maxUndos: 100
});
return {Boolean}
True if the history has undos
return {Boolean}
True if the history has redos
return {State | Undefined}
The most recent state in the undo stack
return {State | Undefined}
The most recent state in the redo stack
{State} newState
The state to push on top of the undo stackreturn {History}
Pushes a new state on the undo stack, and clears the redo stack
{State} current
The current state, that will be pushed on the redo stackreturn {History}
The history with the undo stack popped once, and the current state added to redos.
{State} current
The current state, that will be pushed on the undo stackreturn {History}
The history with the redo stack popped once, and the current state added to redos
These are built-in strategies for keeping the undo stack under its size limit.
Least Recently Used. Simply drops the oldest undo to make room for new one.
Will always keep the initial state (the first undo), and will drop undos such as the oldest undos grow more and more spaced in time, but the more recent undos keep a high precision.
You can imagine the density of undos over time becoming more like this curve: