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