Skip to content

Commit

Permalink
Merge pull request #1801 from statechannels/ts-payment-channel-update…
Browse files Browse the repository at this point in the history
…d-events-demo

Wire up "payment channel updated" events stream to demo
  • Loading branch information
lalexgap committed Oct 12, 2023
2 parents f081e1a + 09617eb commit 33b9e85
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
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)
);

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);
}
)
: 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

0 comments on commit 33b9e85

Please sign in to comment.