From 2db8aa4823b728b1ea847b8c3427c5a4ae7b6182 Mon Sep 17 00:00:00 2001 From: Brord van Wierst Date: Wed, 6 Sep 2023 16:38:38 +0200 Subject: [PATCH] listen error in wasm, madelisten async again --- bindings/nodejs/lib/bindings.ts | 2 +- .../lib/wallet/wallet-method-handler.ts | 20 +++++++++---------- bindings/nodejs/lib/wallet/wallet.ts | 6 +++--- bindings/wasm/src/wallet.rs | 17 +++++++++------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/bindings/nodejs/lib/bindings.ts b/bindings/nodejs/lib/bindings.ts index 9b00a1ed61..7ab5f8fd21 100644 --- a/bindings/nodejs/lib/bindings.ts +++ b/bindings/nodejs/lib/bindings.ts @@ -116,8 +116,8 @@ export { callSecretManagerMethodAsync, callUtilsMethod, callWalletMethodAsync, - destroyWallet, listenWalletAsync, + destroyWallet, getClientFromWallet, getSecretManagerFromWallet, listenMqtt, diff --git a/bindings/nodejs/lib/wallet/wallet-method-handler.ts b/bindings/nodejs/lib/wallet/wallet-method-handler.ts index 2a8fd180a6..bc12b7468f 100644 --- a/bindings/nodejs/lib/wallet/wallet-method-handler.ts +++ b/bindings/nodejs/lib/wallet/wallet-method-handler.ts @@ -99,19 +99,17 @@ export class WalletMethodHandler { * @param eventTypes The wallet event types to listen for. * @param callback The callback function to call when an event is received. */ - listen( + async listen( eventTypes: WalletEventType[], callback: (error: Error, event: Event) => void, - ): void { - try { - listenWalletAsync(eventTypes, callback, this.methodHandler).catch( - (error: any) => { - throw errorHandle(error); - }, - ); - } catch (error: any) { + ): Promise { + return listenWalletAsync( + eventTypes, + callback, + this.methodHandler, + ).catch((error: any) => { throw errorHandle(error); - } + }); } /** @@ -131,7 +129,7 @@ export class WalletMethodHandler { */ getSecretManager(): SecretManager { try { - const result = getSecretManagerFromWallet(this.methodHandler); + let result = getSecretManagerFromWallet(this.methodHandler); return new SecretManager(result); } catch (error: any) { throw errorHandle(error); diff --git a/bindings/nodejs/lib/wallet/wallet.ts b/bindings/nodejs/lib/wallet/wallet.ts index 27f1c301a4..6522554841 100644 --- a/bindings/nodejs/lib/wallet/wallet.ts +++ b/bindings/nodejs/lib/wallet/wallet.ts @@ -199,11 +199,11 @@ export class Wallet { /** * Listen to wallet events with a callback. An empty array will listen to all possible events. */ - listen( + async listen( eventTypes: WalletEventType[], callback: (error: Error, event: Event) => void, - ): void { - this.methodHandler.listen(eventTypes, callback); + ): Promise { + return this.methodHandler.listen(eventTypes, callback); } /** diff --git a/bindings/wasm/src/wallet.rs b/bindings/wasm/src/wallet.rs index 2f417a5b33..4ee6128109 100644 --- a/bindings/wasm/src/wallet.rs +++ b/bindings/wasm/src/wallet.rs @@ -148,13 +148,16 @@ pub async fn listen_wallet( // Spawn on the same thread a continuous loop to check the channel wasm_bindgen_futures::spawn_local(async move { while let Some(wallet_event) = rx.recv().await { - callback - .call1( - &JsValue::NULL, - &JsValue::from(serde_json::to_string(&wallet_event).unwrap()), - ) - // Safe to unwrap, our callback has no return - .unwrap(); + let res = callback.call2( + &JsValue::NULL, + &JsValue::UNDEFINED, + &JsValue::from(serde_json::to_string(&wallet_event).unwrap()), + ); + // Call callback again with the error this time, to prevent wasm crashing. + // This does mean the callback is called a second time instead of once. + if let Err(e) = res { + callback.call2(&JsValue::NULL, &e, &JsValue::UNDEFINED).unwrap(); + } } // No more links to the unbounded_channel, exit loop });