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/// Text a component carries when the value may be borrowed or owned.
17///
18/// Static labels remain borrowed, while runtime strings retain their
19/// ownership as they cross into a component property.
20#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
21pub struct CowText(pub(crate) Cow<'static, str>);
22
23/// Represents a text node in the virtual DOM.
24///
25/// Text nodes may optionally be bound to a reactive signal for automatic updates.
26///
27/// OPT 29: `content` is `Cow<'static, str>` instead of `String` so the
28/// `html!` macro can emit `Cow::Borrowed("...")` for literal text
29/// without allocating a `String` per text node per render. Runtime
30/// text (signals, `format!`, `to_string()`) still falls through to
31/// `Cow::Owned`. The renderer's `set_text_content` and
32/// `create_text_node` calls take `&str`, which is what `Cow<'static, str>`
33/// derefs to, so call sites use `.as_ref()` (yields `&str`).
34#[derive(Clone, CustomDebug, Data, New)]
35pub struct TextNode {
36    /// The text content.
37    #[get(pub(crate))]
38    #[get_mut(pub(crate))]
39    #[set(pub(crate))]
40    pub(crate) content: Cow<'static, str>,
41    /// An optional binder that wires a freshly created DOM `Text` node to
42    /// its backing signal. The binder is invoked once per DOM text node at
43    /// materialization time (`create_dom_with_doc`); it subscribes the
44    /// source signal directly to that node, so no intermediate bridge
45    /// signal is allocated per binding. Kept text nodes are patched in
46    /// place without re-invoking the binder, so re-renders of a bound
47    /// position never accumulate subscriptions.
48    #[debug(skip)]
49    #[get(pub(crate))]
50    #[get_mut(pub(crate))]
51    #[set(pub(crate))]
52    pub(crate) binder: Option<TextNodeBinder>,
53}
54
55/// A closure-based dynamic node that re-renders when its dependency signals change.
56///
57/// Holds a shared reference to a heap-allocated render closure that produces a fresh
58/// `VirtualNode` on each evaluation. The renderer subscribes to the closure's
59/// signals and patches the DOM automatically.
60/// Contains a `HookContext` that persists hook state (like `use_signal`) across
61/// re-renders, ensuring that signal values are not reset when the render function
62/// is called again.
63///
64/// Uses `Rc<UnsafeCell<>>` instead of `Rc<RefCell<>>` to avoid runtime borrow
65/// checking overhead. Safety is guaranteed by the single-threaded WASM context.
66/// The `Rc` provides automatic memory management — the render closure is freed
67/// when the last reference (either in the VirtualNode tree or the signal update
68/// callback) is dropped.
69#[derive(Clone, CustomDebug, Data, New)]
70#[new(pub(crate))]
71pub struct DynamicNode {
72    /// Shared reference to the heap-allocated render closure inner state.
73    /// `Rc` ensures automatic deallocation; `UnsafeCell` allows mutable access
74    /// without RefCell overhead.
75    #[debug(skip)]
76    #[get(pub(crate))]
77    #[get_mut(pub(crate))]
78    #[set(pub(crate))]
79    pub(crate) render_fn: Rc<UnsafeCell<RenderFnInner>>,
80    /// Persistent hook context for this dynamic node, storing signal
81    /// state and other hook values across render cycles.
82    #[get(pub(crate))]
83    #[get_mut(pub(crate))]
84    #[set(pub(crate))]
85    pub(crate) hook_context: HookContext,
86}