Home > database >  Why Rxjs combineLatest depends on both subscription and not call when one of them changed?
Why Rxjs combineLatest depends on both subscription and not call when one of them changed?

Time:08-21

I have two subscriptions item as you see in the image: enter image description here

one of them is search input another one is a selection filter. I want to change the result when one or both of them change. I use Rxjs combineLatest so when both of them or search input change, everything is ok, but when changing the Partner type at the first, the response does not change.

ngOnInit() {
super.ngOnInit();
this.getAllPartners();

combineLatest([this.searchPartnerFc.valueChanges, this.filterFc.valueChanges])
  .pipe(
    switchMap(([search, filter]) => {
      let partnersBySearch = this.getSearchResult(search);
      let partnersByFilter = this.getFilterResult(filter);

      this.partners = partnersBySearch.filter(item => partnersByFilter.indexOf(item) > 0);
      return this.partners;
    }),
  )
  .subscribe();
}

getFilterResult(filterKey: string) {
    if (filterKey == 'All') return this.allPartners;
    else return this.allPartners.filter(partner => partner.partnerType == filterKey);
}

getSearchResult(searchString: string) {
    return this.allPartners.filter(x => x.partnerName.toLowerCase().includes(searchString));
}

CodePudding user response:

I think it's not working because of the nature of the combineLatest operator. It will only emit when both of the observables have emitted an initial value first. So when only the first observable emits, and the second doesn't, it will not do anything: https://www.learnrxjs.io/learn-rxjs/operators/combination/combinelatest

I think you need a merge operator here: https://www.learnrxjs.io/learn-rxjs/operators/combination/merge

Also please take a look here on this thread: How to 'wait' for two observables in RxJS

CodePudding user response:

You can achieve your desired result by providing a default emission for each of your source observables using startWith:

combineLatest([
  this.searchPartnerFc.valueChanges.pipe(startWith('')), 
  this.filterFc.valueChanges.pipe(startWith('All'))
]).pipe(
  ...
);
  • Related