2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-02-12 21:05:10 -05:00
|
|
|
import { assertEquals } from "@std/assert/mod.ts";
|
2021-05-22 12:08:24 -04:00
|
|
|
|
2023-01-12 07:43:36 -05:00
|
|
|
Deno.test("BroadcastChannel worker", async () => {
|
2021-05-22 12:08:24 -04:00
|
|
|
const intercom = new BroadcastChannel("intercom");
|
|
|
|
let count = 0;
|
|
|
|
|
2022-07-20 10:56:53 -04:00
|
|
|
const url = import.meta.resolve(
|
2021-08-11 10:20:47 -04:00
|
|
|
"../testdata/workers/broadcast_channel.ts",
|
|
|
|
);
|
2022-07-20 10:56:53 -04:00
|
|
|
const worker = new Worker(url, { type: "module", name: "worker" });
|
2021-05-22 12:08:24 -04:00
|
|
|
worker.onmessage = () => intercom.postMessage(++count);
|
|
|
|
|
2023-11-10 16:29:09 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2021-05-22 12:08:24 -04:00
|
|
|
|
|
|
|
intercom.onmessage = function (e) {
|
|
|
|
assertEquals(count, e.data);
|
|
|
|
if (count < 42) {
|
|
|
|
intercom.postMessage(++count);
|
|
|
|
} else {
|
|
|
|
worker.terminate();
|
|
|
|
intercom.close();
|
2023-11-10 16:29:09 -05:00
|
|
|
resolve();
|
2021-05-22 12:08:24 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
});
|
2023-01-12 07:43:36 -05:00
|
|
|
|
|
|
|
Deno.test("BroadcastChannel immediate close after post", () => {
|
|
|
|
const bc = new BroadcastChannel("internal_notification");
|
|
|
|
bc.postMessage("New listening connected!");
|
|
|
|
bc.close();
|
|
|
|
});
|