ngrx / platform

Reactive State for Angular
https://ngrx.io
Other
8.05k stars 1.98k forks source link

Can't access private method from within SignalStore #4567

Closed hafnerpw closed 1 month ago

hafnerpw commented 1 month ago

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

signals

Minimal reproduction of the bug/regression with instructions

From other methods and hooks inside the signal-store I want to access private methods of it. The Documentation is not providing an example for it https://ngrx.io/guide/signals/signal-store/private-store-members So I tried it like this:

export const ComponentMetaInfoStore = signalStore(
  withState<ComponentMetaInfoState>(initialState),
  withMethods(
    (
      store,
      componentsService = inject(ComponentControllerService)
    ) => ({
      _addComponent: (component: ComponentDtoV2) => {
        if (store.components().find((c) => c.id === component.id)) {
          return;
        }
        patchState(store, { components: [...store.components(), component] });
      },
      addComponent: rxMethod<CreateComponentDtoV2>(
        pipe(
          switchMap((component) => componentsService.create({ body: component })),
          tapResponse({
            next: (component) => {
              _addComponent(component);
            },
            error: (error: HttpErrorResponse) => {
              patchState(store, { error: error.message });
            }
          })
        )
      )
    })
  ),
  withHooks((store, eventDispatcher = inject(EventDispatcherService)) => {
    return {
      onInit() {
        eventDispatcher
          .on<ComponentDtoV2>(BaseEventType.CREATE)
          .subscribe((component) => {
            _addComponent(component);
          });
      }
    };
  })
);

other access with this._addComponent or store._addComponent is not possible either.

Expected behavior

private signal store methods should be accessible from within the store. Also the documentation should show an example on how to use it.

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

@ngrx/signals: 18.1.0, @ngrx/operators: 18.1.0, typescript: 5.4.5, angular: 18.2.3

Other information

No response

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