Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ readme = "README.md"
[features]
default = ["es_modules"]
es_modules = []
keep_worker_alive = []

[dependencies]
wasm-bindgen = "0.2"
Expand Down
9 changes: 5 additions & 4 deletions src/wasm32/js/web_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ importScripts('WASM_BINDGEN_SHIM_URL');
// 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;
let [module, memory, work] = event.data;

wasm_bindgen(module, memory).catch(err => {
console.log(err);
Expand All @@ -21,8 +21,9 @@ self.onmessage = event => {
// This executes closure defined by work context.
wasm.wasm_thread_entry_point(work);

// Once done, terminate web worker
close();
if (!wasm.keep_worker_alive()) {
// Once done, terminate web worker
close();
}
});
};

10 changes: 6 additions & 4 deletions src/wasm32/js/web_worker_module.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// synchronously, using the browser, import wasm_bindgen shim JS scripts
import init, {wasm_thread_entry_point} from "WASM_BINDGEN_SHIM_URL";
import init, { wasm_thread_entry_point, keep_worker_alive } from "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;
let [module, memory, work] = event.data;

init(module, memory).catch(err => {
console.log(err);
Expand All @@ -21,7 +21,9 @@ self.onmessage = event => {
// This executes closure defined by work context.
wasm_thread_entry_point(work);

// Once done, terminate web worker
close();
if (!keep_worker_alive()) {
// Once done, terminate web worker
close();
}
});
};
9 changes: 9 additions & 0 deletions src/wasm32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ pub fn wasm_thread_entry_point(ptr: u32) {
WorkerMessage::ThreadComplete.post();
}

/// Whether to keep the worker alive or not after it's execution is done
#[wasm_bindgen]
pub fn keep_worker_alive() -> bool {
#[cfg(feature = "keep_worker_alive")]
return true;
#[cfg(not(feature = "keep_worker_alive"))]
return false;
}

/// Used to relay spawn requests from web workers to main thread
struct BuilderRequest {
builder: Builder,
Expand Down
10 changes: 9 additions & 1 deletion src/wasm32/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use wasm_bindgen::prelude::*;
use web_sys::{Blob, Url, WorkerGlobalScope};
use web_sys::{Blob, DedicatedWorkerGlobalScope, Url, WorkerGlobalScope};

pub fn available_parallelism() -> io::Result<NonZeroUsize> {
if let Some(window) = web_sys::window() {
Expand All @@ -26,6 +26,14 @@ pub fn is_web_worker_thread() -> bool {
js_sys::eval("self").unwrap().dyn_into::<WorkerGlobalScope>().is_ok()
}

pub fn close_current_web_worker() {
js_sys::eval("self")
.unwrap()
.dyn_into::<DedicatedWorkerGlobalScope>()
.unwrap()
.close();
}

#[cfg(feature = "es_modules")]
#[wasm_bindgen(module = "/src/wasm32/js/module_workers_polyfill.min.js")]
extern "C" {
Expand Down