From f9d08042f034e1d81c9883629d366e7aecfb2113 Mon Sep 17 00:00:00 2001 From: Fedeparma74 Date: Thu, 18 Jan 2024 20:36:17 +0100 Subject: [PATCH 1/4] fix: allow use in web worker --- Cargo.toml | 1 + src/factory.rs | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 785bb76..67b57aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ web-sys = { version = "0.3.66", features = [ "IdbTransactionMode", "IdbVersionChangeEvent", "Window", + "WorkerGlobalScope", ] } [dev-dependencies] diff --git a/src/factory.rs b/src/factory.rs index b242f4e..07a9143 100644 --- a/src/factory.rs +++ b/src/factory.rs @@ -6,9 +6,9 @@ use futures_util::{ }; use std::{future::Future, marker::PhantomData}; use web_sys::{ - js_sys::Function, + js_sys::{self, Function}, wasm_bindgen::{closure::Closure, JsCast, JsValue}, - IdbDatabase, IdbFactory, IdbOpenDbRequest, IdbVersionChangeEvent, + IdbDatabase, IdbFactory, IdbOpenDbRequest, IdbVersionChangeEvent, WorkerGlobalScope, }; /// Wrapper for [`IDBFactory`](https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory) @@ -28,11 +28,18 @@ impl Factory { /// /// This internally uses [`indexedDB`](https://developer.mozilla.org/en-US/docs/Web/API/indexedDB). pub fn get() -> crate::Result, Err> { - let window = web_sys::window().ok_or(crate::Error::NotInBrowser)?; - let sys = window - .indexed_db() + let indexed_db = if let Some(window) = web_sys::window() { + window.indexed_db() + } else if let Ok(worker_scope) = js_sys::global().dyn_into::() { + worker_scope.indexed_db() + } else { + return Err(crate::Error::NotInBrowser); + }; + + let sys = indexed_db .map_err(|_| crate::Error::IndexedDbDisabled)? .ok_or(crate::Error::IndexedDbDisabled)?; + Ok(Factory { sys, _phantom: PhantomData, From 591ea398c391251fc73d04abcead5831423d43c1 Mon Sep 17 00:00:00 2001 From: Fedeparma74 Date: Wed, 31 Jan 2024 14:54:25 +0100 Subject: [PATCH 2/4] Add tests in worker scope --- Cargo.toml | 3 +++ tests/js-panic.rs | 3 +++ tests/js.rs | 3 +++ 3 files changed, 9 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 67b57aa..fe7be36 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,3 +43,6 @@ console_error_panic_hook = "0.1.7" futures = "0.3.30" tracing-wasm = "0.2.1" wasm-bindgen-test = "0.3.39" + +[features] +test-worker = [] \ No newline at end of file diff --git a/tests/js-panic.rs b/tests/js-panic.rs index 7ab0d37..6f3798d 100644 --- a/tests/js-panic.rs +++ b/tests/js-panic.rs @@ -3,7 +3,10 @@ use indexed_db::Factory; use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; use web_sys::js_sys::JsString; +#[cfg(not(feature = "test-worker"))] wasm_bindgen_test_configure!(run_in_browser); +#[cfg(feature = "test-worker")] +wasm_bindgen_test_configure!(run_in_worker); #[wasm_bindgen_test] #[should_panic(expected = "Transaction blocked without any request under way")] diff --git a/tests/js.rs b/tests/js.rs index f7d5fa4..83da345 100644 --- a/tests/js.rs +++ b/tests/js.rs @@ -5,7 +5,10 @@ use web_sys::{ wasm_bindgen::JsValue, }; +#[cfg(not(feature = "test-worker"))] wasm_bindgen_test_configure!(run_in_browser); +#[cfg(feature = "test-worker")] +wasm_bindgen_test_configure!(run_in_worker); #[wasm_bindgen_test] async fn smoke_test() { From 917da9851d40d02fceb6a4401bef74ba5ca6c5f5 Mon Sep 17 00:00:00 2001 From: Fedeparma74 Date: Thu, 22 Feb 2024 21:36:11 +0100 Subject: [PATCH 3/4] Add common module for browser and worker tests --- Cargo.toml | 3 --- tests/browser.rs | 3 +++ tests/{ => common}/js.rs | 7 +------ tests/{js-panic.rs => common/js_panic.rs} | 7 +------ tests/common/mod.rs | 2 ++ tests/worker.rs | 3 +++ 6 files changed, 10 insertions(+), 15 deletions(-) create mode 100644 tests/browser.rs rename tests/{ => common}/js.rs (97%) rename tests/{js-panic.rs => common/js_panic.rs} (86%) create mode 100644 tests/common/mod.rs create mode 100644 tests/worker.rs diff --git a/Cargo.toml b/Cargo.toml index fe7be36..67b57aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,3 @@ console_error_panic_hook = "0.1.7" futures = "0.3.30" tracing-wasm = "0.2.1" wasm-bindgen-test = "0.3.39" - -[features] -test-worker = [] \ No newline at end of file diff --git a/tests/browser.rs b/tests/browser.rs new file mode 100644 index 0000000..23f51be --- /dev/null +++ b/tests/browser.rs @@ -0,0 +1,3 @@ +wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + +mod common; diff --git a/tests/js.rs b/tests/common/js.rs similarity index 97% rename from tests/js.rs rename to tests/common/js.rs index 83da345..ddd8a05 100644 --- a/tests/js.rs +++ b/tests/common/js.rs @@ -1,15 +1,10 @@ use indexed_db::{Error, Factory}; -use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; +use wasm_bindgen_test::wasm_bindgen_test; use web_sys::{ js_sys::{JsString, Number}, wasm_bindgen::JsValue, }; -#[cfg(not(feature = "test-worker"))] -wasm_bindgen_test_configure!(run_in_browser); -#[cfg(feature = "test-worker")] -wasm_bindgen_test_configure!(run_in_worker); - #[wasm_bindgen_test] async fn smoke_test() { tracing_wasm::set_as_global_default(); diff --git a/tests/js-panic.rs b/tests/common/js_panic.rs similarity index 86% rename from tests/js-panic.rs rename to tests/common/js_panic.rs index 6f3798d..8306a59 100644 --- a/tests/js-panic.rs +++ b/tests/common/js_panic.rs @@ -1,13 +1,8 @@ use anyhow::Context; use indexed_db::Factory; -use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; +use wasm_bindgen_test::wasm_bindgen_test; use web_sys::js_sys::JsString; -#[cfg(not(feature = "test-worker"))] -wasm_bindgen_test_configure!(run_in_browser); -#[cfg(feature = "test-worker")] -wasm_bindgen_test_configure!(run_in_worker); - #[wasm_bindgen_test] #[should_panic(expected = "Transaction blocked without any request under way")] async fn other_awaits_panic() { diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..6b9e47f --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,2 @@ +mod js; +mod js_panic; diff --git a/tests/worker.rs b/tests/worker.rs new file mode 100644 index 0000000..24122d9 --- /dev/null +++ b/tests/worker.rs @@ -0,0 +1,3 @@ +wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_worker); + +mod common; From 7b253f7ce94ca938a262a9508d8fc2f8fe5bf2ab Mon Sep 17 00:00:00 2001 From: Fedeparma74 Date: Sat, 24 Feb 2024 08:55:47 +0100 Subject: [PATCH 4/4] js_panic changes from main --- tests/common/js_panic.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/common/js_panic.rs b/tests/common/js_panic.rs index 8306a59..484b3fb 100644 --- a/tests/common/js_panic.rs +++ b/tests/common/js_panic.rs @@ -3,9 +3,17 @@ use indexed_db::Factory; use wasm_bindgen_test::wasm_bindgen_test; use web_sys::js_sys::JsString; +// Currently, panic=abort, so the sender is never Drop'd in the callback handlers. +// As such, despite the proper error message being displayed by the console error +// panic hook, `should_panic` never detects it due to the deadlock happening first. +// This is acceptable behavior for us still. #[wasm_bindgen_test] +#[ignore] #[should_panic(expected = "Transaction blocked without any request under way")] async fn other_awaits_panic() { + // tracing_wasm::set_as_global_default(); + // std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + let factory = Factory::::get().unwrap(); let db = factory @@ -34,8 +42,11 @@ async fn other_awaits_panic() { } #[wasm_bindgen_test] -#[should_panic(expected = "Transaction blocked without any request under way")] +#[should_panic] // For some reason the error message is not detected here, but appears clearly with console_error_panic_hook async fn await_in_versionchange_panics() { + // tracing_wasm::set_as_global_default(); + // std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + let factory = Factory::::get().unwrap(); let (tx, rx) = futures_channel::oneshot::channel();