-
-
Notifications
You must be signed in to change notification settings - Fork 40
Description
I had issues with this lib being flaky in the past (sometimes failing to ever mark an element in view) and stopped using it, but I wanted to try it again so I gave the code a deeper look.
I noticed the observer code has
observerRef.current = new IntersectionObserver(
([entry]: IntersectionObserverEntryV2[]) => {Do you know for sure that it's safe to assume there will only ever be one entry? I'm still learning the Observer APIs but I haven't come across any evidence we can assume there will only ever be one entry.
According to the MDN docs on Intersection Observer API:
The list of entries received by the callback includes one IntersectionObserverEntry object for each threshold-crossing event β multiple entries can be received at a time, either from multiple targets or from a single target crossing multiple thresholds in a short amount of time. The entries are dispatched using a queue, so they should be ordered by the time they were generated, but you should preferably use IntersectionObserverEntry.time to correctly order them.
I wouldn't be surprised at all if this is why I was seeing flaky behavior in the past. The view I was using it in does a lot of canvas rendering that places significant load on the CPU so I'm guessing that entries were getting batched up and react-cool-inview was failing to examine the latest one.