createCommandStack()
createCommandStack():
object
Creates a command stack with undo/redo support.
This stores actions (execute/undo pairs). When you undo, it calls the command's undo function. Use this when you have reversible operations.
For storing states (snapshots) instead of actions, see the Memento pattern's
createHistory which stores state snapshots and restores them on undo.
Returns
Stack with execute, undo, redo, canUndo, canRedo, clear
execute(): (cmd) => void
Execute a command and push it to the undo stack. Clears redo stack.
cmd:
UndoableCommand
Returns:void
undo(): () => boolean
Undo the last command. Returns false if nothing to undo.
Check canUndo first if the result matters to your logic.
Returns: boolean
redo(): () => boolean
Redo the last undone command. Returns false if nothing to redo.
Check canRedo first if the result matters to your logic.
Returns
boolean
canUndo
Get Signature: > *get canUndo*(): boolean
Returns
boolean
canRedo
Get Signature: > *get canRedo*(): boolean
Returns: boolean
clear(): () => void
Clear all history.
Returns: void
Since
2.4.0
Example
let value = 0;
const stack = createCommandStack();
stack.execute(undoable(() => value += 10, () => value -= 10));
stack.execute(undoable(() => value *= 2, () => value /= 2));
// value = 20
stack.undo(); // value = 10
stack.undo(); // value = 0
stack.redo(); // value = 10