1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

fix(web/worker) define event handlers in DOM order (#8334)

This commit is contained in:
Benjamin Gruenbaum 2020-11-10 14:15:42 +02:00 committed by GitHub
parent 97a9620a01
commit b255a05f18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 10 deletions

View file

@ -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,
};

View file

@ -334,3 +334,27 @@ Deno.test({
w.terminate();
},
});
Deno.test({
name: "Worker event handler order",
fn: async function (): Promise<void> {
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();
},
});