Polyfill: callback is called synchronously
Jessidhia opened this issue · 3 comments
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.
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?
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
min 10fps
With IRC
min 20fps
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.