1

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));
}
heliya rb
  • 682
  • 8
  • 26
  • Please show what `this.allPartners` and `this.partners` is. It doesn't make any sense that you're using `switchMap`, returning something (that doesn't look like an observable), then doing nothing with the result. You say the response does not change, but there is no response, so what are you expecting to happen? If you are just trying to change `this.partners` just use `tap` not `switchMap`, and provide a default emission like BizzyBob said. – Chris Hamilton Aug 20 '22 at 19:45

2 Answers2

1

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(
  ...
);
BizzyBob
  • 12,309
  • 4
  • 27
  • 51
0

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

Tom
  • 68
  • 6
  • If I use Merge, I cannot identify which subscription has changed and call the corresponding function :( – heliya rb Aug 20 '22 at 12:01