Seba Kerckhof
Seba Kerckhof

Reputation: 1391

RXJS: Aggregated debounce

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
.

Upvotes: 25

Views: 5153

Answers (2)

Matt Burnell
Matt Burnell

Reputation: 2796

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

Upvotes: 26

kakigoori
kakigoori

Reputation: 1228

You're probably looking for bufferWithTimeOrCount

from the page:

/* Hitting the count buffer first */
var source = Rx.Observable.interval(100)
    .bufferWithTimeOrCount(500, 3)
    .take(3);

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x.toString());
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: 0,1,2
// => Next: 3,4,5
// => Next: 6,7,8
// => Completed

Upvotes: -1

Related Questions