Skip to content

[undo/redo]: clearStack is executed before the store is modified #190

@richardliebmann

Description

@richardliebmann

Hi,

if I execute clearStack directly in a store method after reverting / initializing a part of the store, the clearStack is executed before the store is modified and the undo/redo stack gets the change as initial value.

export const EditorStore = signalStore(
    { providedIn: 'root' },
    ...
    withUndoRedo({
        maxStackSize: 100,
        keys: ['selectedFile']
    }),
    withMethods((store) => {
      ...
      selectFile(file: ProjectFile) {
          patchState(store, {
              selectedFile: selected,
          });
    
          store.clearStack();
      },
      ...
});

The problem may be that clearStack is executed synchronously, whereas the undo/redo uses an effect that is executed asynchronously.

https://github.com/angular-architects/ngrx-toolkit/blob/399903d198cd8be3ff3ad0df4de5843de855167f/libs/ngrx-toolkit/src/lib/with-undo-redo.ts#L139C9-L139C15

The solution might be to use watchState instead of effect, which is executed synchronously.

Am I right? Should I create a pull request with this fix?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions