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