Search code examples
rxjsstoreeffectsngrx

Does Observable.race for two observables work more than one time?


Tasks is: I have two effects (ngrx/store), let's say ef1$ and ef2$, which are Observables. In angular component I would like to detect which one is triggered. I added the following code:

 Observable.race(ef1$, ef2$)
     .subscribe((payload) => {
          if (payload.type == ThreadActions.THREAD_READY_TO_BE_RENDERED) {
            // do one thing
          }
          else if (payload.type == ThreadActions.THREAD_ITEM_READY_TO_BE_RENDERED) {
            // do another thing
          }
        });

But it looks like that after first emit of any effects Observable.race is going to be dead and no reaction more on additional emits, despite of I sure that effect executes again and again.


Solution

  • It's hard to tell what is you desired functionality but if you want to use the race operator multiple times you'll need to complete the chain when it emits a resubscribe immediately:

    Observable.defer(() => Observable.race(ef1$, ef2$))
      .take(1)
      .repeat()
      .subscribe(...);