Skip to content

Instantly share code, notes, and snippets.

@steida
Created October 5, 2019 00:15
Show Gist options
  • Save steida/7ce3a9d965647bf807b7d2aa0ea88d9e to your computer and use it in GitHub Desktop.
Save steida/7ce3a9d965647bf807b7d2aa0ea88d9e to your computer and use it in GitHub Desktop.

Revisions

  1. steida created this gist Oct 5, 2019.
    22 changes: 22 additions & 0 deletions setImmediate.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    // 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);
    };
    })();