diff --git a/cli/rt/11_workers.js b/cli/rt/11_workers.js index 36a2fd61bb..62210dfaed 100644 --- a/cli/rt/11_workers.js +++ b/cli/rt/11_workers.js @@ -4,6 +4,7 @@ const core = window.Deno.core; const { Window } = window.__bootstrap.globalInterfaces; const { log } = window.__bootstrap.util; + const { defineEventHandler } = window.__bootstrap.webUtil; function createWorker( specifier, @@ -87,9 +88,6 @@ cancelable: false, data, }); - if (this.onmessageerror) { - this.onmessageerror(msgErrorEvent); - } return; } @@ -98,10 +96,6 @@ data, }); - if (this.onmessage) { - this.onmessage(msgEvent); - } - this.dispatchEvent(msgEvent); }; @@ -116,9 +110,6 @@ }); let handled = false; - if (this.onerror) { - this.onerror(event); - } this.dispatchEvent(event); if (event.defaultPrevented) { @@ -205,6 +196,10 @@ } } + defineEventHandler(Worker.prototype, "error"); + defineEventHandler(Worker.prototype, "message"); + defineEventHandler(Worker.prototype, "messageerror"); + window.__bootstrap.worker = { Worker, }; diff --git a/cli/tests/workers_test.ts b/cli/tests/workers_test.ts index 31dc72debd..7cff6848bb 100644 --- a/cli/tests/workers_test.ts +++ b/cli/tests/workers_test.ts @@ -334,3 +334,27 @@ Deno.test({ w.terminate(); }, }); + +Deno.test({ + name: "Worker event handler order", + fn: async function (): Promise { + const promise = createResolvable(); + const w = new Worker( + new URL("subdir/test_worker.ts", import.meta.url).href, + { type: "module", name: "tsWorker" }, + ); + const arr: number[] = []; + w.addEventListener("message", () => arr.push(1)); + w.onmessage = (e): void => { + arr.push(2); + }; + w.addEventListener("message", () => arr.push(3)); + w.addEventListener("message", () => { + assertEquals(arr, [1, 2, 3]); + promise.resolve(); + }); + w.postMessage("Hello World"); + await promise; + w.terminate(); + }, +});