Skip to content

Commit

Permalink
listen error in wasm, madelisten async again
Browse files Browse the repository at this point in the history
  • Loading branch information
Brord van Wierst committed Sep 6, 2023
1 parent 25d9aa4 commit 2db8aa4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion bindings/nodejs/lib/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export {
callSecretManagerMethodAsync,
callUtilsMethod,
callWalletMethodAsync,
destroyWallet,
listenWalletAsync,
destroyWallet,
getClientFromWallet,
getSecretManagerFromWallet,
listenMqtt,
Expand Down
20 changes: 9 additions & 11 deletions bindings/nodejs/lib/wallet/wallet-method-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
return listenWalletAsync(
eventTypes,
callback,
this.methodHandler,
).catch((error: any) => {
throw errorHandle(error);
}
});
}

/**
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions bindings/nodejs/lib/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
return this.methodHandler.listen(eventTypes, callback);
}

/**
Expand Down
17 changes: 10 additions & 7 deletions bindings/wasm/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down

0 comments on commit 2db8aa4

Please sign in to comment.