Skip to content

Commit d300ceb

Browse files
authored
[DevTools] only polyfill requestAnimationFrame when necessary (#24651)
1 parent d2c9e83 commit d300ceb

File tree

1 file changed

+18
-11
lines changed
  • packages/react-devtools-extensions/src

1 file changed

+18
-11
lines changed

packages/react-devtools-extensions/src/main.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,25 @@ const isEdge = getBrowserName() === 'Edge';
3333
// since Chromium v102, requestAnimationFrame no longer fires in devtools_page (i.e. this file)
3434
// mock requestAnimationFrame with setTimeout as a temporary workaround
3535
// https://github.com/facebook/react/issues/24626
36-
// The polyfill is based on https://gist.github.com/jalbam/5fe05443270fa6d8136238ec72accbc0
3736
if (isChrome || isEdge) {
38-
const FRAME_TIME = 16;
39-
let lastTime = 0;
40-
window.requestAnimationFrame = function(callback, element) {
41-
const now = window.performance.now();
42-
const nextTime = Math.max(lastTime + FRAME_TIME, now);
43-
return setTimeout(function() {
44-
callback((lastTime = nextTime));
45-
}, nextTime - now);
46-
};
47-
window.cancelAnimationFrame = clearTimeout;
37+
const timeoutID = setTimeout(() => {
38+
// if requestAnimationFrame is not working, polyfill it
39+
// The polyfill is based on https://gist.github.com/jalbam/5fe05443270fa6d8136238ec72accbc0
40+
const FRAME_TIME = 16;
41+
let lastTime = 0;
42+
window.requestAnimationFrame = function(callback, element) {
43+
const now = window.performance.now();
44+
const nextTime = Math.max(lastTime + FRAME_TIME, now);
45+
return setTimeout(function() {
46+
callback((lastTime = nextTime));
47+
}, nextTime - now);
48+
};
49+
window.cancelAnimationFrame = clearTimeout;
50+
}, 400);
51+
52+
requestAnimationFrame(() => {
53+
clearTimeout(timeoutID);
54+
});
4855
}
4956

5057
let panelCreated = false;

0 commit comments

Comments
 (0)