euv_core/reactive/signal/struct.rs
1use crate::*;
2
3/// Inner state of a signal, holding the value and subscribed listeners.
4///
5/// This struct is not exposed directly; use `Signal` instead.
6#[derive(CustomDebug, Data, New)]
7pub(crate) struct SignalInner<T>
8where
9 T: Clone,
10{
11 /// The current value of the signal.
12 #[debug(skip)]
13 #[get(pub(crate))]
14 #[get_mut(pub(crate))]
15 #[set(pub(crate))]
16 pub(crate) value: T,
17 /// Callbacks to invoke when the value changes.
18 #[debug(skip)]
19 #[get(pub(crate))]
20 #[get_mut(pub(crate))]
21 #[set(pub(crate))]
22 pub(crate) listeners: Vec<Box<dyn FnMut()>>,
23 /// Whether this signal is still active. Set to `false` by `clear_listeners()`
24 /// to make subsequent `set()` calls complete no-ops (no value update, no
25 /// listener invocation, no `schedule_signal_update()`), ensuring stale
26 /// closures like orphaned `setInterval` handlers become harmless.
27 #[get(pub(crate), type(copy))]
28 #[get_mut(pub(crate))]
29 #[set(pub(crate))]
30 pub(crate) alive: bool,
31}
32
33/// A reactive signal handle.
34///
35/// Allows reading, writing, and subscribing to changes.
36/// Implements `Clone` and `Copy` for ergonomic use; all copies share the same
37/// underlying state. The inner state is heap-allocated via `Rc<RefCell<>>` and
38/// tracked in a global registry to prevent premature deallocation. The `Copy`
39/// semantics are safe because only the pointer address is copied — the actual
40/// `Rc` reference is held by the registry for the lifetime of the program.
41#[derive(CustomDebug, Data, Default, Eq, New, PartialEq)]
42pub struct Signal<T>
43where
44 T: Clone + PartialEq + 'static,
45{
46 /// Address of the heap-allocated inner state.
47 #[debug(skip)]
48 #[get(pub(crate), type(copy))]
49 #[get_mut(pub(crate))]
50 #[set(pub(crate))]
51 pub(crate) inner: usize,
52 /// Marker for the generic type parameter (uses fn pointer to be `Copy`
53 /// regardless of `T`).
54 #[debug(skip)]
55 #[get(pub(crate), type(copy))]
56 #[get_mut(pub(crate))]
57 #[set(pub(crate))]
58 pub(crate) _marker: std::marker::PhantomData<fn() -> T>,
59}
60
61/// A `Sync` wrapper for single-threaded global `Signal` access.
62///
63/// SAFETY: This type is only safe to use in single-threaded contexts
64/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
65/// variable, but concurrent access from multiple threads would be
66/// undefined behavior.
67#[derive(CustomDebug, Data, New)]
68pub struct SignalCell<T>
69where
70 T: Clone + PartialEq + 'static,
71{
72 /// Interior-mutable storage for an optional signal handle.
73 #[debug(skip)]
74 #[get(pub(crate))]
75 #[get_mut(pub(crate))]
76 #[set(pub(crate))]
77 pub(crate) inner: UnsafeCell<Option<Signal<T>>>,
78}
79
80/// A `Sync` wrapper for single-threaded global `HashMap` access.
81///
82/// SAFETY: This type is only safe to use in single-threaded contexts
83/// (e.g., WASM). It implements `Sync` to allow usage as a `static mut`
84/// variable, but concurrent access from multiple threads would be
85/// undefined behavior.
86#[derive(Data, Debug, New)]
87pub(crate) struct SignalInnerRegistryCell(
88 /// Interior-mutable storage for the signal inner registry.
89 #[get(pub(crate))]
90 #[get_mut(pub(crate))]
91 #[set(pub(crate))]
92 pub(crate) UnsafeCell<Option<HashMap<usize, Rc<dyn Any>>>>,
93);