Skip to content

Commit

Permalink
Add worker.js file
Browse files Browse the repository at this point in the history
  • Loading branch information
chemicstry committed Mar 24, 2020
1 parent 44b0e26 commit 2b78b6c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions js/worker.js
@@ -0,0 +1,25 @@
// synchronously, using the browser, import out shim JS scripts
importScripts('pkg/web_worker.js');

// Wait for the main thread to send us the shared module/memory. Once we've got
// it, initialize it all with the `wasm_bindgen` global we imported via
// `importScripts`.
//
// After our first message all subsequent messages are an entry point to run,
// so we just do that.
self.onmessage = event => {
let initialised = wasm_bindgen(...event.data).catch(err => {
// Propagate to main `onerror`:
setTimeout(() => {
throw err;
});
// Rethrow to keep promise rejected and prevent execution of further commands:
throw err;
});

self.onmessage = async event => {
// This will queue further commands up until the module is fully initialised:
await initialised;
wasm_bindgen.child_entry_point(event.data);
};
};

0 comments on commit 2b78b6c

Please sign in to comment.