mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 16:51:18 +10:00
chore(progress): Added rolling_progress_updates.rs
Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
@ -6,3 +6,4 @@ pub mod downloadable;
|
||||
pub mod downloadable_metadata;
|
||||
pub mod progress_object;
|
||||
pub mod queue;
|
||||
pub mod rolling_progress_updates;
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
20
src-tauri/src/download_manager/rolling_progress_updates.rs
Normal file
20
src-tauri/src/download_manager/rolling_progress_updates.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user