Class History<T>

Undo/redo history stack wrapper for atoms and cursors. Implements IAtom interface and so can be used directly in place and delegates to wrapped atom/cursor.

Remarks

Value changes are only recorded in history if changed predicate returns truthy value, or else by calling History.record directly. This class too implements the INotify interface to support event listeners for History.undo, History.redo and History.record.

Type Parameters

  • T

Implements

Constructors

  • Type Parameters

    • T

    Parameters

    • state: IAtom<T>

      parent state

    • maxLen: number = 100

      max size of undo stack

    • Optional changed: Predicate2<T>

      predicate to determine changed values (default !equiv(a,b))

    Returns History<T>

Properties

changed: Predicate2<T>
future: T[]
history: T[]
maxLen: number
state: IAtom<T>

Accessors

Methods

  • IWatch.addWatch() implementation. Delegates to wrapped atom/cursor.

    Parameters

    • id: string

      watch ID

    • fn: Watch<T>

      watch function

    Returns boolean

  • Records given state in history. This method is only needed when manually managing snapshots, i.e. when applying multiple swaps on the wrapped atom directly, but not wanting to create an history entry for each change.

    Parameters

    • Optional state: T

      state to record

    Returns void

    Remarks

    DO NOT call this explicitly if using History.reset / History.swap etc.

    If no state is given, uses the wrapped atom's current state value (user code SHOULD always call without arg).

    If recording succeeded, the History.EVENT_RECORD event is emitted with the recorded state provided as event value.

  • Attempts to re-apply most recent value from future stack to atom and returns it if successful (i.e. there's a future).

    Returns undefined | T

    Remarks

    Before the switch, first records the atom's current value into the history stack (to enable History.undo feature). Returns undefined if there's no future (so sad!).

    If redo was possible, the History.EVENT_REDO event is emitted after the restoration with both the prev and curr (restored) states provided as event value (and object with these two keys). This allows for additional state handling to be executed, e.g. application of the "Command pattern". See History.addListener for registering event listeners.

  • IReset.reset() implementation. Delegates to wrapped atom/cursor, but too applies changed predicate to determine if there was a change and if the previous value should be recorded.

    Parameters

    • val: T

      replacement value

    Returns T

  • ISwap.swap() implementation. Delegates to wrapped atom/cursor, but too applies changed predicate to determine if there was a change and if the previous value should be recorded.

    Parameters

    • fn: SwapFn<T, T>

      update function

    • Rest ...args: any[]

      additional args passed to fn

    Returns T

  • Attempts to re-apply most recent historical value to atom and returns it if successful (i.e. there's a history).

    Returns undefined | T

    Remarks

    Before the switch, first records the atom's current value into the future stack (to enable History.redo feature). Returns undefined if there's no history.

    If undo was possible, the History.EVENT_UNDO event is emitted after the restoration with both the prev and curr (restored) states provided as event value (and object with these two keys). This allows for additional state handling to be executed, e.g. application of the "Command pattern". See History.addListener for registering event listeners.

Generated using TypeDoc