Skip to main content

euv_core/reactive/hook/
struct.rs

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