-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathweb_worker.js
28 lines (23 loc) · 931 Bytes
/
web_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
// synchronously, using the browser, import wasm_bindgen shim JS scripts
importScripts('WASM_BINDGEN_SHIM_URL');
// Wait for the main thread to send us the shared module/memory and work context.
// Once we've got it, initialize it all with the `wasm_bindgen` global we imported via
// `importScripts`.
self.onmessage = event => {
let [ module, memory, work ] = event.data;
wasm_bindgen(module, memory).catch(err => {
console.log(err);
// Propagate to main `onerror`:
setTimeout(() => {
throw err;
});
// Rethrow to keep promise rejected and prevent execution of further commands:
throw err;
}).then(wasm => {
// Enter rust code by calling entry point defined in `lib.rs`.
// This executes closure defined by work context.
wasm.wasm_thread_entry_point(work);
// Once done, terminate web worker
close();
});
};