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}
20
21/// A reactive signal handle.
22///
23/// Allows reading, writing, and subscribing to changes.
24/// Implements `Copy` for ergonomic use; all copies share the same underlying state.
25///
26/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
27/// entire program. This is safe in single-threaded WASM contexts where no
28/// concurrent access can occur.
29pub struct Signal<T>
30where
31    T: Clone + PartialEq,
32{
33    /// Raw pointer to the heap-allocated signal inner state.
34    pub(crate) inner: *mut SignalInner<T>,
35}
36
37/// A `Sync` wrapper for single-threaded global `Signal` access.
38///
39/// SAFETY: This type is only safe to use in single-threaded contexts
40/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
41/// variable, but concurrent access from multiple threads would be
42/// undefined behavior.
43pub struct SignalCell<T>
44where
45    T: Clone + PartialEq,
46{
47    /// Interior-mutable storage for an optional signal handle.
48    pub(crate) inner: UnsafeCell<Option<Signal<T>>>,
49}
50
51/// Internal storage for hook state, holding boxed `Any` values.
52///
53/// This struct is not exposed directly; use `HookContext` instead.
54/// The `arm_changed` flag tracks whether a `match` arm switch occurred;
55/// when toggled, the hook array is cleared to prevent signal leakage
56/// between different match arms.
57#[derive(Data)]
58pub struct HookContextInner {
59    /// Storage for hook state values (signals, etc.).
60    #[get(pub(crate))]
61    #[set(pub(crate))]
62    pub(crate) hooks: Vec<Box<dyn Any>>,
63    /// Whether the match arm has changed since the last render.
64    /// Toggled on each `match` arm entry; when the value differs from
65    /// the previous render, hooks are cleared.
66    #[get(pub(crate), type(copy))]
67    #[set(pub(crate))]
68    pub(crate) arm_changed: bool,
69    /// Current hook index, incremented on each hook call and reset per render.
70    #[get(pub(crate), type(copy))]
71    #[set(pub(crate))]
72    pub(crate) hook_index: usize,
73}
74
75/// Manages hook state across render cycles for a DynamicNode.
76///
77/// Stores boxed `Any` values keyed by hook call order, enabling `use_signal`
78/// and similar hooks to persist state between re-renders of the same
79/// dynamic node.
80///
81/// Implements `Copy` for ergonomic use; all copies share the same underlying state.
82///
83/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
84/// entire program. This is safe in single-threaded WASM contexts where no
85/// concurrent access can occur.
86pub struct HookContext {
87    /// Raw pointer to the heap-allocated hook context inner state.
88    pub(crate) inner: *mut HookContextInner,
89}
90
91/// A `Sync` wrapper for single-threaded global `HookContextInner` 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`
95/// variable, but concurrent access from multiple threads would be
96/// undefined behavior.
97pub struct HookContextCell(
98    /// Interior-mutable storage for the hook context inner state.
99    pub(crate) UnsafeCell<HookContextInner>,
100);