Skip to content

Commit

Permalink
fix(indexeddb): window usage in worker env (#2131)
Browse files Browse the repository at this point in the history
This commit tries to get global with type safety and explicit type conversion
  • Loading branch information
borngraced authored Jun 5, 2024
1 parent 40ebe00 commit 2e21532
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
10 changes: 7 additions & 3 deletions mm2src/mm2_db/src/indexed_db/indexed_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ macro_rules! try_serialize_index_value {
return MmError::err(DbTransactionError::ErrorSerializingIndex {
index: $index.to_owned(),
description: ser_err.to_string(),
})
});
},
}
}};
Expand Down Expand Up @@ -326,6 +326,7 @@ impl AddOrIgnoreResult {
}
}
}

impl<'transaction, Table: TableSignature> DbTable<'transaction, Table> {
/// Adds the given item to the table.
/// https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/add
Expand Down Expand Up @@ -830,7 +831,10 @@ fn open_cursor(
/// Detects the current execution environment (window or worker) and follows the appropriate way
/// of getting `web_sys::IdbFactory` instance.
pub(crate) fn get_idb_factory() -> Result<web_sys::IdbFactory, InitDbError> {
let global = js_sys::global();
// try getting global with type safety and explicit type conversion.
let global = js_sys::global()
.dyn_into::<js_sys::Object>()
.map_err(|err| InitDbError::NotSupported(format!("{err:?}")))?;

let idb_factory = if let Some(window) = global.dyn_ref::<Window>() {
window.indexed_db()
Expand Down Expand Up @@ -1216,7 +1220,7 @@ mod tests {
.expect("Couldn't get items by the index 'ticker=RICK'");
assert_eq!(actual_rick_txs, vec![
(rick_tx_1_id, rick_tx_1_updated),
(rick_tx_2_id, rick_tx_2)
(rick_tx_2_id, rick_tx_2),
]);
}

Expand Down
1 change: 0 additions & 1 deletion mm2src/mm2_test_helpers/src/for_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,6 @@ pub async fn enable_eth_coin(
json::from_str(&enable.1).unwrap()
}


pub async fn enable_spl(mm: &MarketMakerIt, coin: &str) -> Json {
let req = json!({
"userpass": mm.userpass,
Expand Down

0 comments on commit 2e21532

Please sign in to comment.