Skip to content

Commit 951888f

Browse files
authored
Allow use in a web worker (#2)
* fix: allow use in web worker * Add tests in worker scope * Add common module for browser and worker tests * js_panic changes from main
1 parent e43c417 commit 951888f

File tree

7 files changed

+23
-11
lines changed

7 files changed

+23
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ web-sys = { version = "0.3.66", features = [
3535
"IdbTransactionMode",
3636
"IdbVersionChangeEvent",
3737
"Window",
38+
"WorkerGlobalScope",
3839
] }
3940

4041
[dev-dependencies]

src/factory.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use futures_util::{
66
};
77
use std::{future::Future, marker::PhantomData, sync::atomic::Ordering};
88
use web_sys::{
9-
js_sys::Function,
9+
js_sys::{self, Function},
1010
wasm_bindgen::{closure::Closure, JsCast, JsValue},
11-
IdbDatabase, IdbFactory, IdbOpenDbRequest, IdbVersionChangeEvent,
11+
IdbDatabase, IdbFactory, IdbOpenDbRequest, IdbVersionChangeEvent, WorkerGlobalScope,
1212
};
1313

1414
/// Wrapper for [`IDBFactory`](https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory)
@@ -28,11 +28,18 @@ impl<Err: 'static> Factory<Err> {
2828
///
2929
/// This internally uses [`indexedDB`](https://developer.mozilla.org/en-US/docs/Web/API/indexedDB).
3030
pub fn get() -> crate::Result<Factory<Err>, Err> {
31-
let window = web_sys::window().ok_or(crate::Error::NotInBrowser)?;
32-
let sys = window
33-
.indexed_db()
31+
let indexed_db = if let Some(window) = web_sys::window() {
32+
window.indexed_db()
33+
} else if let Ok(worker_scope) = js_sys::global().dyn_into::<WorkerGlobalScope>() {
34+
worker_scope.indexed_db()
35+
} else {
36+
return Err(crate::Error::NotInBrowser);
37+
};
38+
39+
let sys = indexed_db
3440
.map_err(|_| crate::Error::IndexedDbDisabled)?
3541
.ok_or(crate::Error::IndexedDbDisabled)?;
42+
3643
Ok(Factory {
3744
sys,
3845
_phantom: PhantomData,

tests/browser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
2+
3+
mod common;

tests/js.rs renamed to tests/common/js.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use indexed_db::{Error, Factory};
2-
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
2+
use wasm_bindgen_test::wasm_bindgen_test;
33
use web_sys::{
44
js_sys::{JsString, Number},
55
wasm_bindgen::JsValue,
66
};
77

8-
wasm_bindgen_test_configure!(run_in_browser);
9-
108
#[wasm_bindgen_test]
119
async fn smoke_test() {
1210
// tracing_wasm::set_as_global_default();

tests/js-panic.rs renamed to tests/common/js_panic.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use anyhow::Context;
22
use indexed_db::Factory;
3-
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
3+
use wasm_bindgen_test::wasm_bindgen_test;
44
use web_sys::js_sys::JsString;
55

6-
wasm_bindgen_test_configure!(run_in_browser);
7-
86
// Currently, panic=abort, so the sender is never Drop'd in the callback handlers.
97
// As such, despite the proper error message being displayed by the console error
108
// panic hook, `should_panic` never detects it due to the deadlock happening first.

tests/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod js;
2+
mod js_panic;

tests/worker.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_worker);
2+
3+
mod common;

0 commit comments

Comments
 (0)