@@ -60,7 +60,7 @@ impl OlmMachine {
60
60
/// the `initialize` method.
61
61
///
62
62
/// Why this pattern? `initialize` returns a `Promise`. Returning a
63
- // `Promise` from a constructor is not idiomatic in JavaScript.
63
+ /// `Promise` from a constructor is not idiomatic in JavaScript.
64
64
#[ wasm_bindgen( constructor) ]
65
65
pub fn new ( ) -> Result < OlmMachine , JsError > {
66
66
Err ( JsError :: new ( "To build an `OlmMachine`, please use the `initialize` method" ) )
@@ -87,17 +87,19 @@ impl OlmMachine {
87
87
///
88
88
/// * `store_passphrase` - The passphrase that should be used to encrypt the
89
89
/// IndexedDB-based store.
90
- pub async fn initialize (
90
+ #[ wasm_bindgen( unchecked_return_type = "Promise<OlmMachine>" ) ]
91
+ pub fn initialize (
91
92
user_id : & identifiers:: UserId ,
92
93
device_id : & identifiers:: DeviceId ,
93
94
store_name : Option < String > ,
94
95
store_passphrase : Option < String > ,
95
- ) -> Result < JsValue , JsValue > {
96
+ ) -> Promise {
96
97
let user_id = user_id. inner . clone ( ) ;
97
98
let device_id = device_id. inner . clone ( ) ;
98
-
99
- let store_handle = StoreHandle :: open ( store_name, store_passphrase) . await ?;
100
- Self :: init_helper ( user_id, device_id, store_handle) . await
99
+ future_to_promise ( async {
100
+ let store_handle = StoreHandle :: open ( store_name, store_passphrase) . await ?;
101
+ Self :: init_helper ( user_id, device_id, store_handle) . await
102
+ } )
101
103
}
102
104
103
105
/// Create a new `OlmMachine` backed by an existing store.
@@ -112,33 +114,34 @@ impl OlmMachine {
112
114
///
113
115
/// * `store_handle` - the connection to the crypto store to be used for
114
116
/// this machine.
115
- #[ wasm_bindgen( js_name = "initFromStore" ) ]
116
- pub async fn init_from_store (
117
+ #[ wasm_bindgen( js_name = "initFromStore" , unchecked_return_type = "Promise<OlmMachine>" ) ]
118
+ pub fn init_from_store (
117
119
user_id : & identifiers:: UserId ,
118
120
device_id : & identifiers:: DeviceId ,
119
121
store_handle : & StoreHandle ,
120
- ) -> Result < JsValue , JsValue > {
122
+ ) -> Promise {
121
123
let user_id = user_id. inner . clone ( ) ;
122
124
let device_id = device_id. inner . clone ( ) ;
123
- Self :: init_helper ( user_id, device_id, store_handle. clone ( ) ) . await
125
+ let store_handle = store_handle. clone ( ) ;
126
+ future_to_promise ( async move {
127
+ Self :: init_helper ( user_id, device_id, store_handle) . await
128
+ } )
124
129
}
125
130
126
131
async fn init_helper (
127
132
user_id : OwnedUserId ,
128
133
device_id : OwnedDeviceId ,
129
134
store_handle : StoreHandle ,
130
- ) -> Result < JsValue , JsValue > {
135
+ ) -> Result < OlmMachine , JsError > {
131
136
Ok ( OlmMachine {
132
137
inner : matrix_sdk_crypto:: OlmMachine :: with_store (
133
138
user_id. as_ref ( ) ,
134
139
device_id. as_ref ( ) ,
135
140
store_handle,
136
141
None ,
137
142
)
138
- . await
139
- . map_err ( JsError :: from) ?,
140
- }
141
- . into ( ) )
143
+ . await ?,
144
+ } )
142
145
}
143
146
144
147
/// The unique user ID that owns this `OlmMachine` instance.
@@ -266,10 +269,13 @@ impl OlmMachine {
266
269
///
267
270
/// All users *whose device lists we are tracking* are flagged as needing a
268
271
/// key query. Users whose devices we are not tracking are ignored.
269
- #[ wasm_bindgen( js_name = "markAllTrackedUsersAsDirty" ) ]
270
- pub async fn mark_all_tracked_users_as_dirty ( & self ) -> Result < ( ) , JsError > {
271
- self . inner . mark_all_tracked_users_as_dirty ( ) . await ?;
272
- Ok ( ( ) )
272
+ #[ wasm_bindgen( js_name = "markAllTrackedUsersAsDirty" , unchecked_return_type = "Promise<void>" ) ]
273
+ pub fn mark_all_tracked_users_as_dirty ( & self ) -> Promise {
274
+ let me = self . inner . clone ( ) ;
275
+ future_to_promise ( async move {
276
+ me. mark_all_tracked_users_as_dirty ( ) . await ?;
277
+ Ok ( JsValue :: UNDEFINED )
278
+ } )
273
279
}
274
280
275
281
/// Handle to-device events and one-time key counts from a sync
@@ -577,9 +583,15 @@ impl OlmMachine {
577
583
///
578
584
/// **Warning**: Only export this and share it with a trusted recipient,
579
585
/// i.e. if an existing device is sharing this with a new device.
580
- #[ wasm_bindgen( js_name = "exportSecretsBundle" ) ]
581
- pub async fn export_secrets_bundle ( & self ) -> Result < store:: SecretsBundle , JsError > {
582
- Ok ( self . inner . store ( ) . export_secrets_bundle ( ) . await ?. into ( ) )
586
+ #[ wasm_bindgen(
587
+ js_name = "exportSecretsBundle" ,
588
+ unchecked_return_type = "Promise<SecretsBundle>"
589
+ ) ]
590
+ pub async fn export_secrets_bundle ( & self ) -> Promise {
591
+ let me = self . inner . clone ( ) ;
592
+ future_to_promise ( async move {
593
+ Ok ( store:: SecretsBundle :: from ( me. store ( ) . export_secrets_bundle ( ) . await ?) )
594
+ } )
583
595
}
584
596
585
597
/// Import and persists secrets from a {@link SecretsBundle}.
@@ -597,10 +609,13 @@ impl OlmMachine {
597
609
///
598
610
/// The provided `SecretsBundle` is freed by this method; be careful not to
599
611
/// use it once this method has been called.
600
- #[ wasm_bindgen( js_name = "importSecretsBundle" ) ]
601
- pub async fn import_secrets_bundle ( & self , bundle : store:: SecretsBundle ) -> Result < ( ) , JsError > {
602
- self . inner . store ( ) . import_secrets_bundle ( & bundle. inner ) . await ?;
603
- Ok ( ( ) )
612
+ #[ wasm_bindgen( js_name = "importSecretsBundle" , unchecked_return_type = "Promise<void>" ) ]
613
+ pub fn import_secrets_bundle ( & self , bundle : store:: SecretsBundle ) -> Promise {
614
+ let me = self . inner . clone ( ) ;
615
+ future_to_promise ( async move {
616
+ me. store ( ) . import_secrets_bundle ( & bundle. inner ) . await ?;
617
+ Ok ( JsValue :: UNDEFINED )
618
+ } )
604
619
}
605
620
606
621
/// Export all the private cross signing keys we have.
@@ -1550,13 +1565,17 @@ impl OlmMachine {
1550
1565
/// # Returns
1551
1566
///
1552
1567
/// `Promise<RoomSettings|undefined>`
1553
- #[ wasm_bindgen( js_name = "getRoomSettings" ) ]
1554
- pub async fn get_room_settings (
1555
- & self ,
1556
- room_id : & identifiers:: RoomId ,
1557
- ) -> Result < JsValue , JsError > {
1558
- let result = self . inner . room_settings ( & room_id. inner ) . await ?;
1559
- Ok ( result. map ( RoomSettings :: from) . into ( ) )
1568
+ #[ wasm_bindgen(
1569
+ js_name = "getRoomSettings" ,
1570
+ unchecked_return_type = "Promise<RoomSettings|undefined>"
1571
+ ) ]
1572
+ pub async fn get_room_settings ( & self , room_id : & identifiers:: RoomId ) -> Promise {
1573
+ let me = self . inner . clone ( ) ;
1574
+ let room = room_id. inner . clone ( ) ;
1575
+ future_to_promise ( async move {
1576
+ let result = me. room_settings ( & room) . await ?;
1577
+ Ok ( result. map ( RoomSettings :: from) )
1578
+ } )
1560
1579
}
1561
1580
1562
1581
/// Store encryption settings for the given room.
@@ -1568,14 +1587,19 @@ impl OlmMachine {
1568
1587
/// If the settings are valid, they will be persisted to the crypto store.
1569
1588
/// These settings are not used directly by this library, but the saved
1570
1589
/// settings can be retrieved via {@link getRoomSettings}.
1571
- #[ wasm_bindgen( js_name = "setRoomSettings" ) ]
1572
- pub async fn set_room_settings (
1590
+ #[ wasm_bindgen( js_name = "setRoomSettings" , unchecked_return_type = "Promise<void>" ) ]
1591
+ pub fn set_room_settings (
1573
1592
& self ,
1574
1593
room_id : & identifiers:: RoomId ,
1575
1594
room_settings : & RoomSettings ,
1576
- ) -> Result < ( ) , JsError > {
1577
- self . inner . set_room_settings ( & room_id. inner , & room_settings. into ( ) ) . await ?;
1578
- Ok ( ( ) )
1595
+ ) -> Promise {
1596
+ let me = self . inner . clone ( ) ;
1597
+ let room = room_id. inner . clone ( ) ;
1598
+ let room_settings = room_settings. into ( ) ;
1599
+ future_to_promise ( async move {
1600
+ me. set_room_settings ( & room, & room_settings) . await ?;
1601
+ Ok ( JsValue :: UNDEFINED )
1602
+ } )
1579
1603
}
1580
1604
1581
1605
/// Manage dehydrated devices
0 commit comments