Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions src/Deposit.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export default class Deposit {
this.publicKeyPointToBitcoinAddress(point)
)

this.receivedFundingConfirmationEmitter = new EventEmitter()
this.eventEmitter = new EventEmitter()
}

// /------------------------------- Accessors -------------------------------
Expand Down Expand Up @@ -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
})
}
)

Expand Down Expand Up @@ -586,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 -----------------------------

/**
Expand Down Expand Up @@ -628,7 +653,7 @@ export default class Deposit {
* confirmations, and requiredConfirmations as its parameter.
*/
onReceivedFundingConfirmation(onReceivedFundingConfirmationHandler) {
this.receivedFundingConfirmationEmitter.on(
this.eventEmitter.on(
"receivedFundingConfirmation",
onReceivedFundingConfirmationHandler
)
Expand Down Expand Up @@ -1003,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}...`
Expand All @@ -1019,8 +1044,8 @@ export default class Deposit {
{},
true
)
}
)
})
.catch(this.notifyError)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not take an onError or similar parameter to autoSubmit? It could be optional if someone wanted to handle errors directly on the promise, and we could also emit the event.

In general, a hybrid mechanism where we move to promise workflow + events depending on what the consumer wants to use might be a nice transition for a future direction.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point. Handled in #119

}

return this.autoSubmittingState
Expand Down