问题描述
我的用例如下:我得到事件,这些事件有时会突然发生.如果发生突发,我只需要处理一次.去抖就是这样做的.
然而,去抖动只给我一个突发的最后一个元素,但我需要知道突发中的所有元素才能聚合它们(使用平面图).
这可以通过定时窗口或缓冲区来完成,但是,这些是固定间隔,因此缓冲区/窗口超时可能发生在突发的中间,因此将突发分成 2 部分而不是 1 部分来处理.
所以我想要的是像
..事件:一个..->一个......事件:b.事件:c.事件:d..->乙、丙、丁.....事件:e..->电子.这可以通过 buffer 通过将去抖动流作为关闭选择器传入,例如:
var s = Rx.Observable.of('a').merge(Rx.Observable.of('b').delay(100)).merge(Rx.Observable.of('c').delay(150)).merge(Rx.Observable.of('d').delay(200)).merge(Rx.Observable.of('e').delay(300)).分享();s.buffer(s.debounce(75)).subscribe(x => console.log(x));
这是一个可运行的版本:https://jsbin.com/wusasi/edit?js,控制台,输出
My use case is as following: I get events, which sometimes happen in bursts. If a burst occurs, I only need to handle it once though. Debounce does this.
However, debounce only gives me the last element of a burst, but I need to know about all elements in a burst to aggregate on them (using flatmap).
This could be done by a timed window or buffer, however, these are fixed intervals, so a buffer/window timeout could occur in the middle of a burst, therefore splitting the burst in 2 parts to handle instead of 1.
So what I'd want is something like
.
.
event: a
.
. -> a
.
.
.
.
.
.event: b
.event: c
.event: d
.
.-> b,c,d
.
.
.
.
.event : e
.
. -> e
.
This can be achieved with buffer by passing a debounced stream in as a closing selector, e.g.:
var s = Rx.Observable.of('a')
.merge(Rx.Observable.of('b').delay(100))
.merge(Rx.Observable.of('c').delay(150))
.merge(Rx.Observable.of('d').delay(200))
.merge(Rx.Observable.of('e').delay(300))
.share()
;
s.buffer(s.debounce(75)).subscribe(x => console.log(x));
Here's a runnable version: https://jsbin.com/wusasi/edit?js,console,output
这篇关于RXJS:聚合去抖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!