2022-01-20 02:10:16 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-05-22 12:08:24 -04:00
|
|
|
import { assertEquals } from "../../../test_util/std/testing/asserts.ts";
|
|
|
|
import { deferred } from "../../../test_util/std/async/deferred.ts";
|
|
|
|
|
|
|
|
Deno.test("broadcastchannel worker", async () => {
|
|
|
|
const intercom = new BroadcastChannel("intercom");
|
|
|
|
let count = 0;
|
|
|
|
|
2021-08-11 10:20:47 -04:00
|
|
|
const url = new URL(
|
|
|
|
"../testdata/workers/broadcast_channel.ts",
|
|
|
|
import.meta.url,
|
|
|
|
);
|
2021-05-22 12:08:24 -04:00
|
|
|
const worker = new Worker(url.href, { type: "module", name: "worker" });
|
|
|
|
worker.onmessage = () => intercom.postMessage(++count);
|
|
|
|
|
|
|
|
const promise = deferred();
|
|
|
|
|
|
|
|
intercom.onmessage = function (e) {
|
|
|
|
assertEquals(count, e.data);
|
|
|
|
if (count < 42) {
|
|
|
|
intercom.postMessage(++count);
|
|
|
|
} else {
|
|
|
|
worker.terminate();
|
|
|
|
intercom.close();
|
|
|
|
promise.resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
});
|