Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS Buffer, how to group multi click events as stream

Tags:

rxjs

In RxJS version 2.2.26

The following code produced a stream that would emit the number of clicks (double click, triple click etc..)

var multiClickStream = clickStream
    .buffer(function() { return clickStream.throttle(250); })
    .map(function(list) { return list.length; })
    .filter(function(x) { return x >= 2; });

In RxJS version 4.0.6

This code no longer produces the desired result.

How can I get that same functionality in RxJS 4.0.6?

Working 2.2.26 Example

Broken 4.0.6 Example

like image 783
Trevor Avatar asked May 18 '16 21:05

Trevor


3 Answers

There is actually a much better way to find double clicks via RxJs:

var mouseDowns = Rx.Observable.fromEvent(button, "mousedown");

var doubleClicks = mouseDowns.timeInterval()
                             .scan<number>((acc, val) => val.interval < 250 ? acc + 1 : 0, 0)
                             .filter(val => val == 1);

The benefit of this is that you don't need to wait for the full 250ms to recognize the double click - if the user has only 120ms between clicks there is a noticable delay between the double click and the resulting action.

Notice, that through counting up (with .scan()) and filtering to the first count we can limit our stream to just double-clicks and ignore every fast click after that - so click click click will not result in two double clicks.

like image 142
Wolfgang Avatar answered Oct 15 '22 23:10

Wolfgang


Just in case someone is wondering on how to do the same thing with RxJS 5 (5.0.0-beta.10), this is how I got it working:

const single$ = Rx.Observable.fromEvent(button, 'click');
single$
    .bufferWhen(() => single$.debounceTime(250))
    .map(list => list.length)
    .filter(length => length >= 2)
    .subscribe(totalClicks => {
        console.log(`multi clicks total: ${totalClicks}`);
    });

I spent a lot of time trying to figure this out as I'm also learning Reactive Programming (with RxJS) so, if you see a problem with this implementation or know of a better way to do the same thing, I'll be very glad to know!

like image 11
rafaelbiten Avatar answered Oct 15 '22 22:10

rafaelbiten


Detect single or double-clicks, but not multi-clicks

Here's a solution I came up with that creates a stream of single or double clicks (not anything more, so no triple clicks). The purpose is to detect multiple double clicks in quick succession, but also to receive notifications of single clicks.

let click$ = Observable.fromEvent(theElement, "click");

let trigger$ = click$.exhaustMap(r =>
    click$.merge(Observable.timer(250)).take(1));
let multiclick$ = click$.buffer(trigger$).map(r => r.length);

The reasoning is this, we use the buffer operator to group clicks, and design a buffer trigger event as follows: Every time a click happens, we start a race between two observables:

  1. A 250 msec timer
  2. The original click stream

Once this race concludes (we use take(1) because we are only interested in the first event) we emit the buffer trigger event. What does this do? It makes the buffering stop either when a 2nd click arrives, or when 250 msec have elapsed.

We use the exhaustMap operator to suppress the creation of another race while one is already going on. This would otherwise occur for the 2nd click in a double-click pair.

like image 6
Aviad P. Avatar answered Oct 15 '22 23:10

Aviad P.