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
use crate::{instance, instance::AtomMut};
use std::{
cell::{Ref, RefMut},
sync::Arc,
};
thread_local! {
static ENGINE: Arc<instance::Engine> = Arc::new(instance::Engine::new());
}
pub fn batch() -> Batch {
ENGINE.with(|engine| Batch::new(engine.batch()))
}
pub fn react(f: impl FnMut() + 'static) {
ENGINE.with(|engine| engine.react(f))
}
#[must_use]
pub struct Batch {
#[allow(dead_code)]
inner: instance::Batch,
}
impl Batch {
pub fn new(inner: instance::Batch) -> Self {
Batch { inner }
}
}
pub struct Atom<T> {
inner: instance::Atom<T>,
}
impl<T: 'static> Atom<T> {
pub fn new(initial_value: T) -> Self {
let engine = ENGINE.with(<_>::clone);
Self {
inner: instance::Atom::new(engine, initial_value),
}
}
#[must_use]
pub fn get(&self) -> Ref<'_, T> {
self.inner.get()
}
#[must_use]
pub fn get_mut(&self) -> AtomMut<'_, T> {
self.inner.get_mut()
}
#[must_use]
pub fn sample_mut(&self) -> RefMut<'_, T> {
self.inner.sample_mut()
}
pub fn set(&self, value: T) {
self.inner.set(value);
}
}
impl<T: Default + 'static> Default for Atom<T> {
fn default() -> Self {
Self::new(T::default())
}
}
impl<T> Clone for Atom<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}