Skip to content

Shouldn't the code assume the observer could be called with more than one entry?Β #900

@jedwards1211

Description

@jedwards1211

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions