createHistory()
createHistory<
T>(initial): History<T>
Creates a history manager for undo/redo functionality.
Tracks state changes as snapshots. Pushing a new state clears any redo history (standard undo/redo behavior).
Type Parametersβ
T: Tβ
The state type
Parametersβ
initial: Tβ
The initial state
Returns: History<T>β
A history manager
Sinceβ
2.4.0
Exampleβ
const history = createHistory({ count: 0 });
history.push({ count: 1 });
history.push({ count: 2 });
history.push({ count: 3 });
history.undo(); // Some({ count: 2 })
history.undo(); // Some({ count: 1 })
history.redo(); // Some({ count: 2 })
// New push clears redo stack
history.push({ count: 10 });
history.canRedo(); // false
Snapshot<T>β
Snapshot<
T> =object
A snapshot with metadata.
Sinceβ
2.4.0
Type Parametersβ
T: Tβ
The state type
Propertiesβ
state: Tβ
timestampβ
readonlytimestamp:number
History<T>β
History<
T> =object
History manager for undo/redo functionality.
Sinceβ
2.4.0
Type Parametersβ
T: Tβ
The state type
Propertiesβ
current(): () => Tβ
Returns the current state.
Returns: Tβ
push(): (state) => voidβ
Pushes a new state, clearing any redo history.
state:
T
Returns:void
undo(): () => Option<T>β
Undoes to previous state. Returns the new current state, or None if at beginning.
Returns: Option<T>β
redo(): () => Option<T>β
Redoes to next state. Returns the new current state, or None if at end.
Returns: Option<T>β
canUndo(): () => booleanβ
Returns true if undo is possible.
Returns: booleanβ
canRedo(): () => booleanβ
Returns true if redo is possible.
Returns: booleanβ
history(): () => ReadonlyArray<Snapshot<T>>β
Returns all snapshots in the undo stack (oldest first).
Returns: ReadonlyArray<Snapshot<T>>β
clear(): () => voidβ
Clears all history, keeping only current state.
Returnsβ
void