Skip to content

Instantly share code, notes, and snippets.

@steida
Created October 5, 2019 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steida/7ce3a9d965647bf807b7d2aa0ea88d9e to your computer and use it in GitHub Desktop.
Save steida/7ce3a9d965647bf807b7d2aa0ea88d9e to your computer and use it in GitHub Desktop.
Minimal modern setImmediate polyfill
// Draft uses YuzuJS/setImmediate polyfill, which can be reduced to this code.
// But it seems request requestAnimationFrame is good enough.
// TODO: Will it work with queue fix?
// // https://github.com/google/closure-library/blob/master/closure/goog/async/nexttick.js#L209
const setImmediate = (() => {
const channel = new MessageChannel();
let head: any = {};
let tail = head;
channel.port1.onmessage = () => {
if (head.next !== undefined) {
head = head.next;
const { callback } = head;
head.callback = null;
callback();
}
};
return (callback: () => void) => {
tail.next = { callback };
tail = tail.next;
channel.port2.postMessage(0);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment