Skip to content

withResources with fail resource is blocking the store. #242

@luandaja

Description

@luandaja

When a resource is in a failing state for any reason and you try to patchState, the console will display an error and the action will be skipped.

Image

This behavior can be reproduced with this store and calling any increment method.

If I'm doing something wrong, please let me know.

import { withResource } from '@angular-architects/ngrx-toolkit';
import { resource } from '@angular/core';
import { patchState, signalStore, withMethods, withState } from '@ngrx/signals';

export const Store = signalStore(
  {
    providedIn: 'root',
  },
  withState({
    count: 0,
  }),

  withResource(() => ({
    test: resource({
      loader: () => {
        console.log('test');
        // throw new Error('test');
        return Promise.reject('test');
      },
    }),
    // test2: httpResource(() => 'https://www.not-exist'),
  })),
  withMethods((store) => ({
    increment() {
      patchState(store, { count: store.count() + 1 });
    },
    increment2() {
      patchState(store, (state) => ({ ...state, count: state.count + 1 }));
    },
  }))
);

Checking the ngrx/platform patchState code, the issue is because when we call patchState, this call internally calls getState that gets the value.

Image

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