diff --git a/Cargo.toml b/Cargo.toml index 128d90b..5973d91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,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 82977ed..4419790 100644 --- a/src/factory.rs +++ b/src/factory.rs @@ -6,9 +6,9 @@ use futures_util::{ }; use std::{future::Future, marker::PhantomData, sync::atomic::Ordering}; 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, 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 98% rename from tests/js.rs rename to tests/common/js.rs index b61c4de..a826c87 100644 --- a/tests/js.rs +++ b/tests/common/js.rs @@ -1,12 +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, }; -wasm_bindgen_test_configure!(run_in_browser); - #[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 94% rename from tests/js-panic.rs rename to tests/common/js_panic.rs index dbb75f1..484b3fb 100644 --- a/tests/js-panic.rs +++ b/tests/common/js_panic.rs @@ -1,10 +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; -wasm_bindgen_test_configure!(run_in_browser); - // 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. 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;