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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::instance::engine::Engine;
use std::{
    cell::{Ref, RefCell, RefMut},
    ops::{Deref, DerefMut},
    sync::Arc,
};

type SubscriptionList = Vec<Arc<RefCell<Vec<Subscription>>>>;
type Subscription = Arc<RefCell<dyn FnMut()>>;

pub struct Atom<T> {
    engine: Arc<Engine>,
    value: Arc<RefCell<T>>,
    subscriptions: Arc<RefCell<SubscriptionList>>,
}

impl<T: 'static> Atom<T> {
    pub fn new(engine: Arc<Engine>, initial_value: T) -> Self {
        Self {
            engine,
            value: Arc::new(RefCell::new(initial_value)),
            subscriptions: Arc::new(RefCell::new(Vec::new())),
        }
    }

    #[must_use]
    pub fn get(&self) -> Ref<'_, T> {
        self.engine.track(&self.subscriptions);
        self.value.borrow()
    }

    #[must_use]
    pub fn get_mut(&self) -> AtomMut<'_, T> {
        AtomMut {
            value: Some(self.value.borrow_mut()),
            subscriptions: self.subscriptions.clone(),
        }
    }

    #[must_use]
    pub fn sample_mut(&self) -> RefMut<'_, T> {
        self.value.borrow_mut()
    }

    pub fn set(&self, value: T) {
        *self.get_mut() = value;
    }
}

impl<T> Clone for Atom<T> {
    fn clone(&self) -> Self {
        Self {
            engine: self.engine.clone(),
            value: self.value.clone(),
            subscriptions: self.subscriptions.clone(),
        }
    }
}

#[allow(clippy::module_name_repetitions)]
pub struct AtomMut<'a, T> {
    // Option dance
    value: Option<RefMut<'a, T>>,
    subscriptions: Arc<RefCell<SubscriptionList>>,
}

impl<T> Deref for AtomMut<'_, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &*self.value.as_ref().unwrap()
    }
}

impl<T> DerefMut for AtomMut<'_, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut *self.value.as_mut().unwrap()
    }
}

impl<T> Drop for AtomMut<'_, T> {
    fn drop(&mut self) {
        drop(self.value.take());

        for subscriptions in self.subscriptions.borrow().iter() {
            for subscription in subscriptions.borrow_mut().iter_mut() {
                let mut func = subscription.borrow_mut();
                // https://github.com/rust-lang/rust/issues/51886
                (&mut *func)();
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::instance::{Atom, Engine};
    use std::sync::Arc;

    #[test]
    fn get_initial_value() {
        let engine = Arc::new(Engine::new());
        let atom = Atom::new(engine, 123);
        assert_eq!(*atom.get(), 123);
    }

    #[test]
    fn set() {
        let engine = Arc::new(Engine::new());
        let atom = Atom::new(engine, 0);
        atom.set(42);
        assert_eq!(*atom.get(), 42);
    }

    #[test]
    fn mutate() {
        let engine = Arc::new(Engine::new());
        let atom = Atom::new(engine, 10);
        *atom.get_mut() += 1;
        assert_eq!(*atom.get(), 11);
    }
}