Skip to content
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

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

Open
kevinfarrugia opened this issue Jul 16, 2021 · 4 comments

Comments

@kevinfarrugia
Copy link

kevinfarrugia commented Jul 16, 2021

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.

@kevinfarrugia kevinfarrugia changed the title Will IntersectionObserver always include all entries in a single callback? Will IntersectionObserver always include all entries in the first callback? Jul 16, 2021
@miketaylr
Copy link
Member

@kevinfarrugia what do browsers do today?

@kevinfarrugia
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants