Options
All
  • Public
  • Public/Protected
  • All
Menu

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

Hierarchy

  • History

Implements

Index

Constructors

constructor

  • new History<T>(state: IAtom<T>, maxLen?: number, changed?: Predicate2<T>): History<T>
  • 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

changed: Predicate2<T>

future

future: T[]

history

history: T[]

maxLen

maxLen: number

state

state: IAtom<T>

Static Readonly EVENT_RECORD

EVENT_RECORD: "record" = "record"

Static Readonly EVENT_REDO

EVENT_REDO: "redo" = "redo"

Static Readonly EVENT_UNDO

EVENT_UNDO: "undo" = "undo"

Accessors

value

  • get value(): T
  • set value(val: T): void

Methods

addListener

  • addListener(id: string, fn: Listener, scope?: any): boolean

addWatch

  • addWatch(id: string, fn: Watch<T>): boolean
  • IWatch.addWatch() implementation. Delegates to wrapped atom/cursor.

    Parameters

    • id: string

      watch ID

    • fn: Watch<T>

      watch function

    Returns boolean

canRedo

  • canRedo(): boolean

canUndo

  • canUndo(): boolean

clear

  • clear(): void

deref

  • deref(): T

notify

  • notify(e: Event): void

notifyWatches

  • notifyWatches(oldState: T, newState: T): void

record

  • record(state?: T): void
  • 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.

    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.

    Parameters

    • Optional state: T

      state to record

    Returns void

redo

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

    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.

    Returns undefined | T

release

  • release(): boolean

removeListener

  • removeListener(id: string, fn: Listener, scope?: any): boolean

removeWatch

  • removeWatch(id: string): boolean

reset

  • reset(val: T): T
  • 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

resetIn

  • resetIn(path: readonly [], val: T): T
  • resetIn<A>(path: Path1<T, A>, val: ValNReducer<T, A, []>): T
  • resetIn<A, B>(path: Path2<T, A, B>, val: ValNReducer<T, A, [B]>): T
  • resetIn<A, B, C>(path: Path3<T, A, B, C>, val: ValNReducer<T, A, [B, C]>): T
  • resetIn<A, B, C, D>(path: Path4<T, A, B, C, D>, val: ValNReducer<T, A, [B, C, D]>): T
  • resetIn<A, B, C, D, E>(path: Path5<T, A, B, C, D, E>, val: ValNReducer<T, A, [B, C, D, E]>): T
  • resetIn<A, B, C, D, E, F>(path: Path6<T, A, B, C, D, E, F>, val: ValNReducer<T, A, [B, C, D, E, F]>): T
  • resetIn<A, B, C, D, E, F, G>(path: Path7<T, A, B, C, D, E, F, G>, val: ValNReducer<T, A, [B, C, D, E, F, G]>): T
  • resetIn<A, B, C, D, E, F, G, H>(path: Path8<T, A, B, C, D, E, F, G, H>, val: ValNReducer<T, A, [B, C, D, E, F, G, H]>): T
  • resetIn<A, B, C, D, E, F, G, H>(path: DeepPath<T, A, B, C, D, E, F, G, H>, val: any): T

resetInUnsafe

  • resetInUnsafe(path: Path, val: any): T

swap

  • swap(fn: SwapFn<T, T>, ...args: any[]): 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

swapIn

  • swapIn<A>(path: readonly [], fn: SwapFn<T, T>, ...args: any[]): T
  • swapIn<A>(path: Path1<T, A>, fn: SwapFn<OptVal<IsOptR<T, A, []>, ValNReducer<T, A, []>>, ValNReducer<T, A, []>>, ...args: any[]): T
  • swapIn<A, B>(path: Path2<T, A, B>, fn: SwapFn<OptVal<IsOptR<T, A, [B]>, ValNReducer<T, A, [B]>>, ValNReducer<T, A, [B]>>, ...args: any[]): T
  • swapIn<A, B, C>(path: Path3<T, A, B, C>, fn: SwapFn<OptVal<IsOptR<T, A, [B, C]>, ValNReducer<T, A, [B, C]>>, ValNReducer<T, A, [B, C]>>, ...args: any[]): T
  • swapIn<A, B, C, D>(path: Path4<T, A, B, C, D>, fn: SwapFn<OptVal<IsOptR<T, A, [B, C, D]>, ValNReducer<T, A, [B, C, D]>>, ValNReducer<T, A, [B, C, D]>>, ...args: any[]): T
  • swapIn<A, B, C, D, E>(path: Path5<T, A, B, C, D, E>, fn: SwapFn<OptVal<IsOptR<T, A, [B, C, D, E]>, ValNReducer<T, A, [B, C, D, E]>>, ValNReducer<T, A, [B, C, D, E]>>, ...args: any[]): T
  • swapIn<A, B, C, D, E, F>(path: Path6<T, A, B, C, D, E, F>, fn: SwapFn<OptVal<IsOptR<T, A, [B, C, D, E, F]>, ValNReducer<T, A, [B, C, D, E, F]>>, ValNReducer<T, A, [B, C, D, E, F]>>, ...args: any[]): T
  • swapIn<A, B, C, D, E, F, G>(path: Path7<T, A, B, C, D, E, F, G>, fn: SwapFn<OptVal<IsOptR<T, A, [B, C, D, E, F, G]>, ValNReducer<T, A, [B, C, D, E, F, G]>>, ValNReducer<T, A, [B, C, D, E, F, G]>>, ...args: any[]): T
  • swapIn<A, B, C, D, E, F, G, H>(path: Path8<T, A, B, C, D, E, F, G, H>, fn: SwapFn<OptVal<IsOptR<T, A, [B, C, D, E, F, G, H]>, ValNReducer<T, A, [B, C, D, E, F, G, H]>>, ValNReducer<T, A, [B, C, D, E, F, G, H]>>, ...args: any[]): T
  • swapIn<A, B, C, D, E, F, G, H>(path: DeepPath<T, A, B, C, D, E, F, G, H>, fn: SwapFn<any, any>, ...args: any[]): T
  • Type parameters

    • A

    Parameters

    • path: readonly []
    • fn: SwapFn<T, T>
    • Rest ...args: any[]

    Returns T

  • Type parameters

    • A

    Parameters

    • path: Path1<T, A>
    • fn: SwapFn<OptVal<IsOptR<T, A, []>, ValNReducer<T, A, []>>, ValNReducer<T, A, []>>
    • Rest ...args: any[]

    Returns T

  • Type parameters

    • A

    • B

    Parameters

    • path: Path2<T, A, B>
    • fn: SwapFn<OptVal<IsOptR<T, A, [B]>, ValNReducer<T, A, [B]>>, ValNReducer<T, A, [B]>>
    • Rest ...args: any[]

    Returns T

  • Type parameters

    • A

    • B

    • C

    Parameters

    • path: Path3<T, A, B, C>
    • fn: SwapFn<OptVal<IsOptR<T, A, [B, C]>, ValNReducer<T, A, [B, C]>>, ValNReducer<T, A, [B, C]>>
    • Rest ...args: any[]

    Returns T

  • Type parameters

    • A

    • B

    • C

    • D

    Parameters

    • path: Path4<T, A, B, C, D>
    • fn: SwapFn<OptVal<IsOptR<T, A, [B, C, D]>, ValNReducer<T, A, [B, C, D]>>, ValNReducer<T, A, [B, C, D]>>
    • Rest ...args: any[]

    Returns T

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    Parameters

    • path: Path5<T, A, B, C, D, E>
    • fn: SwapFn<OptVal<IsOptR<T, A, [B, C, D, E]>, ValNReducer<T, A, [B, C, D, E]>>, ValNReducer<T, A, [B, C, D, E]>>
    • Rest ...args: any[]

    Returns T

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    • F

    Parameters

    • path: Path6<T, A, B, C, D, E, F>
    • fn: SwapFn<OptVal<IsOptR<T, A, [B, C, D, E, F]>, ValNReducer<T, A, [B, C, D, E, F]>>, ValNReducer<T, A, [B, C, D, E, F]>>
    • Rest ...args: any[]

    Returns T

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    Parameters

    • path: Path7<T, A, B, C, D, E, F, G>
    • fn: SwapFn<OptVal<IsOptR<T, A, [B, C, D, E, F, G]>, ValNReducer<T, A, [B, C, D, E, F, G]>>, ValNReducer<T, A, [B, C, D, E, F, G]>>
    • Rest ...args: any[]

    Returns T

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    Parameters

    • path: Path8<T, A, B, C, D, E, F, G, H>
    • fn: SwapFn<OptVal<IsOptR<T, A, [B, C, D, E, F, G, H]>, ValNReducer<T, A, [B, C, D, E, F, G, H]>>, ValNReducer<T, A, [B, C, D, E, F, G, H]>>
    • Rest ...args: any[]

    Returns T

  • Type parameters

    • A

    • B

    • C

    • D

    • E

    • F

    • G

    • H

    Parameters

    • path: DeepPath<T, A, B, C, D, E, F, G, H>
    • fn: SwapFn<any, any>
    • Rest ...args: any[]

    Returns T

swapInUnsafe

  • swapInUnsafe(path: Path, fn: SwapFn<any, any>, ...args: any[]): T

undo

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

    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.

    Returns undefined | T

Generated using TypeDoc