暂停,在恢复时给出最后暂停的值 [英] Pause, upon resume give last paused value
问题描述
我有一个由套接字提供的热 Observable
.我可以使用 pausable 暂停套接字馈送.但是,一旦我取消暂停"可观察对象,我就需要显示订阅暂停时套接字可能发送的最后一个值.我不想跟踪套接字手动发送的最后一个值.这怎么可能是可行的?
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 values that the socket could have sent while the subscription was paused. I don't want to keep track of the last values the socket sends manually. How could this be pausible?
从文档中的示例,请参阅下面的注释:
From the example in the documentation, see 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);
推荐答案
您甚至不需要 pausable
来执行此操作.(还要注意,您标记了 RxJS5,但 pausable
仅存在于 RxJS 4
中).您只需要将 pauser
转换为更高阶的 Observable
:
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();
这篇关于暂停,在恢复时给出最后暂停的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!