diff --git a/src-tauri/src/download_manager/mod.rs b/src-tauri/src/download_manager/mod.rs index 0d664dd..b5bc6b0 100644 --- a/src-tauri/src/download_manager/mod.rs +++ b/src-tauri/src/download_manager/mod.rs @@ -6,3 +6,4 @@ pub mod downloadable; pub mod downloadable_metadata; pub mod progress_object; pub mod queue; +pub mod rolling_progress_updates; diff --git a/src-tauri/src/download_manager/progress_object.rs b/src-tauri/src/download_manager/progress_object.rs index 5d65207..262269b 100644 --- a/src-tauri/src/download_manager/progress_object.rs +++ b/src-tauri/src/download_manager/progress_object.rs @@ -76,7 +76,7 @@ impl ProgressObject { if current_amount >= to_update { self.points_towards_update .fetch_sub(to_update, Ordering::Relaxed); - update_queue(&self); + update_queue(self); } let last_update = self.last_update.read().unwrap(); @@ -102,7 +102,7 @@ impl ProgressObject { let remaining = max - current_amount; // bytes let time_remaining = (remaining / 1000) / kilobytes_per_second.max(1); - update_ui(&self, kilobytes_per_second, time_remaining); + update_ui(self, kilobytes_per_second, time_remaining); } } @@ -156,4 +156,3 @@ fn update_queue(progress: &ProgressObject) { .send(DownloadManagerSignal::UpdateUIQueue) .unwrap(); } - diff --git a/src-tauri/src/download_manager/rolling_progress_updates.rs b/src-tauri/src/download_manager/rolling_progress_updates.rs new file mode 100644 index 0000000..99e8773 --- /dev/null +++ b/src-tauri/src/download_manager/rolling_progress_updates.rs @@ -0,0 +1,20 @@ +use std::sync::{ + atomic::{AtomicUsize, Ordering}, + Arc, +}; + +#[derive(Clone)] +pub struct RollingProgressWindow { + window: Arc<[AtomicUsize; S]>, + current: Arc, +} +impl RollingProgressWindow { + pub fn update(&self, kilobytes_per_second: usize) { + let index = self.current.fetch_add(1, Ordering::SeqCst); + let current = &self.window[index % S]; + current.store(kilobytes_per_second, Ordering::Release); + } + pub fn get_average(&self) -> usize { + self.window.iter().map(|x| x.load(Ordering::Relaxed)).sum() + } +}