暂停,恢复时给出最后暂停的值

Pause, upon resume give last paused value

我有一个热 Observable 插座供电。我可以使用 pausable 暂停套接字馈送。但是一旦我 'unpause' observable,我需要显示在订阅暂停时套接字可能发送的最后一个值。我不想跟踪套接字手动发送的最后一个值。这怎么可能?

从文档中的示例,请参阅以下评论:

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);  

您甚至不需要 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();