From 5126f89d8bb35ff4b83f161ff218b741d9a36744 Mon Sep 17 00:00:00 2001 From: SpikeHD Date: Thu, 12 May 2022 22:25:07 -0700 Subject: [PATCH] stop downloads when removed --- src-tauri/src/downloader.rs | 5 +++++ src/ui/App.tsx | 4 ++-- src/utils/download.ts | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/downloader.rs b/src-tauri/src/downloader.rs index 86ce3dc..2a20143 100644 --- a/src-tauri/src/downloader.rs +++ b/src-tauri/src/downloader.rs @@ -50,6 +50,11 @@ pub async fn download_file(window: tauri::Window, url: &str, path: &str) -> Resu // Await chunks while let Some(item) = stream.next().await { + // Stop the loop if the download is removed from the list + if !DOWNLOADS.lock().unwrap().contains(&path.to_string()) { + break; + } + let chunk = match item { Ok(itm) => itm, Err(e) => { diff --git a/src/ui/App.tsx b/src/ui/App.tsx index acb1aeb..36a67bb 100644 --- a/src/ui/App.tsx +++ b/src/ui/App.tsx @@ -33,8 +33,8 @@ async function download(url: string, filename: string, path: string) { async function TESTDOWNLOAD() { download( - 'https://github.com/Grasscutters/Grasscutter/archive/refs/heads/stable.zip', - 'grasscutter.zip', + 'https://github.com/Koko-boya/Grasscutter_Resources/archive/refs/heads/main.zip', + 'resources.zip', 'S:\\Cultivation' ) } diff --git a/src/utils/download.ts b/src/utils/download.ts index b6c0fd7..ffffae6 100644 --- a/src/utils/download.ts +++ b/src/utils/download.ts @@ -89,13 +89,13 @@ export default class DownloadHandler { } getTotalAverage() { - const files = this.downloads.filter(d => d.status !== 'finished') + const files = this.downloads.filter(d => d.status === 'downloading') const total = files.reduce((acc, d) => acc + d.total, 0) const progress = files.reduce((acc, d) => acc + d.progress, 0) return { average: (progress / total) * 100 || 0, - files: this.downloads.filter(d => d.status !== 'finished' && d.status !== 'error').length, + files: this.downloads.filter(d => d.status === 'downloading').length, totalSize: total, } }