From 7b0398c903bcde2f2d0a79d5d51a9e693acd419c Mon Sep 17 00:00:00 2001 From: Vladimir Nachbaur Date: Sat, 20 Apr 2024 15:33:40 -0400 Subject: [PATCH 1/3] Add feature flag to keep thread alive --- Cargo.toml | 1 + src/wasm32/js/web_worker.js | 7 ++++--- src/wasm32/js/web_worker_module.js | 6 ++++-- src/wasm32/mod.rs | 9 +++++++++ src/wasm32/utils.rs | 8 ++++++++ 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a313b6e..c1e7556 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ readme = "README.md" [features] default = ["es_modules"] es_modules = [] +keep_worker_alive = [] [dependencies] wasm-bindgen = "0.2" diff --git a/src/wasm32/js/web_worker.js b/src/wasm32/js/web_worker.js index 7f02ee4..3cfea0e 100644 --- a/src/wasm32/js/web_worker.js +++ b/src/wasm32/js/web_worker.js @@ -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(); + } }); }; - \ No newline at end of file diff --git a/src/wasm32/js/web_worker_module.js b/src/wasm32/js/web_worker_module.js index 84945b9..646faff 100644 --- a/src/wasm32/js/web_worker_module.js +++ b/src/wasm32/js/web_worker_module.js @@ -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 (!wasm.keep_worker_alive()) { + // Once done, terminate web worker + close(); + } }); }; \ No newline at end of file diff --git a/src/wasm32/mod.rs b/src/wasm32/mod.rs index a413b20..87c3c40 100644 --- a/src/wasm32/mod.rs +++ b/src/wasm32/mod.rs @@ -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, diff --git a/src/wasm32/utils.rs b/src/wasm32/utils.rs index 11da5e2..4aeb15a 100644 --- a/src/wasm32/utils.rs +++ b/src/wasm32/utils.rs @@ -26,6 +26,14 @@ pub fn is_web_worker_thread() -> bool { js_sys::eval("self").unwrap().dyn_into::().is_ok() } +pub fn close_current_web_worker() { + js_sys::eval("self") + .unwrap() + .dyn_into::() + .unwrap() + .close(); +} + #[cfg(feature = "es_modules")] #[wasm_bindgen(module = "/src/wasm32/js/module_workers_polyfill.min.js")] extern "C" { From d64d2416efb182a5b0f3c79e035e55573d82fd2d Mon Sep 17 00:00:00 2001 From: Vladimir Nachbaur Date: Sat, 20 Apr 2024 15:42:29 -0400 Subject: [PATCH 2/3] fix missing using --- src/wasm32/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wasm32/utils.rs b/src/wasm32/utils.rs index 4aeb15a..da4b924 100644 --- a/src/wasm32/utils.rs +++ b/src/wasm32/utils.rs @@ -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 { if let Some(window) = web_sys::window() { From 90c7f2b99687018b8a38238ad262022032e0e1a8 Mon Sep 17 00:00:00 2001 From: Vladimir Nachbaur Date: Sat, 20 Apr 2024 15:52:00 -0400 Subject: [PATCH 3/3] fix js calls --- src/wasm32/js/web_worker.js | 2 +- src/wasm32/js/web_worker_module.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wasm32/js/web_worker.js b/src/wasm32/js/web_worker.js index 3cfea0e..ef4839a 100644 --- a/src/wasm32/js/web_worker.js +++ b/src/wasm32/js/web_worker.js @@ -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); diff --git a/src/wasm32/js/web_worker_module.js b/src/wasm32/js/web_worker_module.js index 646faff..4ba619e 100644 --- a/src/wasm32/js/web_worker_module.js +++ b/src/wasm32/js/web_worker_module.js @@ -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); @@ -21,7 +21,7 @@ self.onmessage = event => { // This executes closure defined by work context. wasm_thread_entry_point(work); - if (!wasm.keep_worker_alive()) { + if (!keep_worker_alive()) { // Once done, terminate web worker close(); }