-
Notifications
You must be signed in to change notification settings - Fork 195
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
Improving offset and adding resize listener #69
Conversation
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
👍 |
Awesome! I was looking for improvements, and the function from #63 would be great to actually replace my original debounce implementation. The problem I have is that on mobile the scroll slows down and until it stops, the debounce is not triggered, and that makes things to seem to take forever. Later-edit: Actually, nevermind, debouce is great on desktop and the throttle from #63 on mobile ^.^ |
@falcon1kr, I have forked you to use your version on our production build, and I have instantly noticed that while you have my debounce fix regarding findDomNode on unMounted components, the debounce function that you moved it in addEventListener is missing it's most important line:
after
I see that |
visibility-sensor.js
Outdated
context.check.apply(context, args); | ||
}; | ||
clearTimeout(timeout); | ||
timeout = setTimeout(later, delay || 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this.lastTimeout = timeout;
back under timeout = setTimeout(later, delay || 0);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eek, clearTimeout(this.lastTimeout);
was left behind as a mistake. From my understanding, this.lastTimeout
was added to track the timeout object so we can cleanup on unmount. But, with the addition of the resize listener, I needed to keep track of multiple timeouts separately to clean up. So I moved it into the "debounce info" object that gets created when addEventListenerWithDebounce
is called (timeout
property). However, looking back, I realized I made a mistake of statically referring to the timeout object when creating the debounce info object ... I'll fix that to replace it with a function so we can use closure to access the "last" timeout object that's created
e675ffe
to
b3976f4
Compare
…react-visibility-sensor into ChristianDavis-feature/throttle
|
Thanks @falcon1kr this is looking good :) Before we merge, is the change to |
Here's how I'm thinking about addressing backwards-compatibility: re-add some of the removed code deleted in this PR, but with a deprecation warning. We can even inform the user exactly how they should upgrade:
If nobody has objections I'll merge this PR in the next few days, along with the above modification (and some updates to proptypes and tests). |
published to v3.8.0 thanks everyone for your help, and your patience getting this big change through :) |
improved offset props to apply with other use cases, including partialVisibility
Also added optional window resize event listener enabled with resizeCheck props which can be used with resizeDelay