File size: 1,559 Bytes
84d2a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::future::{self, Future};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

use tokio::sync::watch;

#[derive(Clone, Debug)]
pub struct UpdateTracker {
    update_operations: Arc<AtomicUsize>,
    update_notifier: Arc<watch::Sender<()>>,
}

impl Default for UpdateTracker {
    fn default() -> Self {
        let (update_notifier, _) = watch::channel(());

        Self {
            update_operations: Default::default(),
            update_notifier: Arc::new(update_notifier),
        }
    }
}

impl UpdateTracker {
    pub fn is_update_in_progress(&self) -> bool {
        self.update_operations.load(Ordering::Relaxed) > 0
    }

    pub fn watch_for_update(&self) -> impl Future<Output = ()> {
        let mut update_subscriber = self.update_notifier.subscribe();

        async move {
            match update_subscriber.changed().await {
                Ok(()) => (),
                Err(_) => future::pending().await,
            }
        }
    }

    pub fn update(&self) -> UpdateGuard {
        if self.update_operations.fetch_add(1, Ordering::Relaxed) == 0 {
            self.update_notifier.send_replace(());
        }

        UpdateGuard::new(self.update_operations.clone())
    }
}

#[derive(Debug)]
pub struct UpdateGuard {
    update_operations: Arc<AtomicUsize>,
}

impl UpdateGuard {
    fn new(update_operations: Arc<AtomicUsize>) -> Self {
        Self { update_operations }
    }
}

impl Drop for UpdateGuard {
    fn drop(&mut self) {
        self.update_operations.fetch_sub(1, Ordering::Relaxed);
    }
}