Skip to content

Conversation

@bman654
Copy link
Contributor

@bman654 bman654 commented Mar 7, 2016

fixes #2089

bman654 added 3 commits March 7, 2016 12:30
Now only uses 1 timer to handle both wait and maxWait.
Code refactored to reduce the number of edge cases that require special
logic
fix issues with leading: false not always being honored
The test was testing that throttle(func, wait, {leading: false}) would return 2
results in a shorter time frame than the `wait`.

With a wait=128,

T:0 throttled func called
T:128 func should be called (trailing edge)
T:192 assertion tests func was called
T:192 throttled func called
T:254 assertion test func should not be called yet
T:288 erroneous assertion func should be called
T:320 func should be called (trailing edge)
T:384 assertion tests func was called
throttled();
}, 192);

setTimeout(function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once I fixed the other bug, this test started failing. After reviewing the test, it seems like it had some bad assertions. Or maybe I am misunderstanding the way throttle works.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how an added assert changes the behavior of a failing test.
Can you explain a bit more what the test change does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the timings of the assertions. The commit message for this change has a bit more details: 43d71ef

@jdalton
Copy link
Member

jdalton commented Mar 7, 2016

@bman654 Thanks for this! Have you signed our CLA?

@bman654
Copy link
Contributor Author

bman654 commented Mar 7, 2016

yes (3/7/2016 15:07:59)

@jdalton
Copy link
Member

jdalton commented Mar 7, 2016

🚀 !

* jQuery(source).on('message', debounced);
*
* // Cancel the trailing debounced invocation.
* jQuery(window).on('popstate', debounced.cancel);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debounce code can be a bit complex and understanding the ins and outs of its async-ness can be challenging at times. Would you mind walking me through the changes step by step (baby step it)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Let me add an additional commit that adds some more detailed comments. I could also walk you through the code in a screenshare.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could also walk you through the code in a screenshare.

😍

jdalton added a commit that referenced this pull request Mar 7, 2016
@jdalton jdalton merged commit bfb3e65 into lodash:master Mar 7, 2016
@jdalton
Copy link
Member

jdalton commented Mar 7, 2016

Thanks for the screenshare!

@bman654
Copy link
Contributor Author

bman654 commented Mar 7, 2016

I studied #1952 use case and the OP was describing a sustained period of activity and the maxWait firings were not very accurate.

I took his code and applied it to this new version (replaced his click handlers with setInterval).

https://jsfiddle.net/7atuxyc3/

It looks like the new code is just as accurate. The throttle calls are occurring every 400ms +/- 8ms, which is all the accuracy you can hope for when using setTimeout.

@bman654 bman654 deleted the debounce-bug branch March 7, 2016 22:04
remainingWait,
shouldInvoke;

if (waitTime >= wait) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this condition for time zone switches?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That bit is a little lower (line 9078)

@lock
Copy link

lock bot commented Dec 27, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

debounce with maxWait starts emitting on "leading" edge after first time

3 participants