securityos/node_modules/comlink/docs/examples/07-sharedworker-example/index.html

22 lines
635 B
HTML
Raw Permalink Normal View History

2024-09-06 15:32:35 +00:00
<!DOCTYPE html>
<script type="module">
import * as Comlink from "https://unpkg.com/comlink/dist/esm/comlink.mjs";
// import * as Comlink from "../../../dist/esm/comlink.mjs";
async function init() {
const worker = new SharedWorker("worker.js");
/**
* SharedWorkers communicate via the `postMessage` function in their `port` property.
* Therefore you must use the SharedWorker's `port` property when calling `Comlink.wrap`.
*/
const obj = Comlink.wrap(worker.port);
alert(`Counter: ${await obj.counter}`);
await obj.inc();
alert(`Counter: ${await obj.counter}`);
}
init();
</script>