Skip to main content

euv_core/vdom/node/
struct.rs

1use super::*;
2
3/// Inner storage for a dynamic node render closure.
4///
5/// Boxes a `dyn FnMut(&mut HookContext) -> VirtualNode` so it can be stored behind `Rc<UnsafeCell<>>`.
6#[derive(CustomDebug, Data, New)]
7pub(crate) struct RenderFnInner {
8    /// The boxed render closure.
9    #[debug(skip)]
10    #[get(pub(crate))]
11    #[get_mut(pub(crate))]
12    #[set(pub(crate))]
13    pub(crate) render_fn: Box<dyn FnMut(&mut HookContext) -> VirtualNode>,
14}
15
16/// Represents a text node in the virtual DOM.
17///
18/// Text nodes may optionally be bound to a reactive signal for automatic updates.
19///
20/// OPT 29: `content` is `Cow<'static, str>` instead of `String` so the
21/// `html!` macro can emit `Cow::Borrowed("...")` for literal text
22/// without allocating a `String` per text node per render. Runtime
23/// text (signals, `format!`, `to_string()`) still falls through to
24/// `Cow::Owned`. The renderer's `set_text_content` and
25/// `create_text_node` calls take `&str`, which is what `Cow<'static, str>`
26/// derefs to, so call sites use `.as_ref()` (yields `&str`).
27#[derive(Clone, CustomDebug, Data, New)]
28pub struct TextNode {
29    /// The text content.
30    #[get(pub(crate))]
31    #[get_mut(pub(crate))]
32    #[set(pub(crate))]
33    pub(crate) content: Cow<'static, str>,
34    /// An optional signal that drives reactive text updates.
35    #[debug(skip)]
36    #[get(pub(crate))]
37    #[get_mut(pub(crate))]
38    #[set(pub(crate))]
39    pub(crate) signal: Option<Signal<String>>,
40}
41
42/// A closure-based dynamic node that re-renders when its dependency signals change.
43///
44/// Holds a shared reference to a heap-allocated render closure that produces a fresh
45/// `VirtualNode` on each evaluation. The renderer subscribes to the closure's
46/// signals and patches the DOM automatically.
47/// Contains a `HookContext` that persists hook state (like `use_signal`) across
48/// re-renders, ensuring that signal values are not reset when the render function
49/// is called again.
50///
51/// Uses `Rc<UnsafeCell<>>` instead of `Rc<RefCell<>>` to avoid runtime borrow
52/// checking overhead. Safety is guaranteed by the single-threaded WASM context.
53/// The `Rc` provides automatic memory management — the render closure is freed
54/// when the last reference (either in the VirtualNode tree or the signal update
55/// callback) is dropped.
56#[derive(Clone, CustomDebug, Data, New)]
57pub struct DynamicNode {
58    /// Shared reference to the heap-allocated render closure inner state.
59    /// `Rc` ensures automatic deallocation; `UnsafeCell` allows mutable access
60    /// without RefCell overhead.
61    #[debug(skip)]
62    #[get(pub(crate))]
63    #[get_mut(pub(crate))]
64    #[set(pub(crate))]
65    pub(crate) render_fn: Rc<UnsafeCell<RenderFnInner>>,
66    /// Persistent hook context for this dynamic node, storing signal
67    /// state and other hook values across render cycles.
68    #[get(pub(crate))]
69    #[get_mut(pub(crate))]
70    #[set(pub(crate))]
71    pub(crate) hook_context: HookContext,
72}