Skip to content
Snippets Groups Projects
Commit 052a0897 authored by Cheng Shao's avatar Cheng Shao :beach:
Browse files

wasm: fix setImmediate() implementation for Cloudflare Workers

This patch fixes setImmediate() implementation for Cloudflare Workers
in the wasm backend's js prelude script. Cloudflare Workers doesn't
support the MessageChannel API, and we use a setTimeout() based
fallback implementation in this case.

(cherry picked from commit c37b96fa)
(cherry picked from commit 6bd2d9ad)
parent 38de318e
No related branches found
No related tags found
No related merge requests found
...@@ -51,6 +51,10 @@ export class JSValManager { ...@@ -51,6 +51,10 @@ export class JSValManager {
// The actual setImmediate() to be used. This is a ESM module top // The actual setImmediate() to be used. This is a ESM module top
// level binding and doesn't pollute the globalThis namespace. // level binding and doesn't pollute the globalThis namespace.
//
// To benchmark different setImmediate() implementations in the
// browser, use https://github.com/jphpsf/setImmediate-shim-demo as a
// starting point.
const setImmediate = await (async () => { const setImmediate = await (async () => {
// node, bun, or other scripts might have set this up in the browser // node, bun, or other scripts might have set this up in the browser
if (globalThis.setImmediate) { if (globalThis.setImmediate) {
...@@ -67,30 +71,35 @@ const setImmediate = await (async () => { ...@@ -67,30 +71,35 @@ const setImmediate = await (async () => {
return (cb, ...args) => scheduler.postTask(() => cb(...args)); return (cb, ...args) => scheduler.postTask(() => cb(...args));
} }
// A simple & fast setImmediate() implementation for browsers. It's // Cloudflare workers doesn't support MessageChannel
// not a drop-in replacement for node.js setImmediate() because: if (globalThis.MessageChannel) {
// 1. There's no clearImmediate(), and setImmediate() doesn't return // A simple & fast setImmediate() implementation for browsers. It's
// anything // not a drop-in replacement for node.js setImmediate() because:
// 2. There's no guarantee that callbacks scheduled by setImmediate() // 1. There's no clearImmediate(), and setImmediate() doesn't return
// are executed in the same order (in fact it's the opposite lol), // anything
// but you are never supposed to rely on this assumption anyway // 2. There's no guarantee that callbacks scheduled by setImmediate()
class SetImmediate { // are executed in the same order (in fact it's the opposite lol),
#fs = []; // but you are never supposed to rely on this assumption anyway
#mc = new MessageChannel(); class SetImmediate {
#fs = [];
#mc = new MessageChannel();
constructor() { constructor() {
this.#mc.port1.addEventListener("message", () => { this.#mc.port1.addEventListener("message", () => {
this.#fs.pop()(); this.#fs.pop()();
}); });
this.#mc.port1.start(); this.#mc.port1.start();
} }
setImmediate(cb, ...args) { setImmediate(cb, ...args) {
this.#fs.push(() => cb(...args)); this.#fs.push(() => cb(...args));
this.#mc.port2.postMessage(undefined); this.#mc.port2.postMessage(undefined);
}
} }
const sm = new SetImmediate();
return (cb, ...args) => sm.setImmediate(cb, ...args);
} }
const sm = new SetImmediate(); return (cb, ...args) => setTimeout(cb, 0, ...args);
return (cb, ...args) => sm.setImmediate(cb, ...args);
})(); })();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment