Home > Enterprise >  take only the value from stream if the value changes
take only the value from stream if the value changes

Time:11-11

I have a stream with Rxjs to follow the loading status. I get mainly pending - pending - pending - success - success 0 - success But I want to take only pending - success case. If first value is pending and second value is pending too, don't take it.

dashboard.component.ts

export enum REQUEST_STATUS {
  pending = 'pending',
  error = 'error',
  success = 'success'
}

export class DashboardComponent implements OnInit {
    public loadingStatusInquiries$: Observable<REQUEST_STATUS>;

    constructor(private readonly store: Store) {
    this.loadingStatusInquiries$ = this.store.pipe(
      select(selectLoadingStatusAll),
      filter((status) => !!status),
         );
     }


}

Which operator should I use to implement above logic?

CodePudding user response:

Use distinctUntilChanged operator for that:

this.loadingStatusInquiries$ = this.store.pipe(
  select(selectLoadingStatusAll),
  filter((status) => !!status),
  distinctUntilChanged(),
);
  • Related