-
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
Use 'scroll' and 'resize' DOM events instead of a timer #10
Comments
What I'd like here (but haven't gotten around to yet) is to use both. So we'd have scroll and resize listeners as the main trigger, but also have a timer for the cases where visibility changes without either a scroll or a resize. With this implemented we'd treat the timer as a fallback and use a much longer delay, making the CPU impact negligible (or, at least: within the control of the developer). Fancy having a go at that? PRs welcome :) |
Absolutely, to use both a timer and events would be great. And in this case, perhaps the default 1000 ms delay is fine. Unfortunately, I'm too busy now to help you with the implementation. I hope someone can do it! :) |
I've put a call out on twitter in case anyone would like to volunteer :) otherwise I'll get to it when I next have time |
I can take a look at it, if it's still something you guys want to add :) |
Or we remove both timer and don't add events. It can be easily done in the user space. I use active=false and do the checks manually in my code. Only user code can really know what it needs to react to and how often. |
@Ghostavio: 👍 :) @kof: If you remove everything, what would be the point of the module? :) |
I don't remove "everything", component remains as usefull as before, its a very small api change that makes api lower lever but with less bugs. |
you want periodical check? its easy setInterval(::this.refs.sensor.check, 1000) you want on scroll? window.addEventListener('scroll', ::this.refs.sensor.check) you want a scroll with debounce? (you should want it) import debounce from 'lodash/function/debounce' |
everything you might want to do you can do just in one line of code, its the same line you will use for a fragile option. |
Also I would think about how to get rid of the imperative .check() call. |
@kof: I think a sensor should be able to work by itself. Imagine if you had to call a check() method on a button to find out if it is clicked or not, it would be wrong don't you think? |
@mvila in the ideal world you would be right .... in practice I can see lots of issues to make it magically work. If a tool can't do the magic without side effects, it should not have this magic. |
@kof: Well, listening to scroll and resize events to trigger the visibility check doesn't look too magical to me. If we do that we can increase the timer to 1 sec or remove it completely. It should work in 95% of cases and for the remaining 5% the developer has always the possibility to trigger the checking manually. |
I know following problems:
|
I could think of a container component which contains the listeners/timers and communicates with the inner sensors. |
I like the idea of 2 separate components, @kof gives some good examples of why you might need that. What if we said that by default the 2 components come bundled together, but you can opt into separating them and manually associating the trigger with the sensor? |
Yes its possible. Component "Sensor" and component "Trigger" and a default component which is Sensor wrapped into Trigger. |
I agree with @mvila. The component would aim to be too generic removing all the setup, scroll and resize should be right for the majority of the cases. As suggested by @kof, for particular cases that differ from that, @joshwnj So it seems just right to change the timer by debounced scroll/resize. |
https://developers.google.com/web/updates/2016/04/intersectionobserver?hl=en That would be even more cool, ofc browser support sucks yet. |
Cool, I would rename this project to react-intersection-observer and start using IntersectionObserver if supported. |
Maybe (and that is strong maybe) I will have some time next week to give it a shot at implementing and testing this. |
Implemented this in #54 if you guys can have a look, it would be awesome 😁 |
Thanks @eek ! Will take a look |
Instead of using a timer it would be great (and CPU saver...) to use 'scroll' and 'resize' DOM events to check the visibility only when it's necessary. https://github.com/Pomax/react-component-visibility does that but it doesn't have the 'containment' feature.
The text was updated successfully, but these errors were encountered: