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

fix(messaging): Remove non standard functions (setImmediate, clearImm… #25

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/messaging/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,22 @@ 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;
};
})();

/**
* A function to unschedule an event loop callback.
*/
const unschedule = (() => {
let ok = typeof cancelAnimationFrame === 'function';
return ok ? cancelAnimationFrame : clearImmediate;
return ok ? cancelAnimationFrame : (handle: number) => clearTimeout(handle);
})();

/**
Expand Down
9 changes: 8 additions & 1 deletion packages/messaging/tests/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
})();


Expand Down