Pause RxJS stream based on a value in the stream

前端 未结 6 435
一生所求
一生所求 2021-01-03 23:56

I have a simple component with a single button that starts and pauses a stream of numbers generated by RxJS timer.

import { Component, OnInit } from \'@angul         


        
6条回答
  •  走了就别回头了
    2021-01-04 00:20

    As simple as it can get with one windowToggle and use active.next(false) working example: https://stackblitz.com/edit/angular-pdw7kw

     defer(() => {
          let count = 0;
          return stream$.pipe(
            windowToggle(on$, () => off$),
            exhaustMap(obs => obs),
            mergeMap(_ => {
              if ((++count) % 5 === 0) {
                this.active$.next(false)
                return never()
              }
              return of(count)
            }),
          )
        }).subscribe(console.log)
    

提交回复
热议问题