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