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

Wire up "payment channel updated" events stream to demo #1801

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
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
39 changes: 14 additions & 25 deletions packages/payment-proxy-client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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)
);
Comment on lines +150 to +155
Copy link
Contributor Author

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.


console.timeEnd("Create Payment Channel");
};
const fetchAndDownloadFile = async () => {
setErrorText("");
setFetchInProgress(true);
Expand All @@ -182,25 +180,16 @@ export default function App() {
nitroClient,
(progress) => {
setDownloadProgress(progress);
updateChannelInfo(paymentChannelInfo.ID);
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);

Expand Down
4 changes: 2 additions & 2 deletions packages/payment-proxy-client/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function fetchFile(
paymentAmount: number,
channelId: string,
nitroClient: NitroRpcClient,
updateChannelCallback: () => void
updateChannelCallback?: () => void
): Promise<File> {
console.time("Create Payment Vouncher");
const voucher = await nitroClient.CreateVoucher(channelId, paymentAmount);
Expand All @@ -15,7 +15,7 @@ export async function fetchFile(
console.time("Fetch file");
const req = createRequest(url, voucher);
const fetchPromise = fetch(req);
updateChannelCallback();
updateChannelCallback && updateChannelCallback();
const response = await fetchPromise;
console.timeEnd("Fetch file");
if (response.status != 200) {
Expand Down
Loading