diff --git a/packages/messaging/src/index.ts b/packages/messaging/src/index.ts index f9588692e..03175f5f8 100644 --- a/packages/messaging/src/index.ts +++ b/packages/messaging/src/index.ts @@ -494,7 +494,14 @@ namespace MessageLoop { */ const schedule = (() => { let ok = typeof requestAnimationFrame === 'function'; - return ok ? requestAnimationFrame : setImmediate; + let prev = 0; + return ok ? requestAnimationFrame : (callback: FrameRequestCallback) => { + const curr = new Date().getTime(); + const next = Math.max(0, 16 - (curr - prev)); + const handle = window.setTimeout(() => callback(curr + next), next); + prev = curr + next; + return handle; + }; })(); /** @@ -502,7 +509,7 @@ namespace MessageLoop { */ const unschedule = (() => { let ok = typeof cancelAnimationFrame === 'function'; - return ok ? cancelAnimationFrame : clearImmediate; + return ok ? cancelAnimationFrame : (handle: number) => clearTimeout(handle); })(); /** diff --git a/packages/messaging/tests/src/index.spec.ts b/packages/messaging/tests/src/index.spec.ts index 3bab33fc4..19fea0ad5 100644 --- a/packages/messaging/tests/src/index.spec.ts +++ b/packages/messaging/tests/src/index.spec.ts @@ -64,7 +64,14 @@ class LogHook implements IMessageHook { const defer = (() => { let ok = typeof requestAnimationFrame === 'function'; - return ok ? requestAnimationFrame : setImmediate; + let prev = 0; + return ok ? requestAnimationFrame : (callback: FrameRequestCallback) => { + const curr = new Date().getTime(); + const next = Math.max(0, 16 - (curr - prev)); + const handle = window.setTimeout(() => callback(curr + next), next); + prev = curr + next; + return handle; + }; })();