javascript - SwitchMap not working as intended in Angular - Stack Overflow

I have a search service returning results, if the user submits twice, only the last search should retur

I have a search service returning results, if the user submits twice, only the last search should return results.

I have the following code in a service, view (updated elsewhere) has all the post body info needed for the request.

This method is called from elsewhere in the code using this.searchService.getResults()

getResults() {
    const view: SearchView = this.getCurrentSearch();
    if (!view.query || view.query === '') {
      return;
    }

    this.checkPermissions(view).subscribe(searchView => {
     //do some stuff
     this.cacheService
       .cachedPost(this.url, searchView, () => {
        return this.http.post(this.url, searchView);
      })
      .switchMap(res => this.pageTransform(res))
      .subscribe(
        // results here
      );
   });
}

`

From running tests I can see that if I fire 3 searches in quick succession, they all resolve, whereas I only want the last to.

I have a search service returning results, if the user submits twice, only the last search should return results.

I have the following code in a service, view (updated elsewhere) has all the post body info needed for the request.

This method is called from elsewhere in the code using this.searchService.getResults()

getResults() {
    const view: SearchView = this.getCurrentSearch();
    if (!view.query || view.query === '') {
      return;
    }

    this.checkPermissions(view).subscribe(searchView => {
     //do some stuff
     this.cacheService
       .cachedPost(this.url, searchView, () => {
        return this.http.post(this.url, searchView);
      })
      .switchMap(res => this.pageTransform(res))
      .subscribe(
        // results here
      );
   });
}

`

From running tests I can see that if I fire 3 searches in quick succession, they all resolve, whereas I only want the last to.

Share Improve this question asked Mar 28, 2018 at 15:56 Ben TaliadorosBen Taliadoros 9,58118 gold badges67 silver badges106 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

That's because you're calling the method getResults() multiple times which creates multiple Rx chains and all of them are processed. If you want switchMap to work correctly you need to keep a reference to only one chain and push values to it:

private search$ = new Subject();

private searchSubscription = search$
  .switchMap(view => this.checkPermissions(view)
    .concatMap(searchView => this.cacheService.cachedPost(this.url, searchView, () => {
      return this.http.post(this.url, searchView);
    })
    .concatMap(res => this.pageTransform(res))
  )
  .subscribe(...);

getResults() {
  const view: SearchView = this.getCurrentSearch();

  ...

  this.search$.next(view);
}

I didn't test this code for obvious reasons but I think you'll get the point. Also don't forget to unsubscribe in ngOnDestroy() with this.searchSubscription.unsubscribe().

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745393398a4625767.html

相关推荐

  • javascript - SwitchMap not working as intended in Angular - Stack Overflow

    I have a search service returning results, if the user submits twice, only the last search should retur

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信