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 #[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 /// Raw pointer to the heap-allocated signal inner state.
33 pub(crate) inner: *mut SignalInner<T>,
34}
35
36/// A `Sync` wrapper for single-threaded global `Signal` access.
37///
38/// SAFETY: This type is only safe to use in single-threaded contexts
39/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
40/// variable, but concurrent access from multiple threads would be
41/// undefined behavior.
42pub struct SignalCell<T>
43where
44 T: Clone + PartialEq,
45{
46 /// Interior-mutable storage for an optional signal handle.
47 pub(crate) inner: UnsafeCell<Option<Signal<T>>>,
48}
49
50/// Internal storage for hook state, holding boxed `Any` values and an index.
51///
52/// This struct is not exposed directly; use `HookContext` instead.
53#[derive(Data)]
54pub struct HookContextInner {
55 /// Internal storage for hook state values.
56 #[get(pub(crate))]
57 #[set(pub(crate))]
58 pub(crate) hooks: Vec<Box<dyn Any>>,
59 /// Current hook index, incremented on each hook call and reset per render.
60 #[set(pub(crate))]
61 pub(crate) hook_index: usize,
62}
63
64/// Manages hook state across render cycles for a DynamicNode.
65///
66/// Stores boxed `Any` values keyed by hook call order, enabling `use_signal`
67/// and similar hooks to persist state between re-renders of the same
68/// dynamic node.
69///
70/// Implements `Copy` for ergonomic use; all copies share the same underlying state.
71///
72/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
73/// entire program. This is safe in single-threaded WASM contexts where no
74/// concurrent access can occur.
75pub struct HookContext {
76 /// Raw pointer to the heap-allocated hook context inner state.
77 pub(crate) inner: *mut HookContextInner,
78}
79
80/// A `Sync` wrapper for single-threaded global `HookContextInner` access.
81///
82/// SAFETY: This type is only safe to use in single-threaded contexts
83/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
84/// variable, but concurrent access from multiple threads would be
85/// undefined behavior.
86pub struct HookContextCell(
87 /// Interior-mutable storage for the hook context inner state.
88 pub(crate) UnsafeCell<HookContextInner>,
89);