chore(progress): Added rolling_progress_updates.rs

Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
quexeky
2025-01-16 17:23:07 +11:00
parent 7c3140e424
commit 9369ff14b8
3 changed files with 23 additions and 3 deletions

View File

@ -6,3 +6,4 @@ pub mod downloadable;
pub mod downloadable_metadata;
pub mod progress_object;
pub mod queue;
pub mod rolling_progress_updates;

View File

@ -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();
}

View File

@ -0,0 +1,20 @@
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
#[derive(Clone)]
pub struct RollingProgressWindow<const S: usize> {
window: Arc<[AtomicUsize; S]>,
current: Arc<AtomicUsize>,
}
impl<const S: usize> RollingProgressWindow<S> {
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()
}
}