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

Polyfill: callback is called synchronously #225

Closed
Jessidhia opened this issue Jun 30, 2017 · 3 comments
Closed

Polyfill: callback is called synchronously #225

Jessidhia opened this issue Jun 30, 2017 · 3 comments
Labels

Comments

@Jessidhia
Copy link

Jessidhia commented Jun 30, 2017

The intersection observer's callback should be called asynchronously according to spec.

https://wicg.github.io/IntersectionObserver/#update-intersection-observations-algo step 2.12

https://wicg.github.io/IntersectionObserver/#queue-an-intersectionobserverentry step 3

https://wicg.github.io/IntersectionObserver/#queue-intersection-observer-task step 3

The this._callback invocation should be done in a requestIdleCallback or setTimeout call.

@josephliccini
Copy link

I am getting an issue where both in IE and FireFox, when the polyfill is added, it quickly freezes up the page and the browser goes into "not responding". Would this synchronous callback create this behavior?

@Rendez
Copy link

Rendez commented Aug 10, 2017

I haven't been able to test extensively so I didn't experience a freeze up of the page in my case with several IO targets being observed. Whenever I am using the polyfill I make sure I am wrapping my calls with requestIdleCallback, which indeed seems to be the best way to prevent potential "jank".

So to wrap your individual invocations, here's a snippet you can use for the time-being:

import { requestIdleCallback } from 'request-idle-callback'; // optional polyfill

const hasNativeSupport = 'IntersectionObserver' in window && window.IntersectionObserver.prototype.toString().contains('[object IntersectionObserver');
const ensureAsync = (callback) => hasNativeSupport ? callback : (...args) => requestIdleCallback(() => { callback(...args); });

export default ensureAsync;

And here's a couple of Profile snapshots taken with Firefox's devtools - bare in mind it's me scrolling in the same page up and down without a given order, so it's far from accurate to measure - I did however obtain the best result 2-3 times in a row with requestIdleCallback:

Without RIC

without ric

min 10fps

With IRC

with ric

min 20fps

@miketaylr
Copy link
Member

Given Firefox's native implementation, I'm going to optimistically close this issue. mlisook/plastic-image#19 (comment) is also positive to see. If someone is still experiencing an issue, please comment and we can re-open.

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

No branches or pull requests

4 participants