cope/singleton/
mod.rs

1use crate::{instance, instance::AtomMut};
2use std::{
3    cell::{Ref, RefMut},
4    sync::Arc,
5};
6
7thread_local! {
8    static ENGINE: Arc<instance::Engine> = Arc::new(instance::Engine::new());
9}
10
11pub fn batch() -> Batch {
12    ENGINE.with(|engine| Batch::new(engine.batch()))
13}
14
15pub fn react(f: impl FnMut() + 'static) {
16    ENGINE.with(|engine| engine.react(f))
17}
18
19#[must_use]
20pub struct Batch {
21    #[allow(dead_code)] // This is only here to be dropped
22    inner: instance::Batch,
23}
24
25impl Batch {
26    pub fn new(inner: instance::Batch) -> Self {
27        Batch { inner }
28    }
29}
30
31pub struct Atom<T> {
32    inner: instance::Atom<T>,
33}
34
35impl<T: 'static> Atom<T> {
36    pub fn new(initial_value: T) -> Self {
37        let engine = ENGINE.with(<_>::clone);
38        Self {
39            inner: instance::Atom::new(engine, initial_value),
40        }
41    }
42
43    #[must_use]
44    pub fn get(&self) -> Ref<'_, T> {
45        self.inner.get()
46    }
47
48    #[must_use]
49    pub fn get_mut(&self) -> AtomMut<'_, T> {
50        self.inner.get_mut()
51    }
52
53    #[must_use]
54    pub fn sample_mut(&self) -> RefMut<'_, T> {
55        self.inner.sample_mut()
56    }
57
58    pub fn set(&self, value: T) {
59        self.inner.set(value);
60    }
61}
62
63impl<T: Default + 'static> Default for Atom<T> {
64    fn default() -> Self {
65        Self::new(T::default())
66    }
67}
68
69impl<T> Clone for Atom<T> {
70    fn clone(&self) -> Self {
71        Self {
72            inner: self.inner.clone(),
73        }
74    }
75}