Skip to content

Improve worker.js reliability #1820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 15 additions & 26 deletions examples/raytrace-parallel/worker.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
// synchronously, using the browser, import out shim JS scripts
importScripts('raytrace_parallel.js');

let booted = false;
let lastPtr = null;

// 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 = function(args) {
self.onmessage = event => run(event.data);
const [module, memory] = args.data;
wasm_bindgen(module, memory)
.then(() => {
booted = true;
if (lastPtr)
run(lastPtr);
})
.catch(e => setTimeout(() => { throw e; })); // propagate to main `onerror`
};
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;
});

function run(ptr) {
if (!booted) {
lastPtr = ptr;
return;
}
lastPtr = null;
try {
wasm_bindgen.child_entry_point(ptr);
} catch (e) {
throw new Error(e.message + "\n\n" + e.stack);
}
}
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);
};
};