Skip to main content

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 `deactivate()`
24    /// (and `clear_signal_listeners_by_addr`) to make subsequent `set()` calls
25    /// complete no-ops (no value update, no listener invocation, no
26    /// `schedule_signal_update()`), ensuring stale closures like orphaned
27    /// `setInterval` handlers or pending `spawn_local` futures become harmless.
28    #[get(pub(crate), type(copy))]
29    #[get_mut(pub(crate))]
30    #[set(pub(crate))]
31    pub(crate) alive: bool,
32    /// IDs of dynamic nodes that depend on this signal for precise dirty marking.
33    /// When this signal changes, only these dynamic nodes are marked dirty
34    /// instead of broadcasting to all registered dynamic nodes.
35    #[debug(skip)]
36    #[get(pub(crate))]
37    #[get_mut(pub(crate))]
38    #[set(pub(crate))]
39    #[new(skip)]
40    pub(crate) dependents: Vec<usize>,
41    /// Flag indicating that `replace_subscribe` was called during
42    /// `update_and_notify`'s swap-out phase. When a listener callback
43    /// re-registers listeners via `replace_subscribe`, it intends to
44    /// replace all existing listeners — but the old listeners have
45    /// already been swapped out into the local variable. Without this
46    /// flag, `update_and_notify` would incorrectly merge the old
47    /// listeners back with the new ones, defeating `replace_subscribe`'s
48    /// replacement semantics and causing listener accumulation.
49    #[get(pub(crate), type(copy))]
50    #[get_mut(pub(crate))]
51    #[set(pub(crate))]
52    #[new(skip)]
53    pub(crate) listeners_replaced: bool,
54}
55
56/// A reactive signal handle.
57///
58/// Allows reading, writing, and subscribing to changes.
59/// Implements `Clone` and `Copy` for ergonomic use; all copies share the same
60/// underlying state. The inner state is heap-allocated via `Box` and accessed
61/// through a raw pointer stored as a `usize`. The allocation is tracked in a
62/// global registry for lifecycle management. The `Copy` semantics are safe
63/// because only the pointer address is copied — the actual heap allocation
64/// is owned by the registry.
65#[derive(CustomDebug, Data, Eq, New, PartialEq)]
66pub struct Signal<T>
67where
68    T: Clone + PartialEq + 'static,
69{
70    /// Address of the heap-allocated inner state (`*mut SignalInner<T>`).
71    #[debug(skip)]
72    #[get(pub(crate), type(copy))]
73    #[get_mut(pub(crate))]
74    #[set(pub(crate))]
75    pub(crate) inner: usize,
76    /// Marker for the generic type parameter (uses fn pointer to be `Copy`
77    /// regardless of `T`).
78    #[debug(skip)]
79    #[get(pub(crate), type(copy))]
80    #[get_mut(pub(crate))]
81    #[set(pub(crate))]
82    pub(crate) _marker: PhantomData<fn() -> T>,
83}
84
85/// A `Sync` wrapper for single-threaded global `Signal` access.
86///
87/// SAFETY: This type is only safe to use in single-threaded contexts
88/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
89/// variable, but concurrent access from multiple threads would be
90/// undefined behavior.
91#[derive(CustomDebug, Data, New)]
92pub struct SignalCell<T>
93where
94    T: Clone + PartialEq + 'static,
95{
96    /// Interior-mutable storage for an optional signal handle.
97    #[debug(skip)]
98    #[get(pub(crate))]
99    #[get_mut(pub(crate))]
100    #[set(pub(crate))]
101    pub(crate) inner: UnsafeCell<Option<Signal<T>>>,
102}
103
104/// A `Sync` wrapper for single-threaded global `HashSet` access.
105///
106/// SAFETY: This type is only safe to use in single-threaded contexts
107/// (e.g., WASM). It implements `Sync` to allow usage as a `static mut`
108/// variable, but concurrent access from multiple threads would be
109/// undefined behavior.
110#[derive(Data, Debug, New)]
111pub(crate) struct SignalInnerRegistryCell(
112    /// Interior-mutable storage for the signal inner registry.
113    #[get(pub(crate))]
114    #[get_mut(pub(crate))]
115    #[set(pub(crate))]
116    pub(crate) UnsafeCell<Option<HashSet<usize>>>,
117);