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 `current_id` field tracks which match arm owns the hooks;
55/// when the ID changes, the hook array is cleared to prevent
56/// signal leakage 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 /// Current context ID, determined by the active match arm.
64 /// When this changes, the hooks array is cleared.
65 #[get(pub(crate), type(copy))]
66 #[set(pub(crate))]
67 pub(crate) current_id: u64,
68 /// Current hook index, incremented on each hook call and reset per render.
69 #[get(pub(crate), type(copy))]
70 #[set(pub(crate))]
71 pub(crate) hook_index: usize,
72}
73
74/// Manages hook state across render cycles for a DynamicNode.
75///
76/// Stores boxed `Any` values keyed by hook call order, enabling `use_signal`
77/// and similar hooks to persist state between re-renders of the same
78/// dynamic node.
79///
80/// Implements `Copy` for ergonomic use; all copies share the same underlying state.
81///
82/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
83/// entire program. This is safe in single-threaded WASM contexts where no
84/// concurrent access can occur.
85pub struct HookContext {
86 /// Raw pointer to the heap-allocated hook context inner state.
87 pub(crate) inner: *mut HookContextInner,
88}
89
90/// A `Sync` wrapper for single-threaded global `HookContextInner` access.
91///
92/// SAFETY: This type is only safe to use in single-threaded contexts
93/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
94/// variable, but concurrent access from multiple threads would be
95/// undefined behavior.
96pub struct HookContextCell(
97 /// Interior-mutable storage for the hook context inner state.
98 pub(crate) UnsafeCell<HookContextInner>,
99);