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}
42
43/// A reactive signal handle.
44///
45/// Allows reading, writing, and subscribing to changes.
46/// Implements `Clone` and `Copy` for ergonomic use; all copies share the same
47/// underlying state. The inner state is heap-allocated via `Box` and accessed
48/// through a raw pointer stored as a `usize`. The allocation is tracked in a
49/// global registry for lifecycle management. The `Copy` semantics are safe
50/// because only the pointer address is copied — the actual heap allocation
51/// is owned by the registry.
52#[derive(CustomDebug, Data, Eq, New, PartialEq)]
53pub struct Signal<T>
54where
55 T: Clone + PartialEq + 'static,
56{
57 /// Address of the heap-allocated inner state (`*mut SignalInner<T>`).
58 #[debug(skip)]
59 #[get(pub(crate), type(copy))]
60 #[get_mut(pub(crate))]
61 #[set(pub(crate))]
62 pub(crate) inner: usize,
63 /// Marker for the generic type parameter (uses fn pointer to be `Copy`
64 /// regardless of `T`).
65 #[debug(skip)]
66 #[get(pub(crate), type(copy))]
67 #[get_mut(pub(crate))]
68 #[set(pub(crate))]
69 pub(crate) _marker: PhantomData<fn() -> T>,
70}
71
72/// A `Sync` wrapper for single-threaded global `Signal` access.
73///
74/// SAFETY: This type is only safe to use in single-threaded contexts
75/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
76/// variable, but concurrent access from multiple threads would be
77/// undefined behavior.
78#[derive(CustomDebug, Data, New)]
79pub struct SignalCell<T>
80where
81 T: Clone + PartialEq + 'static,
82{
83 /// Interior-mutable storage for an optional signal handle.
84 #[debug(skip)]
85 #[get(pub(crate))]
86 #[get_mut(pub(crate))]
87 #[set(pub(crate))]
88 pub(crate) inner: UnsafeCell<Option<Signal<T>>>,
89}
90
91/// A `Sync` wrapper for single-threaded global `HashSet` access.
92///
93/// SAFETY: This type is only safe to use in single-threaded contexts
94/// (e.g., WASM). It implements `Sync` to allow usage as a `static mut`
95/// variable, but concurrent access from multiple threads would be
96/// undefined behavior.
97#[derive(Data, Debug, New)]
98pub(crate) struct SignalInnerRegistryCell(
99 /// Interior-mutable storage for the signal inner registry.
100 #[get(pub(crate))]
101 #[get_mut(pub(crate))]
102 #[set(pub(crate))]
103 pub(crate) UnsafeCell<Option<HashSet<usize>>>,
104);