我有一个热Observable
由套接字供电。我可以使用pausable暂停套接字馈送。但是,一旦我“取消暂停”可观察对象,我需要显示在订阅暂停时套接字可能发送的最后一个值。我不想跟踪套接字手动发送的最后一个值。这怎么可能暂停?
从文档中的示例,请参阅下面的注释:
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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)