Skip to content

Commit

Permalink
fix: Add polyfill for requestIdleCallback for webkit based browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Jul 21, 2024
1 parent 7072aba commit 1a13e28
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions sandbox/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ var game = new ex.Engine({
filtering: ex.ImageFiltering.Pixel,
multiSampleAntialiasing: true
},
garbageCollection: true,
uvPadding: 0,
physics: {
colliders: {
Expand Down
24 changes: 24 additions & 0 deletions src/engine/Polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,28 @@ export function polyfill() {
if (typeof window !== 'undefined' && !(<any>window).devicePixelRatio) {
(<any>window).devicePixelRatio = window.devicePixelRatio || 1;
}

/* istanbul ignore next */
if (typeof window !== 'undefined' && !(window as any).requestIdleCallback) {
// Adapted from https://developer.chrome.com/blog/using-requestidlecallback#checking_for_requestidlecallback
(window as any).requestIdleCallback =
window.requestIdleCallback ||
function (cb: (args: any) => any) {
const start = Date.now();
return setTimeout(function () {
cb({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 50 - (Date.now() - start));
}
});
}, 1);
};

(window as any).cancelIdleCallback =
window.cancelIdleCallback ||
function (id: any) {
clearTimeout(id);
};
}
}

0 comments on commit 1a13e28

Please sign in to comment.