Get all current (active) subscriptions

后端 未结 7 1098
清歌不尽
清歌不尽 2021-02-12 20:58

Is it possible to get all the \"active\" subscriptions without storing them manually?

I\'d like to unsubscribe all of the \"active\" subscriptions and don\'

7条回答
  •  爱一瞬间的悲伤
    2021-02-12 21:48

    One of the options is to use takeUntil in combination with Subject to stop all (Observable) subscriptions.

    terminator$: Subject = new Subject();
    
    Observable.timer(1000)
      .do(i => console.log(`timer 1: ${i}`))
      .takeUntil(terminator$)
      .subscribe();
    
    Observable.timer(5000)
      .do(i => console.log(`timer 2: ${i}`))
      .takeUntil(terminator$)
      .subscribe();
    
    const stopSubscriptions = () => terminator$.next(true);
    

提交回复
热议问题