RxJS - pause, after resuming gives the last paused value

I have hot observed power from the socket. I can use pausable to pause the socket. But as soon as I “canceled” the observable, I need to display the last values ​​that the socket could send when the subscription was suspended. I don't want to keep track of the latest values ​​that the socket sends manually ... How could this be believable?

In the example documentation below, see the comments below:

var pauser = new Rx.Subject();
var source = Rx.Observable.fromEvent(document, 'mousemove').pausable(pauser);

var subscription = source.subscribe(
    function (x) {
        //somehow after pauser.onNext(true)...push the last socket value sent while this was paused...
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// To begin the flow
pauser.onNext(true); 

// To pause the flow at any point
pauser.onNext(false);  
+4
source share
1 answer

pausable. ( , RxJS5, pausable RxJS 4). pauser 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();
+3

Source: https://habr.com/ru/post/1663406/


All Articles