Skip to main content

euv_core/vdom/node/
fn.rs

1use crate::*;
2
3/// Constructs a `VirtualNode::Dynamic` from a render closure with hook context management.
4///
5/// # Arguments
6///
7/// - `FnMut() -> VirtualNode + 'static` - The render closure that produces
8///   a virtual node tree. Called on initial render and on every signal update.
9///
10/// # Returns
11///
12/// - `VirtualNode` - A `VirtualNode::Dynamic` wrapping the render closure
13///   with a fresh `HookContext`.
14pub fn create_dynamic_node<F>(mut render_fn: F) -> VirtualNode
15where
16    F: FnMut() -> VirtualNode + 'static,
17{
18    let hook_context: HookContext = create_hook_context();
19    let mut hook_context_for_closure: HookContext = hook_context.clone();
20    let inner: Rc<RefCell<RenderFnInner>> =
21        Rc::new(RefCell::new(RenderFnInner::new(Box::new(move || {
22            hook_context_for_closure.reset_hook_index();
23            render_fn()
24        }))));
25    let dynamic_node: DynamicNode = DynamicNode::new(inner, hook_context);
26    VirtualNode::Dynamic(dynamic_node)
27}
28
29/// Constructs a `VirtualNode::Dynamic` for match expressions where arm hook
30/// isolation is required. The render closure receives a `&mut HookContext`
31/// so it can call `set_arm_changed` before each arm body.
32///
33/// # Arguments
34///
35/// - `FnMut(&mut HookContext) -> VirtualNode + 'static` - The render closure
36///   that receives a mutable reference to the hook context.
37///
38/// # Returns
39///
40/// - `VirtualNode` - A `VirtualNode::Dynamic` wrapping the render closure
41///   with a fresh `HookContext`.
42pub fn create_dynamic_node_with_context<F>(mut render_fn: F) -> VirtualNode
43where
44    F: FnMut(&mut HookContext) -> VirtualNode + 'static,
45{
46    let hook_context: HookContext = create_hook_context();
47    let mut hook_context_for_closure: HookContext = hook_context.clone();
48    let inner: Rc<RefCell<RenderFnInner>> =
49        Rc::new(RefCell::new(RenderFnInner::new(Box::new(move || {
50            hook_context_for_closure.reset_hook_index();
51            render_fn(&mut hook_context_for_closure)
52        }))));
53    let dynamic_node: DynamicNode = DynamicNode::new(inner, hook_context);
54    VirtualNode::Dynamic(dynamic_node)
55}