From f0fc84d80cf025d22f4bbc6c46c991475e9de391 Mon Sep 17 00:00:00 2001 From: joshuaboud Date: Mon, 19 Jul 2021 17:34:54 -0300 Subject: [PATCH] have single upload manager that can be added to --- navigator/components/FileUpload.js | 2 +- navigator/components/FileUploadManager.js | 39 ++++++++++++++++------- navigator/components/NavDragDrop.js | 4 +-- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/navigator/components/FileUpload.js b/navigator/components/FileUpload.js index b52b521..0309a1b 100644 --- a/navigator/components/FileUpload.js +++ b/navigator/components/FileUpload.js @@ -44,6 +44,7 @@ export class FileUpload { this.timestamp = Date.now(); this.modal_prompt = new ModalPrompt(); this.using_webkit = true; + this.make_html_element(); } make_html_element() { @@ -121,7 +122,6 @@ export class FileUpload { } async upload() { - this.make_html_element(); this.proc = cockpit.spawn(["/usr/share/cockpit/navigator/scripts/write-chunks.py3", this.path], {err: "out", superuser: "try"}); this.proc.fail((e, data) => { this.reader.onload = () => {} diff --git a/navigator/components/FileUploadManager.js b/navigator/components/FileUploadManager.js index 3e8e46e..22e6f74 100644 --- a/navigator/components/FileUploadManager.js +++ b/navigator/components/FileUploadManager.js @@ -23,25 +23,40 @@ import {NavWindow} from "./NavWindow.js"; export class FileUploadManager { /** * - * @param {FileUpload[]} uploads * @param {NavWindow} nav_window_ref * @param {number} max_concurrent */ - constructor(uploads, nav_window_ref, max_concurrent = 10) { - this.remaining_uploads = uploads; - this.max_concurrent = Math.min(max_concurrent, uploads.length); - let start_next = this.kickoff = () => { + constructor(nav_window_ref, max_concurrent = 10) { + this.nav_window_ref = nav_window_ref; + this.running = 0; + this.remaining_uploads = []; + this.max_concurrent = max_concurrent; + this.start_next = () => { let next_upload = this.remaining_uploads.pop(); - next_upload?.upload?.(); - if (!this.remaining_uploads.length) - nav_window_ref.refresh(); + if (next_upload) { + next_upload?.upload?.(); + this.running++; + } } - this.remaining_uploads.forEach((upload) => {upload.done_hook = start_next}); } - start_uploads() { - for (let i = 0; i < this.max_concurrent; i++) { - this.kickoff(); + /** + * + * @param {...FileUpload} uploads + */ + add(...uploads) { + let done_hook = () => { + this.running--; + this.start_next(); + if (!this.running) + this.nav_window_ref.refresh(); + } + for (let upload of uploads) { + upload.done_hook = done_hook; + this.remaining_uploads.unshift(upload); + } + while (this.remaining_uploads.length && this.running < this.max_concurrent) { + this.start_next(); } } } \ No newline at end of file diff --git a/navigator/components/NavDragDrop.js b/navigator/components/NavDragDrop.js index cdc7faa..5b9e73c 100644 --- a/navigator/components/NavDragDrop.js +++ b/navigator/components/NavDragDrop.js @@ -36,6 +36,7 @@ export class NavDragDrop { this.drop_area = drop_area; this.nav_window_ref = nav_window_ref; this.modal_prompt = new ModalPrompt(); + this.upload_manager = new FileUploadManager(this.nav_window_ref, 10); } /** @@ -188,8 +189,7 @@ export class NavDragDrop { } uploads = await this.handle_conflicts(uploads); this.nav_window_ref.stop_load(); - let upload_manager = new FileUploadManager(uploads, this.nav_window_ref); - upload_manager.start_uploads(); + this.upload_manager.add(... uploads); break; default: this.drop_area.classList.remove("drag-enter");