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 /// IDs of dynamic nodes that depend on this signal for precise dirty marking.
32 /// When this signal changes, only these dynamic nodes are marked dirty
33 /// instead of broadcasting to all registered dynamic nodes.
34 #[debug(skip)]
35 #[get(pub(crate))]
36 #[get_mut(pub(crate))]
37 #[set(pub(crate))]
38 #[new(skip)]
39 pub(crate) dependents: Vec<usize>,
40}
41
42/// A reactive signal handle.
43///
44/// Allows reading, writing, and subscribing to changes.
45/// Implements `Clone` and `Copy` for ergonomic use; all copies share the same
46/// underlying state. The inner state is heap-allocated via `Rc<RefCell<>>` and
47/// tracked in a global registry to prevent premature deallocation. The `Copy`
48/// semantics are safe because only the pointer address is copied — the actual
49/// `Rc` reference is held by the registry for the lifetime of the program.
50#[derive(CustomDebug, Data, Default, Eq, New, PartialEq)]
51pub struct Signal<T>
52where
53 T: Clone + PartialEq + 'static,
54{
55 /// Address of the heap-allocated inner state.
56 #[debug(skip)]
57 #[get(pub(crate), type(copy))]
58 #[get_mut(pub(crate))]
59 #[set(pub(crate))]
60 pub(crate) inner: usize,
61 /// Marker for the generic type parameter (uses fn pointer to be `Copy`
62 /// regardless of `T`).
63 #[debug(skip)]
64 #[get(pub(crate), type(copy))]
65 #[get_mut(pub(crate))]
66 #[set(pub(crate))]
67 pub(crate) _marker: std::marker::PhantomData<fn() -> T>,
68}
69
70/// A `Sync` wrapper for single-threaded global `Signal` access.
71///
72/// SAFETY: This type is only safe to use in single-threaded contexts
73/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
74/// variable, but concurrent access from multiple threads would be
75/// undefined behavior.
76#[derive(CustomDebug, Data, New)]
77pub struct SignalCell<T>
78where
79 T: Clone + PartialEq + 'static,
80{
81 /// Interior-mutable storage for an optional signal handle.
82 #[debug(skip)]
83 #[get(pub(crate))]
84 #[get_mut(pub(crate))]
85 #[set(pub(crate))]
86 pub(crate) inner: UnsafeCell<Option<Signal<T>>>,
87}
88
89/// A `Sync` wrapper for single-threaded global `HashMap` access.
90///
91/// SAFETY: This type is only safe to use in single-threaded contexts
92/// (e.g., WASM). It implements `Sync` to allow usage as a `static mut`
93/// variable, but concurrent access from multiple threads would be
94/// undefined behavior.
95#[derive(Data, Debug, New)]
96pub(crate) struct SignalInnerRegistryCell(
97 /// Interior-mutable storage for the signal inner registry.
98 #[get(pub(crate))]
99 #[get_mut(pub(crate))]
100 #[set(pub(crate))]
101 pub(crate) UnsafeCell<Option<HashMap<usize, Rc<dyn Any>>>>,
102);