16
16
17
17
use std:: time:: Duration ;
18
18
19
- use js_sys:: { Date , Uint8Array } ;
20
- use matrix_sdk_common:: ruma:: {
21
- DeviceKeyAlgorithm , MilliSecondsSinceUnixEpoch , SecondsSinceUnixEpoch , UInt ,
19
+ use js_sys:: { Date , Promise , Uint8Array } ;
20
+ use matrix_sdk_common:: {
21
+ js_tracing:: JsLogger ,
22
+ ruma:: { DeviceKeyAlgorithm , MilliSecondsSinceUnixEpoch , SecondsSinceUnixEpoch , UInt } ,
22
23
} ;
23
24
use matrix_sdk_crypto:: {
24
25
olm:: PrivateCrossSigningIdentity ,
@@ -31,11 +32,14 @@ use matrix_sdk_crypto::{
31
32
vodozemac:: { Curve25519PublicKey , Ed25519PublicKey } ,
32
33
Session ,
33
34
} ;
35
+ use tracing:: dispatcher;
34
36
use wasm_bindgen:: prelude:: * ;
35
37
36
38
use crate :: {
39
+ future:: future_to_promise,
37
40
identifiers:: { DeviceId , RoomId , UserId } ,
38
41
store:: StoreHandle ,
42
+ tracing:: logger_to_dispatcher,
39
43
} ;
40
44
41
45
type Result < T , E = JsError > = std:: result:: Result < T , E > ;
@@ -52,7 +56,7 @@ pub struct Migration {}
52
56
///
53
57
/// Can be imported into the rust store with {@link Migration::migrateBaseData}.
54
58
#[ wasm_bindgen( getter_with_clone) ]
55
- #[ derive( Debug , Default ) ]
59
+ #[ derive( Debug , Default , Clone ) ]
56
60
pub struct BaseMigrationData {
57
61
/// The user id of the account owner.
58
62
#[ wasm_bindgen( js_name = "userId" ) ]
@@ -115,15 +119,23 @@ impl Migration {
115
119
/// account objects.
116
120
/// * `store_handle` - A connection to the CryptoStore which will be used to
117
121
/// store the vodozemac data.
118
- #[ wasm_bindgen( js_name = "migrateBaseData" ) ]
119
- pub async fn migrate_base_data (
122
+ /// * `logger` - An optional logger instance to use for writing log messages
123
+ /// during the migration operation. An instance of `JsLogger`.
124
+ #[ wasm_bindgen( js_name = "migrateBaseData" , unchecked_return_type = "Promise<void>" ) ]
125
+ pub fn migrate_base_data (
120
126
data : & BaseMigrationData ,
121
127
pickle_key : Uint8Array ,
122
128
store_handle : & StoreHandle ,
123
- ) -> Result < JsValue , JsError > {
124
- migrate_base_data_to_store ( data, & ( pickle_key. to_vec ( ) ) , store_handle. store . as_ref ( ) )
125
- . await ?;
126
- Ok ( JsValue :: UNDEFINED )
129
+ logger : Option < JsLogger > ,
130
+ ) -> Promise {
131
+ let _guard = dispatcher:: set_default ( & logger_to_dispatcher ( logger) ) ;
132
+ let store_handle = store_handle. clone ( ) ;
133
+ let data = data. clone ( ) ;
134
+ future_to_promise ( async move {
135
+ migrate_base_data_to_store ( & data, & ( pickle_key. to_vec ( ) ) , store_handle. store . as_ref ( ) )
136
+ . await ?;
137
+ Ok ( JsValue :: UNDEFINED )
138
+ } )
127
139
}
128
140
}
129
141
@@ -241,21 +253,30 @@ impl Migration {
241
253
/// session objects.
242
254
/// * `store_handle` - A connection to the CryptoStore which will be used to
243
255
/// store the vodozemac data.
244
- #[ wasm_bindgen( js_name = "migrateOlmSessions" ) ]
245
- pub async fn migrate_olm_sessions (
256
+ /// * `logger` - An optional logger instance to use for writing log messages
257
+ /// during the migration operation. An instance of `JsLogger`.
258
+ #[ wasm_bindgen( js_name = "migrateOlmSessions" , unchecked_return_type = "Promise<void>" ) ]
259
+ pub fn migrate_olm_sessions (
246
260
sessions : Vec < PickledSession > ,
247
261
pickle_key : Uint8Array ,
248
262
store_handle : & StoreHandle ,
249
- ) -> Result < JsValue , JsError > {
263
+ logger : Option < JsLogger > ,
264
+ ) -> Result < Promise , JsError > {
265
+ let _guard = dispatcher:: set_default ( & logger_to_dispatcher ( logger) ) ;
266
+
250
267
let pickle_key = pickle_key. to_vec ( ) ;
251
268
252
269
let rust_sessions = sessions
253
270
. into_iter ( )
254
271
. map ( |session| libolm_pickled_session_to_rust_pickled_session ( session, & pickle_key) )
255
272
. collect :: < Result < _ > > ( ) ?;
256
273
257
- import_olm_sessions_to_store ( rust_sessions, store_handle. store . as_ref ( ) ) . await ?;
258
- Ok ( JsValue :: UNDEFINED )
274
+ let store_handle = store_handle. clone ( ) ;
275
+
276
+ Ok ( future_to_promise ( async move {
277
+ import_olm_sessions_to_store ( rust_sessions, store_handle. store . as_ref ( ) ) . await ?;
278
+ Ok ( JsValue :: UNDEFINED )
279
+ } ) )
259
280
}
260
281
}
261
282
@@ -393,12 +414,16 @@ impl Migration {
393
414
/// megolm session objects.
394
415
/// * `store_handle` - A connection to the CryptoStore which will be used to
395
416
/// store the vodozemac data.
396
- #[ wasm_bindgen( js_name = "migrateMegolmSessions" ) ]
397
- pub async fn migrate_megolm_sessions (
417
+ /// * `logger` - An optional logger instance to use for writing log messages
418
+ /// during the migration operation. An instance of `JsLogger`.
419
+ #[ wasm_bindgen( js_name = "migrateMegolmSessions" , unchecked_return_type = "Promise<void>" ) ]
420
+ pub fn migrate_megolm_sessions (
398
421
sessions : Vec < PickledInboundGroupSession > ,
399
422
pickle_key : Uint8Array ,
400
423
store_handle : & StoreHandle ,
401
- ) -> Result < JsValue , JsError > {
424
+ logger : Option < JsLogger > ,
425
+ ) -> Result < Promise , JsError > {
426
+ let _guard = dispatcher:: set_default ( & logger_to_dispatcher ( logger) ) ;
402
427
let pickle_key = pickle_key. to_vec ( ) ;
403
428
404
429
let rust_sessions = sessions
@@ -408,8 +433,11 @@ impl Migration {
408
433
} )
409
434
. collect :: < Result < _ > > ( ) ?;
410
435
411
- import_megolm_sessions_to_store ( rust_sessions, store_handle. store . as_ref ( ) ) . await ?;
412
- Ok ( JsValue :: UNDEFINED )
436
+ let store_handle = store_handle. clone ( ) ;
437
+ Ok ( future_to_promise ( async move {
438
+ import_megolm_sessions_to_store ( rust_sessions, store_handle. store . as_ref ( ) ) . await ?;
439
+ Ok ( JsValue :: UNDEFINED )
440
+ } ) )
413
441
}
414
442
}
415
443
0 commit comments