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

Implement throttle, move findDOMNode call #63

Merged
merged 3 commits into from
Apr 5, 2017

Conversation

ChristianDavis
Copy link
Contributor

Throttle is similar to a debounce but it limits a function to be called
a max number of times per time limit, instead of just delaying the
call. Also moved the findDOMNode call to didMount, since not necessary
to call every time Check is called

Throttle is similar to a debounce but it limits a function to be called
a max number of times per time limit, instead of just delaying the
call. Also moved the findDOMNode call to didMount, since not necessary
to call every time Check is called
@ChristianDavis
Copy link
Contributor Author

I think a throttle function is probably the expected behavior for a scroll function vs using debounce. This throttle calls the function on the tail end of the timeout. I’ll document if you agree with the implementation

@joshwnj
Copy link
Owner

joshwnj commented Jan 18, 2017

Thanks @ChristianDavis ! Will try this out soon when I have an opportunity.

@@ -22,10 +22,6 @@
],
"author": "joshwnj",
"license": "MIT",
"peerDependencies": {
Copy link
Owner

Choose a reason for hiding this comment

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

I need to learn more about peerDependencies :) Can you elaborate on the reason for moving it?

Perhaps @kof could chime in too, as he initiated it here: a68451e

Copy link
Contributor Author

@ChristianDavis ChristianDavis Jan 18, 2017

Choose a reason for hiding this comment

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

Forgot I had removed that. It doesn't really need to be removed, but those packages are a devDependency if the example is to be built. peerDependencies was changed in NPM 3, doesn't install the package just issues warnings. The peer part can remain as a duplicate

if (!wait) {
wait = true;
setTimeout(function () {
callback.call();
Copy link
Owner

Choose a reason for hiding this comment

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

Out of curiosity is there any reason for callback.call() instead of just callback() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I grabbed someone else's implementation, and there are no arguments here. Should just be callback()

@eek
Copy link
Collaborator

eek commented Jan 18, 2017

I initially implemented throttle myself before doing this pull request: -> #54 (comment)

Unfortunately it didn't work well in my case (that's why I preferred debounce), triggering at the beginning but not also at the end if the time is lower than the time set in the throttle. Unexpected behaviour may arise.

I see both are available now, so that's ok :D

@ChristianDavis
Copy link
Contributor Author

ChristianDavis commented Jan 18, 2017

@eek Yeah, controlling scroll is tough. Lots of specific tweaks required. I think using the long interval with an active check is a great approach. But with debounce, you can scroll right past an event without ever triggering it. Throttle was required for my use, what did you mean by

triggering at the beginning but not also at the end if the time is lower than the time set in the throttle

@eek
Copy link
Collaborator

eek commented Feb 24, 2017

Hey!

Sorry for the late-reply, I haven't tested your code but look a bit over it, it seems more like a combination between debounce and throttle, which is really great, I also tested a throttle function, but throttle usually triggers every x milliseconds starting from the beginning, so if it was 1s delay, and I would scroll for 0.9s, it would have triggered just the first time.

Your function is great because it also triggers afterwards, I would actually propose to replace debounce with this. Hah

@joshwnj
Copy link
Owner

joshwnj commented Mar 1, 2017

Before we merge this, can someone try to reproduce #66? It might be a simple matter of adding clearTimeout to the component unmount handler, but it would be good to see if we can reproduce the same error first.

@eek
Copy link
Collaborator

eek commented Mar 8, 2017

Fixed #66 in #68

Throttle might have the same issue.

@joshwnj joshwnj merged commit e00d585 into joshwnj:master Apr 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants