Skip to main content

euv_core/reactive/hook/
struct.rs

1use crate::*;
2
3/// Internal storage for hook state, holding boxed `Any` values.
4///
5/// This struct is not exposed directly; use `HookContext` instead.
6/// The `arm_changed` flag tracks whether a `match` arm switch occurred;
7/// when toggled, the hook array is cleared to prevent signal leakage
8/// between different match arms.
9#[derive(CustomDebug, Data, Default, New)]
10pub struct HookContextInner {
11    /// Storage for hook state values (signals, etc.).
12    #[debug(skip)]
13    #[get(pub(crate))]
14    #[set(pub(crate))]
15    pub(crate) hooks: Vec<Box<dyn Any>>,
16    /// The match arm index from the last render.
17    #[get(pub(crate), type(copy))]
18    #[set(pub(crate))]
19    pub(crate) arm_changed: usize,
20    /// Current hook index, incremented on each hook call and reset per render.
21    #[get(pub(crate), type(copy))]
22    #[set(pub(crate))]
23    pub(crate) hook_index: usize,
24    /// Cleanup closures registered by hooks (e.g., `use_signal`) that must
25    /// be executed when the hook context is cleared due to a `match` arm
26    /// switch.
27    #[debug(skip)]
28    #[get(pub(crate))]
29    #[set(pub(crate))]
30    pub(crate) cleanups: Vec<Box<dyn FnOnce()>>,
31}
32
33/// Manages hook state across render cycles for a DynamicNode.
34///
35/// Stores boxed `Any` values keyed by hook call order, enabling `use_signal`
36/// and similar hooks to persist state between re-renders of the same
37/// dynamic node.
38///
39/// Implements `Clone` for ergonomic use; all clones share the same underlying state.
40#[derive(CustomDebug, Data, New)]
41pub struct HookContext {
42    /// Shared reference to the heap-allocated hook context inner state.
43    #[debug(skip)]
44    #[get(pub(crate))]
45    #[set(pub(crate))]
46    pub(crate) inner: Rc<RefCell<HookContextInner>>,
47}
48
49/// A `Sync` wrapper for single-threaded global `HookContextInner` access.
50///
51/// SAFETY: This type is only safe to use in single-threaded contexts
52/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
53/// variable, but concurrent access from multiple threads would be
54/// undefined behavior.
55#[derive(CustomDebug, Data, New)]
56pub struct HookContextCell(
57    /// Interior-mutable storage for the hook context inner state.
58    #[debug(skip)]
59    #[get(pub(crate))]
60    #[set(pub(crate))]
61    pub(crate) UnsafeCell<HookContextInner>,
62);