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 struct SignalInner<T>
8where
9    T: Clone,
10{
11    /// The current value of the signal.
12    #[set(pub(crate))]
13    pub(crate) value: T,
14    /// Callbacks to invoke when the value changes.
15    #[get(pub(crate))]
16    #[set(pub(crate))]
17    pub(crate) listeners: Vec<Rc<RefCell<dyn FnMut()>>>,
18}
19
20/// A reactive signal handle.
21///
22/// Allows reading, writing, and subscribing to changes.
23/// Implements `Copy` for ergonomic use; all copies share the same underlying state.
24///
25/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
26/// entire program. This is safe in single-threaded WASM contexts where no
27/// concurrent access can occur.
28pub struct Signal<T>
29where
30    T: Clone + PartialEq,
31{
32    pub(crate) inner: *mut SignalInner<T>,
33}
34
35/// Internal storage for hook state, holding boxed `Any` values and an index.
36///
37/// This struct is not exposed directly; use `HookContext` instead.
38#[derive(Data)]
39pub struct HookContextInner {
40    /// Internal storage for hook state values.
41    #[get(pub(crate))]
42    #[set(pub(crate))]
43    pub(crate) hooks: Vec<Box<dyn Any>>,
44    /// Current hook index, incremented on each hook call and reset per render.
45    #[set(pub(crate))]
46    pub(crate) hook_index: usize,
47}
48
49/// Manages hook state across render cycles for a DynamicNode.
50///
51/// Stores boxed `Any` values keyed by hook call order, enabling `use_signal`
52/// and similar hooks to persist state between re-renders of the same
53/// dynamic node.
54///
55/// Implements `Copy` for ergonomic use; all copies share the same underlying state.
56///
57/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
58/// entire program. This is safe in single-threaded WASM contexts where no
59/// concurrent access can occur.
60pub struct HookContext {
61    pub(crate) inner: *mut HookContextInner,
62}
63
64/// A `Sync` wrapper for single-threaded global `HookContextInner` access.
65///
66/// SAFETY: This type is only safe to use in single-threaded contexts
67/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
68/// variable, but concurrent access from multiple threads would be
69/// undefined behavior.
70pub struct HookContextCell(pub(crate) UnsafeCell<HookContextInner>);