gpt4 book ai didi

rxjs - 暂停,恢复后给出最后的暂停值

转载 作者:行者123 更新时间:2023-12-04 19:34:52 35 4
gpt4 key购买 nike

我有一个由套接字提供的热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();

关于rxjs - 暂停,恢复后给出最后的暂停值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41077300/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com