Skip to content

@ngrx/signals: patchState blocks when State signal throws an error #5183

Description

@rainerhahnekamp

Which @ngrx/* package(s) are the source of the bug?

signals

Minimal reproduction of the bug/regression with instructions

Signals can throw an error when they are accessed. This can happen for state
slices that are backed by APIs like resource or linkedSignal.

patchState currently calls getState internally before applying an update, so
it eagerly reads every state slice. As a result, patching an unrelated state
slice can lead to a dead-end, in that sense that other state slices are not
patchable anymore.

Reproduction

const Store = signalStore(
  { providedIn: 'root', protectedState: false },
  withState({ name: '' }),
  withLinkedState(() => {
    const { value } = resource({
      loader: async () => {
        throw new Error('Failed to load value.');
      },
    });

    return { value };
  })
);

const store = inject(Store);

patchState(store, { name: 'John' });

Expected behavior

It should not block

Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)

NgRx 21

Other information

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions