Skip to main content

euv_core/reactive/
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(Data)]
7pub(crate) struct SignalInner<T>
8where
9    T: Clone,
10{
11    /// The current value of the signal.
12    #[get(pub(crate))]
13    #[set(pub(crate))]
14    pub(crate) value: T,
15    /// Callbacks to invoke when the value changes.
16    #[get(pub(crate))]
17    #[set(pub(crate))]
18    pub(crate) listeners: Vec<Rc<RefCell<dyn FnMut()>>>,
19    /// Whether this signal is still active. Set to `false` by `clear_listeners()`
20    /// to make subsequent `set()` calls complete no-ops (no value update, no
21    /// listener invocation, no `schedule_signal_update()`), ensuring stale
22    /// closures like orphaned `setInterval` handlers become harmless.
23    #[get(pub(crate), type(copy))]
24    #[set(pub(crate))]
25    pub(crate) alive: bool,
26}
27
28/// A reactive signal handle.
29///
30/// Allows reading, writing, and subscribing to changes.
31/// Implements `Copy` for ergonomic use; all copies share the same underlying state.
32///
33/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
34/// entire program. This is safe in single-threaded WASM contexts where no
35/// concurrent access can occur.
36pub struct Signal<T>
37where
38    T: Clone + PartialEq,
39{
40    /// Raw pointer to the heap-allocated signal inner state.
41    pub(crate) inner: *mut SignalInner<T>,
42}
43
44/// A `Sync` wrapper for single-threaded global `Signal` access.
45///
46/// SAFETY: This type is only safe to use in single-threaded contexts
47/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
48/// variable, but concurrent access from multiple threads would be
49/// undefined behavior.
50pub struct SignalCell<T>
51where
52    T: Clone + PartialEq,
53{
54    /// Interior-mutable storage for an optional signal handle.
55    pub(crate) inner: UnsafeCell<Option<Signal<T>>>,
56}
57
58/// Internal storage for hook state, holding boxed `Any` values.
59///
60/// This struct is not exposed directly; use `HookContext` instead.
61/// The `arm_changed` flag tracks whether a `match` arm switch occurred;
62/// when toggled, the hook array is cleared to prevent signal leakage
63/// between different match arms.
64#[derive(Data)]
65pub struct HookContextInner {
66    /// Storage for hook state values (signals, etc.).
67    #[get(pub(crate))]
68    #[set(pub(crate))]
69    pub(crate) hooks: Vec<Box<dyn Any>>,
70    /// Whether the match arm has changed since the last render.
71    /// Toggled on each `match` arm entry; when the value differs from
72    /// the previous render, hooks are cleared.
73    #[get(pub(crate), type(copy))]
74    #[set(pub(crate))]
75    pub(crate) arm_changed: bool,
76    /// Current hook index, incremented on each hook call and reset per render.
77    #[get(pub(crate), type(copy))]
78    #[set(pub(crate))]
79    pub(crate) hook_index: usize,
80    /// Cleanup closures registered by hooks (e.g., `use_signal`) that must
81    /// be executed when the hook context is cleared due to a `match` arm
82    /// switch. Each closure typically clears signal listeners so that
83    /// stale `setInterval` closures become no-ops.
84    #[get(pub(crate))]
85    #[set(pub(crate))]
86    pub(crate) cleanups: Vec<Box<dyn FnOnce()>>,
87}
88
89/// Manages hook state across render cycles for a DynamicNode.
90///
91/// Stores boxed `Any` values keyed by hook call order, enabling `use_signal`
92/// and similar hooks to persist state between re-renders of the same
93/// dynamic node.
94///
95/// Implements `Copy` for ergonomic use; all copies share the same underlying state.
96///
97/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
98/// entire program. This is safe in single-threaded WASM contexts where no
99/// concurrent access can occur.
100pub struct HookContext {
101    /// Raw pointer to the heap-allocated hook context inner state.
102    pub(crate) inner: *mut HookContextInner,
103}
104
105/// A `Sync` wrapper for single-threaded global `HookContextInner` access.
106///
107/// SAFETY: This type is only safe to use in single-threaded contexts
108/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
109/// variable, but concurrent access from multiple threads would be
110/// undefined behavior.
111pub struct HookContextCell(
112    /// Interior-mutable storage for the hook context inner state.
113    pub(crate) UnsafeCell<HookContextInner>,
114);