Rxjs: How is `throttle` supposed to be used?

Created on 4 Nov 2015  路  10Comments  路  Source: ReactiveX/rxjs

Hello everyone!
I'm having a hard time to understand the logic behind the throttle operator. I assume I'm missing something.

With the 5.0.0-alpha.7 version of the library when I do:

var Rxjs = require('@reactivex/rxjs');
var obs = new Rxjs.Observable(function (subscriber) {
    function emit() { subscriber.next("foobar"); setTimeout(emit, 1000); }
    emit();
    return function () {}
});
obs.throttle(2000).subscribe(function (value) { console.log(value); });

The only result I get is:

foobar

While I was expecting something like

foobar
# 2s with nothing, then:
foobar
# etc...

I run that code by simply doing:

npm install @reactivex/[email protected]
node wut.js

Is it a bug or is it because the default scheduler need some triggering from my part ?
Best,

Nemikolh

question

Most helpful comment

@Nemikolh ... you're conflating throttle and debounce ... this is something I'm guilty of too #480.

Throttle lets the call through, and drops all calls for N seconds before allowing another call to get through.

Debounce will wait until an N second gap in calls, then allow the last call through.

All 10 comments

@Nemikolh ... you're conflating throttle and debounce ... this is something I'm guilty of too #480.

Throttle lets the call through, and drops all calls for N seconds before allowing another call to get through.

Debounce will wait until an N second gap in calls, then allow the last call through.

But if I understand you correctly, then If I wait long enough on my example I should see another foobar right ?
In my example, you see foobar only once even if you wait for 5minutes.
Ideas?

Note: If I remove the call to throttle, I do see all foobar being printed on my console.

But if I understand you correctly, then If I wait long enough on my example I should see another foobar right ?

No, throttleTime(2000) will drop the foobar that emits at 1000.

Throttle takes one, then drops all until the timer (of 2000) is up, then takes one and starts the timer again, dropping everything.

Sorry, I'm not sure to follow you. Here my concern is really not about "when", it is about "what".

Please have a look at my example again to consider the following:

In the console after an _"infinite"_ time you would see the same thing whether you run this:

// The fact that the time is 1, 100 or 2000 is not the problem I'm concern with right now
obs.throttle(25).subscribe(function (value) { console.log(value); });

or this:

// This code produce the same thing if we ignore the time
obs.delay(25).take(1).subscribe(function (value) { console.log(value); });

The fact that throttle(x) drop all subsequent subscriber.next calls make it really look like a delay(x).take(1) in the example I've given.

I think this is not intended behaviour because discussion #480 seems to imply that in my example, If I throttle(6000) then every 6000 I should see one foobar being printed on the console.
Basically, the discussion seems to lead toward that kind of patterns:

x-x-x-x-x-x-x-x-x // subscriber.next calls produced by the setTimeout
----x---x---x---x // subscribe callback called less frequently

But what we're seeing when running the code is this:

x-x-x-x-x-x-x-x-x // subscriber.next calls produced by the setTimeout
----x------------ // subscribe callback called just once

What do you think?

Note: Maybe in my example this wasn't super clear, but the emit function re-schedule itself through setTimeout. So it will call endlessly subscriber.next("foobar");

@Nemikolh it looks like #496 we still have some work to do around throttle.

In the end though the behavior should look like this:

var someObservable = cold('---x------x------x------x------x---|');
var someDuration = 100;  //  '----------|';
                                      //  '----------|';
var expected =            '---x------------x--------------x---|';

expectObservable(someObservable.throttleTime(someDuration)).toBe(expected);

... if that makes sense.

Is it okay to close this issue, since it's just a question? Or would you rather I keep it open for a while to see if someone else chimes in?

@blesh That make sense a lot! Thanks! :smiley:

I'm okay with closing the issue. Eventually you can refer to this missing behavior in #496.

Not a problem, if you need anything else, please don't hesitate to drop another issue, or hit me up on twitter.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

peterbakonyi05 picture peterbakonyi05  路  4Comments

chalin picture chalin  路  4Comments

LittleFox94 picture LittleFox94  路  3Comments

cartant picture cartant  路  3Comments

haf picture haf  路  3Comments