-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworker.js
35 lines (30 loc) · 847 Bytes
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import "./wasm_exec"
onmessage = async function ({ data: { cmd, mainWasm, payload } }) {
if (cmd === "wasmParaseDemo") {
await initWasm(mainWasm)
const data = self.$wasmParaseDemo(
payload.name,
new Uint8Array(await payload.arrayBuffer()),
onRoundEnd,
)
if (data) self.postMessage([cmd, JSON.parse(data)])
}
}
function onRoundEnd(json) {
self.postMessage(["wasmParaseDemo:RoundEnd", JSON.parse(json)])
}
// this function will be overrided by main_wasm.go
self.$wasmParaseDemo = function () {
console.error("wasmParaseDemo not initialized")
}
self.$wasmLogger = function (log) {
self.postMessage(["wasmLogger", log])
}
async function initWasm(path) {
const go = new Go()
const { instance } = await WebAssembly.instantiateStreaming(
fetch(path),
go.importObject,
)
go.run(instance)
}