Pause, upon resume give last paused value

前端 未结 1 541
再見小時候
再見小時候 2020-12-31 16:15

I have a hot Observable fed by a socket. I can use the pausable to pause the socket feed. But once I \'unpause\' the observable, I need to display the last valu

相关标签:
1条回答
  • 2020-12-31 16:45

    You don't even need pausable to do this. (Note as well that you tagged RxJS5 but pausable only exists in RxJS 4). You simply need to convert your pauser into a higher order Observable:

    var source = Rx.Observable.fromEvent(document, 'mousemove')
      // Always preserves the last value sent from the source so that
      // new subscribers can receive it.
      .publishReplay(1);
    
    pauser
      // Close old streams (also called flatMapLatest)
      .switchMap(active => 
        // If the stream is active return the source
        // Otherwise return an empty Observable.
        Rx.Observable.if(() => active, source, Rx.Observable.empty())
      )
      .subscribe(/**/)
    
    //Make the stream go live
    source.connect();
    
    0 讨论(0)
提交回复
热议问题