Why switchMap does not cancel previous observer?

前端 未结 1 825
臣服心动
臣服心动 2021-01-14 22:52

I have method described in component (controller):

public requestPassportData(): void {
    const th = this;
    const data = {
      get_scanned_data: true
         


        
相关标签:
1条回答
  • 2021-01-14 23:24

    Because every time you call requestPassportData() you'll create a new chain with new Observable.timer that has nothing to do with the previous calls.

    There are obviously multiple ways to solve this but you can for example do the following:

    private requestPasswordSubject$ = new Subject();
    
    private requestPassword = this.requestPasswordSubject$
      .switchMap(() => Observable.timer(0, 1000)
        .switchMap(() => this.requestMethods.requestPassportData(data))
        .takeWhile(() => {
          return (
             th.formRegister.currentForm().index == 1 ||
             th.formRegister.currentForm().index == 2 ||
             th.formRegister.currentForm().index == 3
          );
        });
      )
      .subscribe(response => {});
    
    public requestPassportData(): void {
      ...
      this.requestPasswordSubject$.next();
    }
    

    Actual implementation depends on what exactly you're trying to achieve but hopefully you get the point what is different to what you have now.

    0 讨论(0)
提交回复
热议问题