From 4d808cec194faf1df325ae78c5db87b5b9fd8d76 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Tue, 16 Feb 2021 13:41:11 +0100 Subject: [PATCH 1/2] Gave generic name to event emitter in Deposit The event emitter was renamed to a generic name, so it won't be dedicated to just one type of event but it will be used for different types of events. --- src/Deposit.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Deposit.js b/src/Deposit.js index 0d8ad56..48fd148 100644 --- a/src/Deposit.js +++ b/src/Deposit.js @@ -432,7 +432,7 @@ export default class Deposit { this.publicKeyPointToBitcoinAddress(point) ) - this.receivedFundingConfirmationEmitter = new EventEmitter() + this.eventEmitter = new EventEmitter() } // /------------------------------- Accessors ------------------------------- @@ -507,14 +507,11 @@ export default class Deposit { transaction.transactionID, requiredConfirmations, ({ transactionID, confirmations, requiredConfirmations }) => { - this.receivedFundingConfirmationEmitter.emit( - "receivedFundingConfirmation", - { - transactionID, - confirmations, - requiredConfirmations - } - ) + this.eventEmitter.emit("receivedFundingConfirmation", { + transactionID, + confirmations, + requiredConfirmations + }) } ) @@ -628,7 +625,7 @@ export default class Deposit { * confirmations, and requiredConfirmations as its parameter. */ onReceivedFundingConfirmation(onReceivedFundingConfirmationHandler) { - this.receivedFundingConfirmationEmitter.on( + this.eventEmitter.on( "receivedFundingConfirmation", onReceivedFundingConfirmationHandler ) From e72df5e4696459edcdbf3aed113e22137c5e32c9 Mon Sep 17 00:00:00 2001 From: Jakub Nowakowski Date: Tue, 16 Feb 2021 13:41:51 +0100 Subject: [PATCH 2/2] Error handler for Deposit autoSubmit errors Previously when an error was returned from `provideBTCFundingProof` transaction in the `autoSubmit` flow an UnhandledPromiseRejectionWarning was logged, user of the library had no chance to handle this error. As autoSubmit happens asynchronously we need a way to bubble up the error. In this proposed approach we catch the error an emit it as an event. User is required to define error callback with `onError` function. --- src/Deposit.js | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Deposit.js b/src/Deposit.js index 48fd148..c22c090 100644 --- a/src/Deposit.js +++ b/src/Deposit.js @@ -583,6 +583,34 @@ export default class Deposit { ) } + // /---------------------------- Error Handler ----------------------------- + + /** + * Emits an event with an error. + * + * @param {Error} err Error to emit. + */ + notifyError(err) { + this.eventEmitter.emit("error", err) + } + + /** + * A handler that is notified whenever an error occurs. + * + * @callback OnErrorHandler + * @param {Error} err Error + */ + + /** + * Registers a handler for errors. + * + * @param {OnErrorHandler} onErrorHandler + * A handler that receives an error. + */ + onError(onErrorHandler) { + this.eventEmitter.on("error", onErrorHandler) + } + // /---------------------------- Event Handlers ----------------------------- /** @@ -1000,8 +1028,8 @@ export default class Deposit { this.autoSubmittingState = { fundingTransaction: this.fundingTransaction, fundingConfirmations: this.fundingConfirmations, - proofTransaction: this.fundingConfirmations.then( - async ({ transaction, requiredConfirmations }) => { + proofTransaction: this.fundingConfirmations + .then(async ({ transaction, requiredConfirmations }) => { console.debug( `Submitting funding proof to deposit ${this.address} for ` + `Bitcoin transaction ${transaction.transactionID}...` @@ -1016,8 +1044,8 @@ export default class Deposit { {}, true ) - } - ) + }) + .catch(this.notifyError) } return this.autoSubmittingState