-
Notifications
You must be signed in to change notification settings - Fork 19
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
Wire up "payment channel updated" events stream to demo #1801
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
44cb3d0
try swapping out queries for event binding
geoknee c67e1b7
use a combination of queries and event listeners
geoknee 7c6e3f5
silence errors
geoknee 5aed7bf
use onPaymentChannelUpdated
geoknee bbf0d40
fix typo
geoknee 09617eb
really fix typo
geoknee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,14 +116,6 @@ export default function App() { | |
.finally(() => console.timeEnd("Connect to Nitro Node")); | ||
}, [url]); | ||
|
||
const updateChannelInfo = async (channelId: string) => { | ||
if (channelId == "") { | ||
throw new Error("Empty channel id provided"); | ||
} | ||
const paymentChannel = await nitroClient?.GetPaymentChannel(channelId); | ||
setPaymentChannelInfo(paymentChannel); | ||
}; | ||
|
||
const triggerFileDownload = (file: File) => { | ||
// This will prompt the browser to download the file | ||
const blob = new Blob([file], { type: file.type }); | ||
|
@@ -149,15 +141,21 @@ export default function App() { | |
// await nitroClient.WaitForObjective(result.Id); | ||
|
||
setPaymentChannelId(result.ChannelId); | ||
updateChannelInfo(result.ChannelId); | ||
console.timeEnd("Create Payment Channel"); | ||
|
||
// TODO: Slightly hacky but we wait a beat before querying so we see the updated balance | ||
setTimeout(() => { | ||
updateChannelInfo(result.ChannelId); | ||
}, 1000); | ||
}; | ||
nitroClient.onPaymentChannelUpdated( | ||
result.ChannelId, | ||
setPaymentChannelInfo | ||
); | ||
|
||
// It's possible the channel updated before we registered the handler above, so | ||
// query the channel once now to get the latest information: | ||
|
||
setPaymentChannelInfo( | ||
await nitroClient?.GetPaymentChannel(result.ChannelId) | ||
); | ||
|
||
console.timeEnd("Create Payment Channel"); | ||
}; | ||
const fetchAndDownloadFile = async () => { | ||
setErrorText(""); | ||
setFetchInProgress(true); | ||
|
@@ -182,25 +180,16 @@ export default function App() { | |
nitroClient, | ||
(progress) => { | ||
setDownloadProgress(progress); | ||
updateChannelInfo(paymentChannelInfo.ID); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the nub of the change. Instead of reaching out with http queries each time we think the channel will have changed, we just listen for events being pushed to us. This should cut down on a bit of latency, I think (round trip vs half round trip). |
||
} | ||
) | ||
: await fetchFile( | ||
selectedFile.url, | ||
skipPayment ? 0 : costPerByte * selectedFile.size, | ||
paymentChannelInfo.ID, | ||
nitroClient, | ||
() => { | ||
updateChannelInfo(paymentChannelInfo.ID); | ||
} | ||
nitroClient | ||
); | ||
setDownloadProgress(100); | ||
triggerFileDownload(file); | ||
|
||
// TODO: Slightly hacky but we wait a beat before querying so we see the updated balance | ||
setTimeout(() => { | ||
updateChannelInfo(paymentChannelInfo.ID); | ||
}, 50); | ||
} catch (e: unknown) { | ||
console.error(e); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty common issue and makes for pretty bad DX. Perhaps a smarter event queue could be used in place of an event emitter.