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

22 lines
635 B
HTML

<!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>