RxjS shareReplay : how to reset its value?

后端 未结 4 548
醉梦人生
醉梦人生 2021-01-05 02:30

I use shareReplay to call only once (like a cache) a webservice to retrieve some informations :

In my service :

getProf         


        
4条回答
  •  被撕碎了的回忆
    2021-01-05 03:11

    The other answers are all fine, but rather than having to reset the actual shareReplayed observable, a simpler approach might just be caching the result like so:

    protected profile$: Observable;
    
    getProfile$(): Observable {
      if (!this.profile$) {
        this.profile$ = this.callWS().pipe(shareReplay(1));
      }
    
      return this.profile$;
    }
    
    resetProfile() {
      this.profile$ = null;
    }
    

    ref: https://blog.angularindepth.com/fastest-way-to-cache-for-lazy-developers-angular-with-rxjs-444a198ed6a6

提交回复
热议问题