summaryrefslogtreecommitdiffstats
path: root/src/common.rs
diff options
context:
space:
mode:
authorSebastian Thiel <sebastian.thiel@icloud.com>2023-05-02 10:55:47 +0200
committerSebastian Thiel <sebastian.thiel@icloud.com>2023-05-05 12:28:10 +0200
commitc921dc72d3008179e72df9d85f0e0c21c998e199 (patch)
tree55ae29f3639ba73f891143374ec83d66020b6b01 /src/common.rs
parente03c560e8b54e2e231d578e1d5e9dcd206d34216 (diff)
Simplify GUI refreshes by using a throttle
Diffstat (limited to 'src/common.rs')
-rw-r--r--src/common.rs13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/common.rs b/src/common.rs
index a277c1f..ab71426 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -124,14 +124,16 @@ pub struct Throttle {
}
impl Throttle {
- pub fn new(duration: Duration) -> Self {
+ pub fn new(duration: Duration, initial_sleep: Option<Duration>) -> Self {
let instance = Self {
trigger: Default::default(),
};
let trigger = Arc::downgrade(&instance.trigger);
std::thread::spawn(move || {
- std::thread::sleep(Duration::from_secs(1));
+ if let Some(duration) = initial_sleep {
+ std::thread::sleep(duration)
+ }
while let Some(t) = trigger.upgrade() {
t.store(true, Ordering::Relaxed);
std::thread::sleep(duration);
@@ -145,10 +147,15 @@ impl Throttle {
where
F: FnOnce(),
{
- if self.trigger.swap(false, Ordering::Relaxed) {
+ if self.can_update() {
f()
}
}
+
+ /// Return `true` if we are not currently throttled.
+ pub fn can_update(&self) -> bool {
+ self.trigger.swap(false, Ordering::Relaxed)
+ }
}
/// Configures a filesystem walk, including output and formatting options.