Search code examples
angularrxjsngrx

Angular, ngrx - problem with infinitive loop in selector


There is possible to dispatch action in selector in one store?

    this.store$.pipe(select(selectPersonByName, {personSelectorProps: this.id[]0}))
            .subscribe(history => {
                this.store$.dispatch(selectAssignWorkHistory({historyArray:  history}));
            });
    }

When I run that code, I have infinitive loop. Dispatch action refresh the store, and re-run selector and so on...


Solution

  • If this action meant to be fired once upon subscribing to the store selector, you may chain the above statement the RxJS take() operator. This operator will ensure that only the specified count values supplied to be take() operator will be emitted by the source observable.

    this.store$
      .pipe(
        select(selectPersonByName, {personSelectorProps: this.id[0]}),
        take(1),
      ).subscribe(history => {
        this.store$.dispatch(selectAssignWorkHistory({historyArray:  history}));
      });