@s-libs/signal-store
    Preparing search index...

    Class UndoManagerSuperclass<StateType, UndoStateType>Abstract

    Assists in creating undo/redo functionality. Below is a minimal undo service.

    @Injectable({ providedIn: 'root' })
    class UndoService extends UndoManagerSuperclass<MyAppState, MyAppState> {
    constructor() {
    super(inject(MyAppStore));
    }

    protected extractUndoState(state: MyAppState): MyAppState {
    // In practice, you'll usually want to track only part of the state...
    return state;
    }

    protected applyUndoState(stateToApply: MyAppState): void {
    // ...and restore only those parts here.
    this.store.state = stateToApply;
    }
    }

    Then in your app you can simply call service.pushCurrentState() after any user interaction to preserve it in the undo stack.

    ++myStore('counter').state;
    undoService.pushCurrentState();

    undoService.undo(); // sets the store back to 0
    undoService.redo(); // sets the store to 1 again

    Type Parameters

    • StateType
    • UndoStateType
    Index

    Constructors

    Properties

    canRedo: Signal<boolean> = ...

    Emits whether any states are available for redo().

    canUndo: Signal<boolean> = ...

    Emits whether any states are available for undo().

    maxDepth: number = 0

    The maximum size of the history before discarding the oldest state. 0 means no limit.

    state: Signal<UndoStateType> = ...

    Emits the current undo state that was most recently pushed or applied. Calls to undo will apply the state before this in the stack, and redo will apply the state after this.

    Accessors

    Methods

    • Reset the store to the given state.

      The undoOrRedo and stateToOverwrite parameters can be useful e.g. if a scroll position is kept in the undo state. In such a case you want to change the scrolling so the user can see what just changed by undoing/redoing. To do that, set the scoll to what it was in stateToOverwrite when undoing, and to what it is in stateToApply when redoing.

      Parameters

      Returns void

    • Drops the state from the internal undo stack that would be applied by a call to .undo(). This is useful e.g. if making multiple changes in a row that should be collapsed into a single undo state: call this first before pushing the new version.

      Returns void

      Error when there is no such state (i.e. when canUndo() returns false)

    • Each time a state is added to the history, this method will be called to determine whether the oldest state should be dropped. Override to implement more complex logic than the simple maxDepth.

      Parameters

      • size: number

      Returns boolean

    • Add the current state to the undo history. Any states that could be reached using redo() are discarded.

      Parameters

      • __namedParameters: { collectDebounce?: number; collectKey?: string } = {}

      Returns void