Search code examples
rxjsngrx

how to filter out the initial value but all the subsequent ones?


This notificationsCount$ is being called by several components. That's why I make one initial call the first time a component want to read the data.

this.initialLoad = false;    

readonly notificationsCount$ = this.store.pipe(
  select(getNotificationsCount),
  tap(() => {
    if(!this.initialLoad){
       this.initialLoad = true;
       this.loadNotifications();
    }
  }),
  skip(1)
);

without filtering, the selector is returning 0, the initial value, and 3, the number of notifications after the call was made to backend. I want the selector to emit every value but the initial one, i.e. 0.

skip(0) still returns both values, i.e. 0 and 3. skip(1) returns 0. skip(2) doesn't return anything.

Thanks for helping


Solution

  • There is index argument you can mergeMap/switchMap to count the emission and depend on the index you can swallow the emission with never()

    this.store.pipe(mergeMap((value,index)=>{
    if(index<1) return never()
    // do you stuff 
    }))