Created
October 5, 2019 00:15
-
-
Save steida/7ce3a9d965647bf807b7d2aa0ea88d9e to your computer and use it in GitHub Desktop.
Minimal modern setImmediate polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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