Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace recursion with promise sequence in useInvoiceable #752

Merged
merged 7 commits into from
Jan 17, 2024

Conversation

ekzyis
Copy link
Member

@ekzyis ekzyis commented Jan 15, 2024

Commit description:

This commit refactors useInvoicable. The hard-to-follow recursion was replaced by awaiting promises which resolve or reject when one step of our JIT invoice flow is done.

Therefore, onSubmit is now fully agnostic of JIT invoices. The handler only returns when payment + action was successful or canceled - just like when a custodial zap was successful.

This was done while working on #749 since we will heavily rely on JIT invoices there and thus this was a good time to tackle this TODO:

// TODO: refactor this so it can be easily understood
// there's lots of state cascading paired with logic
// independent of the state, and it's hard to follow

I think this is much better now. There is no recursion anymore.

Keeping in draft since I want to sleep over this and do another review and test tomorrow.


update: this should be stable now. video

@ekzyis ekzyis marked this pull request as draft January 15, 2024 02:28
This commit refactors `useInvoicable`. The hard-to-follow recursion was replaced by awaiting promises which resolve or reject when one step of our JIT invoice flow is done.

Therefore, `onSubmit` is now fully agnostic of JIT invoices. The handler only returns when payment + action was successful or canceled - just like when a custodial zap was successful.
* move invoice cancel logic into hook where invoice is also created
* fix missing invoice cancellation if user closes modal or goes back.
* refactor promise logic: it makes more sense to wrap the payment promise with the modal promise than the other way around.
@ekzyis ekzyis marked this pull request as ready for review January 16, 2024 01:27
@@ -33,6 +33,7 @@ export default function useModal () {
}
const previousModalContent = modalStack[modalStack.length - 1]
setModalStack(modalStack.slice(0, -1))
modalOptions?.onClose?.()
Copy link
Member Author

Choose a reason for hiding this comment

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

This is required to also cancel HODL invoices in case the user clicks on <- and already paid:

2024-01-16-023623_524x702_scrot

Copy link
Member

@huumn huumn left a comment

Choose a reason for hiding this comment

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

Dude so impressed! Excellent use of promises and very clean and clear overall.

I have one nit, but we can ignore it if you want.

await onRetry()
} catch (err) {
console.error('retry error:', err)
setRetryError(retryError + 1)
Copy link
Member

Choose a reason for hiding this comment

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

nit: if you're referencing prior state, you usually want to use the function form of setState:

setRetryError(retryError => retryError + 1)

@ekzyis
Copy link
Member Author

ekzyis commented Jan 17, 2024

Dude so impressed! Excellent use of promises and very clean and clear overall.

Thank you :)

Key takeaway from this PR for me: Don't ignore the eslint rule no-async-promise-executor.

I spent hours debugging why my errors are not getting caught by the try/catch in the calling context until I found this.

TL;DR If you use an async promise executor, you essentially create a detached promise with no error handlers attached iiuc.

Some warnings really make sense.

@huumn huumn merged commit 9ef0c81 into master Jan 17, 2024
1 check passed
@ekzyis ekzyis deleted the 533-refactor-invoiceable branch February 14, 2024 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants