From 530058d12121816073f1cd2a205e4c93d52084d8 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Thu, 27 Oct 2022 08:58:31 +0200 Subject: handle request cancellation --- client-web/source/resource/file.ts | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'client-web/source/resource/file.ts') diff --git a/client-web/source/resource/file.ts b/client-web/source/resource/file.ts index 2b4c7fc..97da70b 100644 --- a/client-web/source/resource/file.ts +++ b/client-web/source/resource/file.ts @@ -31,15 +31,30 @@ export const resource_file: ResourceHandlerDecl = { on_statechange(_s) { }, on_enable(channel, disable) { if (!(channel instanceof RTCDataChannel)) throw new Error("not a data channel"); - const download = StreamDownload( - info.size!, info.label ?? "file", - position => { - display.status = `${display_filesize(position)} / ${display_filesize(info.size!)}` - } - ); const display = transfer_status_el() this.el.appendChild(display.el) + const reset = () => { + download_button.disabled = false + download_button.textContent = "Download again" + this.el.removeChild(display.el) + disable() + } + + const download = StreamDownload( + { + size: info.size!, + filename: info.label ?? "file", + progress(position) { + display.status = `${display_filesize(position)} / ${display_filesize(info.size!)}` + }, + cancel() { + channel.close() + log({ scope: "*", warn: true }, "download stream aborted") + reset() + } + } + ); channel.onopen = _ev => { log("dc", `${user.display_name}: channel open`); @@ -49,11 +64,8 @@ export const resource_file: ResourceHandlerDecl = { } channel.onclose = _ev => { log("dc", `${user.display_name}: channel closed`); - this.el.removeChild(display.el) download.close() - download_button.disabled = false - download_button.textContent = "Download" - disable() + reset() } channel.onmessage = ev => { // console.log(ev.data); -- cgit v1.2.3-70-g09d2