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)]
10pub struct HookContextInner {
11 /// Storage for hook state values (signals, etc.).
12 pub(crate) hooks: Vec<Box<dyn Any>>,
13 /// Whether the match arm has changed since the last render.
14 /// Toggled on each `match` arm entry; when the value differs from
15 /// the previous render, hooks are cleared.
16 #[get(type(copy))]
17 pub(crate) arm_changed: bool,
18 /// Current hook index, incremented on each hook call and reset per render.
19 #[get(type(copy))]
20 pub(crate) hook_index: usize,
21 /// Cleanup closures registered by hooks (e.g., `use_signal`) that must
22 /// be executed when the hook context is cleared due to a `match` arm
23 /// switch. Each closure typically clears signal listeners so that
24 /// stale `setInterval` closures become no-ops.
25 #[debug(skip)]
26 pub(crate) cleanups: Vec<Box<dyn FnOnce()>>,
27}
28
29/// Manages hook state across render cycles for a DynamicNode.
30///
31/// Stores boxed `Any` values keyed by hook call order, enabling `use_signal`
32/// and similar hooks to persist state between re-renders of the same
33/// dynamic node.
34///
35/// Implements `Copy` for ergonomic use; all copies share the same underlying state.
36///
37/// SAFETY: The inner pointer is allocated via `Box::leak` and lives for the
38/// entire program. This is safe in single-threaded WASM contexts where no
39/// concurrent access can occur.
40#[derive(Debug)]
41pub struct HookContext {
42 /// Raw pointer to the heap-allocated hook context inner state.
43 pub(crate) inner: *mut HookContextInner,
44}
45
46/// A `Sync` wrapper for single-threaded global `HookContextInner` access.
47///
48/// SAFETY: This type is only safe to use in single-threaded contexts
49/// (e.g., WASM). It implements `Sync` to allow usage as a `static`
50/// variable, but concurrent access from multiple threads would be
51/// undefined behavior.
52#[derive(Debug)]
53pub struct HookContextCell(
54 /// Interior-mutable storage for the hook context inner state.
55 pub(crate) UnsafeCell<HookContextInner>,
56);