0.2.0 • Published 10 years ago
undore v0.2.0
Undore
A persistent data structure to manage undos and redos
Example
var undore = require('undore');
// initialize the state to 10
var dataState = undore(10);
// operations does not mutate the original object
undore.set(dataState, 5);
console.log(undore.get(dataState)); // 10
// but we can manually reasign our variable
dataState = undore.set(dataState, 5);
console.log(undore.get(dataState)); // 5
// maybe 5 isn't really a nice value, let's rollback
dataState = undore.undo(dataState);
console.log(undore.get(dataState)); // 10
// nevermind, 5 is pretty awesome
dataState = undore.redo(dataState);
console.log(undore.get(dataState)); // 5
Operations
undore(initialState) → undore
Constructor of an Undore instance. This instance is in fact an instance of Immutable.Map
with three keys:
state
: The most recent and valid version of the statehistory
: AnImmutable.Stack
that contains every past versions of the state.redos
: Also anImmutable.Stack
with changes that were undone and may be redone.
Note: Undore does not enforce the use of immutable object as its state.
undore.set(undore, value) → undore
Change the value and record the change in the history.
undore.get(undore) → value
Get the current value.
undore.update(undore, updater) → undore
Update the state with a function of previous state to next state. These are equivalent:
undore.set(myState, updater(undore.get(myState)))
undore.update(myState, updater)
undore.undo(undore) → undore
Undo the latest change if any.
undore.redo(undore) → undore
Redo the latest undo if any.
Demo
License
MIT