Aller au contenu principal

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>

Type

Snapshot<T> = object

A snapshot with metadata.


Since

2.4.0


Type Parameters

T: T

The state type


Properties

state: T

timestamp

readonly timestamp: number

History<T>

Type

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