Skip to content
Merged
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 @@ -35,6 +35,7 @@ web-sys = { version = "0.3.66", features = [
"IdbTransactionMode",
"IdbVersionChangeEvent",
"Window",
"WorkerGlobalScope",
] }

[dev-dependencies]
Expand Down
17 changes: 12 additions & 5 deletions src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,11 +28,18 @@ impl<Err: 'static> Factory<Err> {
///
/// This internally uses [`indexedDB`](https://developer.mozilla.org/en-US/docs/Web/API/indexedDB).
pub fn get() -> crate::Result<Factory<Err>, 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::<WorkerGlobalScope>() {
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,
Expand Down
3 changes: 3 additions & 0 deletions tests/browser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

mod common;
4 changes: 1 addition & 3 deletions tests/js.rs → tests/common/js.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
4 changes: 1 addition & 3 deletions tests/js-panic.rs → tests/common/js_panic.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 2 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod js;
mod js_panic;
3 changes: 3 additions & 0 deletions tests/worker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_worker);

mod common;