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(crate) struct HookContextInner {
11    /// Storage for hook state values (signals, etc.).
12    #[debug(skip)]
13    #[get(pub(crate))]
14    #[get_mut(pub(crate))]
15    #[set(pub(crate))]
16    pub(crate) hooks: Vec<Box<dyn Any>>,
17    /// The match arm index from the last render.
18    #[get(pub(crate), type(copy))]
19    #[get_mut(pub(crate))]
20    #[set(pub(crate))]
21    pub(crate) arm_changed: usize,
22    /// Current hook index, incremented on each hook call and reset per render.
23    #[get(pub(crate), type(copy))]
24    #[get_mut(pub(crate))]
25    #[set(pub(crate))]
26    pub(crate) hook_index: usize,
27    /// Cleanup closures registered by hooks (e.g., `use_signal`) that must
28    /// be executed when the hook context is cleared due to a `match` arm
29    /// switch.
30    #[debug(skip)]
31    #[get(pub(crate))]
32    #[get_mut(pub(crate))]
33    #[set(pub(crate))]
34    pub(crate) cleanups: Vec<Box<dyn FnOnce()>>,
35}
36
37/// Manages hook state across render cycles for a DynamicNode.
38///
39/// Stores boxed `Any` values keyed by hook call order, enabling `use_signal`
40/// and similar hooks to persist state between re-renders of the same
41/// dynamic node.
42///
43/// Implements `Clone` for ergonomic use; all clones share the same underlying state.
44#[derive(CustomDebug, Data, New)]
45pub struct HookContext {
46    /// Shared reference to the heap-allocated hook context inner state.
47    #[debug(skip)]
48    #[get(pub(crate))]
49    #[get_mut(pub(crate))]
50    #[set(pub(crate))]
51    pub(crate) inner: Rc<RefCell<HookContextInner>>,
52}
53
54/// A handle to a browser interval timer created by `use_interval`.
55///
56/// Stores the numeric interval ID returned by `window.setInterval` so the
57/// timer can be cancelled later via `clear_interval`.
58#[derive(Clone, Copy, Data, New)]
59pub struct IntervalHandle {
60    /// The interval ID assigned by the browser.
61    #[get(pub, type(copy))]
62    #[get_mut(pub(crate))]
63    #[set(pub(crate))]
64    pub interval_id: i32,
65}