You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
635 B
22 lines
635 B
|
4 months ago
|
<!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>
|