Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How is throttle supposed to be used? #649

Closed
Nemikolh opened this issue Nov 4, 2015 · 10 comments
Closed

How is throttle supposed to be used? #649

Nemikolh opened this issue Nov 4, 2015 · 10 comments

Comments

@Nemikolh
Copy link

Nemikolh commented Nov 4, 2015

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

@benlesh
Copy link
Member

benlesh commented Nov 4, 2015

@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.

@Nemikolh
Copy link
Author

Nemikolh commented Nov 4, 2015

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?

@Nemikolh
Copy link
Author

Nemikolh commented Nov 4, 2015

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

@benlesh
Copy link
Member

benlesh commented Nov 4, 2015

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.

@Nemikolh
Copy link
Author

Nemikolh commented Nov 4, 2015

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?

@Nemikolh
Copy link
Author

Nemikolh commented Nov 4, 2015

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");

@benlesh
Copy link
Member

benlesh commented Nov 5, 2015

@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.

@benlesh
Copy link
Member

benlesh commented Nov 5, 2015

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?

@Nemikolh
Copy link
Author

Nemikolh commented Nov 5, 2015

@Blesh That make sense a lot! Thanks! 😃

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

@Nemikolh Nemikolh closed this as completed Nov 5, 2015
@benlesh
Copy link
Member

benlesh commented Nov 5, 2015

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

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants