-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
26 lines (24 loc) · 814 Bytes
/
worker.js
File metadata and controls
26 lines (24 loc) · 814 Bytes
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
async function work({ challenge, difficulty, startAt }) {
const { default: initWasm, proof_of_work } = await import('./brain_pow.js')
const wasmUrl = new URL('./brain_pow_bg.wasm', import.meta.url)
await initWasm({ module_or_path: wasmUrl })
const result = proof_of_work(challenge, difficulty, startAt)
return result
}
globalThis.onmessage = (e) => {
const { challenge, difficulty, startAt } = e.data
work({ challenge, difficulty, startAt })
.then((result) => {
globalThis.postMessage({ result })
globalThis.close()
})
.catch((error) => {
if (typeof error === 'string') {
globalThis.postMessage({ error })
} else {
globalThis.postMessage({ error: error.message })
}
globalThis.close()
})
}
globalThis.postMessage({ ready: true })