w3c / IntersectionObserver

Intersection Observer
https://www.w3.org/TR/intersection-observer/
Other
3.62k stars 522 forks source link

Will `IntersectionObserver` always include all entries in the first callback? #476

Open kevinfarrugia opened 3 years ago

kevinfarrugia commented 3 years ago

Suppose that we have created an IntersectionObserver and we give the observer a large array of elements to observe by iterating through the array and observing each element.

for (let target of list) {
  observer.observe(target);
}

Is it guaranteed that when the callback is executed the first time it will include all entries that are being observed in a single callback (as opposed to queuing multiple callbacks with a subset of the original array)?

To clarify, if the above holds true, then in the below example, the line console.log(entries.length, target.length); will never be reached with the first callback - but will be reached with subsequent callbacks.

let list = document.querySelectorAll('li');

const observer = new IntersectionObserver((entries, observer) => { 
  if (entries.length !== list.length) {
   // this statement will never be reached with the first callback
    console.log(entries.length, list.length);
  }
});

for (let target of list) {
  observer.observe(target);
}

Please excuse me if this is not the appropriate place to ask and I will close the issue.

miketaylr commented 2 years ago

@kevinfarrugia what do browsers do today?

kevinfarrugia commented 2 years ago

Running a quick test on the latest Chrome, Firefox and Safari it always includes all the entries.

Test URL with 100,000 entries: https://cdpn.io/pen/debug/ZErmQEw?authentication_hash=VGAWNooOepzr

miketaylr commented 2 years ago

Thanks for the test and results! The link 403s now, mind sticking the code into a gist or something? Might be useful to turn into a WPT down the road.

Given that this behavior is interoperable, if the spec doesn't currently require it, it sounds reasonable to do so (I'm still getting familiar w/ the spec, but I will try to find where that should be in the coming days(weeks/months/years??).

kevinfarrugia commented 2 years ago

Here's a Gist. Let me know if I could help any further.