Skip to content

Commit 77c3130

Browse files
committed
Allow supplying a logger for store open operations
1 parent 2d66344 commit 77c3130

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/store.rs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
33
use std::sync::Arc;
44

5+
use js_sys::Promise;
56
use matrix_sdk_crypto::{
67
store::{DynCryptoStore, IntoCryptoStore, MemoryStore},
78
types::BackupSecrets,
89
};
10+
use tracing::{dispatcher, instrument::WithSubscriber};
911
use wasm_bindgen::prelude::*;
1012
use zeroize::{Zeroize, Zeroizing};
1113

1214
use crate::{
1315
encryption::EncryptionAlgorithm,
16+
future::future_to_promise,
1417
identifiers::{RoomId, UserId},
1518
impl_from_to_inner,
19+
tracing::{logger_to_dispatcher, JsLogger},
1620
vodozemac::Curve25519PublicKey,
1721
};
1822

@@ -44,12 +48,19 @@ impl StoreHandle {
4448
///
4549
/// * `store_passphrase` - The passphrase that should be used to encrypt the
4650
/// store, for IndexedDB-based stores
47-
#[wasm_bindgen(js_name = "open")]
48-
pub async fn open_for_js(
51+
///
52+
/// * `logger` - An optional logger instance to use for writing log messages
53+
/// during the open operation. An instance of `JsLogger`.
54+
#[wasm_bindgen(js_name = "open", unchecked_return_type = "Promise<StoreHandle>")]
55+
pub fn open_for_js(
4956
store_name: Option<String>,
5057
store_passphrase: Option<String>,
51-
) -> Result<StoreHandle, JsError> {
52-
StoreHandle::open(store_name, store_passphrase).await
58+
logger: Option<JsLogger>,
59+
) -> Promise {
60+
let _guard = dispatcher::set_default(&logger_to_dispatcher(logger));
61+
future_to_promise(async move {
62+
StoreHandle::open(store_name, store_passphrase).with_current_subscriber().await
63+
})
5364
}
5465

5566
pub(crate) async fn open(
@@ -108,26 +119,33 @@ impl StoreHandle {
108119
///
109120
/// * `store_key` - The key that should be used to encrypt the store, for
110121
/// IndexedDB-based stores. Must be a 32-byte array.
111-
#[wasm_bindgen(js_name = "openWithKey")]
112-
pub async fn open_with_key(
122+
///
123+
/// * `logger` - An optional logger instance to use for writing log messages
124+
/// during the open operation. An instance of `JsLogger`.
125+
#[wasm_bindgen(js_name = "openWithKey", unchecked_return_type = "Promise<StoreHandle>")]
126+
pub fn open_with_key(
113127
store_name: String,
114128
mut store_key: Vec<u8>,
115-
) -> Result<StoreHandle, JsError> {
116-
let store_key_array: Zeroizing<[u8; 32]> = Zeroizing::new(
117-
store_key
118-
.as_slice()
119-
.try_into()
120-
.map_err(|_| JsError::new("Expected a key of length 32"))?,
121-
);
122-
store_key.zeroize();
123-
124-
let store = matrix_sdk_indexeddb::IndexeddbCryptoStore::open_with_key(
125-
&store_name,
126-
&store_key_array,
127-
)
128-
.await?;
129-
130-
Ok(Self { store: store.into_crypto_store() })
129+
logger: Option<JsLogger>,
130+
) -> Promise {
131+
let _guard = dispatcher::set_default(&logger_to_dispatcher(logger));
132+
future_to_promise(async move {
133+
let store_key_array: Zeroizing<[u8; 32]> = Zeroizing::new(
134+
store_key
135+
.as_slice()
136+
.try_into()
137+
.map_err(|_| JsError::new("Expected a key of length 32"))?,
138+
);
139+
store_key.zeroize();
140+
141+
let store = matrix_sdk_indexeddb::IndexeddbCryptoStore::open_with_key(
142+
&store_name,
143+
&store_key_array,
144+
)
145+
.await?;
146+
147+
Ok(Self { store: store.into_crypto_store() })
148+
})
131149
}
132150
}
133151

0 commit comments

Comments
 (0)