如何创建带rxjs 5的pausableBuffer
我正在努力使我认为是一个 我让别人分享了他们的代码,但我不知道如何将其转换为自定义操作(没有typescript/只有ES6)如何创建带rxjs 5的pausableBuffer,rxjs,rxjs5,Rxjs,Rxjs5,我正在努力使我认为是一个 我让别人分享了他们的代码,但我不知道如何将其转换为自定义操作(没有typescript/只有ES6) const attach = Rx.Observable.timer(0 * 1000, 8 * 1000).mapTo('@'); const detach = Rx.Observable.timer(4 * 1000, 8 * 1000).mapTo('#'); const input = Rx.Observable.interval(1* 1000); cons
const attach = Rx.Observable.timer(0 * 1000, 8 * 1000).mapTo('@');
const detach = Rx.Observable.timer(4 * 1000, 8 * 1000).mapTo('#');
const input = Rx.Observable.interval(1* 1000);
const pauser = attach.mapTo(true).merge(detach.mapTo(false));
input
.publish(_input => _input
.combineLatest(pauser, (v, b) => b)
.filter(e => e)
.publish(_switch => _input.bufferWhen(() => _switch.take(1)))
)
.flatMap(e => Rx.Observable.from(e))
.concatMap(e => Rx.Observable.empty().delay(150).startWith(e))
有人能帮我创建它吗?这样我就可以只做
input.pausableBuffer(pauser)
(也许还可以定义一个startsWith)。你可以像这样将它添加到原型中:
var pausableBuffer = function(pauser) {
return this.publish(_input => _input
.combineLatest(pauser, (v, b) => b)
.filter(e => e)
.publish(_switch => _input.bufferWhen(() => _switch.take(1)))
)
.flatMap(e => Rx.Observable.from(e));
}
Rx.Observable.prototype.pausableBuffer = pausableBuffer;
需要记住的一点是,这将在暂停状态下启动。要改为在活动状态下启动,请将.startWith(true)
添加到暂停器
var pausableBuffer = function(pauser) {
return this.publish(_input => _input
.combineLatest(pauser.startWith(true), (v, b) => b)
.filter(e => e)
.publish(_switch => _input.bufferWhen(() => _switch.take(1)))
)
.flatMap(e => Rx.Observable.from(e));
}
Rx.Observable.prototype.pausableBuffer = pausableBuffer;
2019年更新:RxJs 6风格:
var pausableBuffer = function(pauser) {
return (source) => source.pipe(publish(_input =>
combineLatest(_input, pauser.pipe(startWith(true))).pipe(
map(([inp, pa]) => pa),
filter(pa => pa),
publish(_switch => _input.pipe(bufferWhen(() => _switch.pipe(take(1)))))
)),
mergeMap(e => from(e))
);
}
这个例子在RxJS 6中看起来像什么?@chrismarx更简单实际上,你添加了管道,而不是将函数添加到原型中,而是将其作为一个返回函数的内部函数。内部函数的参数将替换这个例子中现在的
这个
。pausableBuffer=(pauser)=>(源代码)=>source.publish(\u input=>\u input.pipe(…)
感谢您提供了一个示例。我在使用此答案时遇到了问题,因为this.publish不再有效,只是切换到了“publish”也不起作用。我最后使用了这里的缓冲示例,它使用了bufferToggle和windowToggle-@chrismarx仍然是一个有效的运算符。我只是意识到我忘记了管道它。@chrismarx我在答案中添加了完整的RxJs 6 soluion+JsBin演示。看到这个类似的问题了吗-