euv 0.1.1

A declarative, cross-platform UI framework for Rust with virtual DOM, reactive signals, and RSX macros for WebAssembly.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
use crate::*;

/// Implementation of the virtual DOM renderer.
impl Renderer {
    /// Renders the given virtual DOM tree into the real DOM.
    pub fn render(&mut self, vnode: VirtualNode) {
        let new_unwrapped: VirtualNode = self.unwrap_component(&vnode);
        if let Some(old_vnode) = self.try_get_current_tree() {
            let old_unwrapped: VirtualNode = self.unwrap_component(old_vnode);
            self.patch_root(&old_unwrapped, &new_unwrapped);
        } else {
            let dom_node: Node = self.create_dom_node(&new_unwrapped);
            while let Some(child) = self.get_root().first_child() {
                self.get_root().remove_child(&child).unwrap();
            }
            self.get_root().append_child(&dom_node).unwrap();
        }
        self.set_current_tree(Some(vnode));
    }

    /// Patches the root DOM tree by replacing the single child of `self.root`.
    fn patch_root(&mut self, old_node: &VirtualNode, new_node: &VirtualNode) {
        let dom_child: Option<Node> = self.get_root().first_child();
        let is_element: bool = if let Some(ref dom_child) = dom_child {
            dom_child.dyn_ref::<Element>().is_some()
        } else {
            false
        };
        if is_element {
            let element: Element = dom_child.unwrap().dyn_into::<Element>().unwrap();
            self.patch_node(old_node, new_node, &element);
        } else if let Some(dom_child) = dom_child {
            let new_dom: Node = self.create_dom_node(new_node);
            self.get_root().replace_child(&new_dom, &dom_child).unwrap();
        } else {
            let new_dom: Node = self.create_dom_node(new_node);
            self.get_root().append_child(&new_dom).unwrap();
        }
    }

    /// Patches an existing DOM node to match the new virtual node.
    fn patch_node(
        &mut self,
        old_node: &VirtualNode,
        new_node: &VirtualNode,
        dom_element: &Element,
    ) {
        match (old_node, new_node) {
            (VirtualNode::Text(old_text), VirtualNode::Text(new_text)) => {
                if old_text.get_content() != new_text.get_content() {
                    dom_element.set_text_content(Some(new_text.get_content()));
                }
            }
            (
                VirtualNode::Element {
                    tag: old_tag,
                    attributes: old_attrs,
                    children: old_children,
                    key: _old_key,
                },
                VirtualNode::Element {
                    tag: new_tag,
                    attributes: new_attrs,
                    children: new_children,
                    key: _new_key,
                },
            ) => {
                if !Self::tags_equal(old_tag, new_tag) {
                    let new_dom: Node = self.create_dom_node(new_node);
                    if let Some(parent) = dom_element.parent_node() {
                        parent.replace_child(&new_dom, dom_element).unwrap();
                    }
                    return;
                }
                self.patch_attributes(dom_element, old_attrs, new_attrs);
                self.patch_children(dom_element, old_children, new_children);
            }
            (VirtualNode::Fragment(old_children), VirtualNode::Fragment(new_children)) => {
                self.patch_children(dom_element, old_children, new_children);
            }
            _ => {
                let new_dom: Node = self.create_dom_node(new_node);
                if let Some(parent) = dom_element.parent_node() {
                    parent.replace_child(&new_dom, dom_element).unwrap();
                }
            }
        }
    }

    /// Patches attributes of an element, adding, removing, or updating as needed.
    fn patch_attributes(
        &mut self,
        element: &Element,
        old_attrs: &[AttributeEntry],
        new_attrs: &[AttributeEntry],
    ) {
        let mut old_map: HashMap<&str, &AttributeValue> = HashMap::new();
        for attr in old_attrs {
            old_map.insert(attr.get_name(), attr.get_value());
        }
        let mut new_map: HashMap<&str, &AttributeValue> = HashMap::new();
        for attr in new_attrs {
            new_map.insert(attr.get_name(), attr.get_value());
        }
        for name in old_map.keys() {
            if !new_map.contains_key(*name) {
                Self::remove_dom_attribute_or_property(element, name);
            }
        }
        for attr in new_attrs {
            let should_set: bool = match old_map.get(attr.get_name().as_str()) {
                Some(old_value) => !Self::attribute_values_equal(old_value, attr.get_value()),
                None => true,
            };
            if should_set {
                match attr.get_value() {
                    AttributeValue::Text(value) => {
                        if value.is_empty() {
                            Self::remove_dom_attribute_or_property(element, attr.get_name());
                        } else {
                            Self::set_dom_attribute_or_property(element, attr.get_name(), value);
                        }
                    }
                    AttributeValue::Signal(signal) => {
                        let value: String = signal.get();
                        if value.is_empty() && !Self::is_boolean_property(attr.get_name()) {
                            Self::remove_dom_attribute_or_property(element, attr.get_name());
                        } else {
                            Self::set_dom_attribute_or_property(element, attr.get_name(), &value);
                        }
                    }
                    AttributeValue::Event(handler) => {
                        self.attach_event_listener(element, handler);
                    }
                    AttributeValue::Dynamic(_) => {}
                    AttributeValue::Css(css_class) => {
                        css_class.inject_style();
                        Self::set_dom_attribute_or_property(
                            element,
                            attr.get_name(),
                            css_class.get_name(),
                        );
                    }
                }
            }
        }
    }

    /// Returns true if the given attribute name is a boolean attribute that
    /// requires DOM property-based manipulation instead of HTML attribute strings.
    fn is_boolean_property(name: &str) -> bool {
        matches!(name, "checked" | "disabled" | "selected" | "readonly")
    }

    /// Removes or clears a DOM attribute/property, depending on the attribute name.
    ///
    /// For `value`, sets the DOM property to an empty string rather than calling
    /// `remove_attribute`, because `remove_attribute("value")` only removes the
    /// HTML attribute and does not clear the displayed value of input elements.
    /// For boolean properties (`checked`, `disabled`, `selected`, `readonly`),
    /// sets the DOM property to `false` rather than calling `remove_attribute`,
    /// because `remove_attribute` on a previously-set attribute may not correctly
    /// reset the property in all browsers.
    fn remove_dom_attribute_or_property(element: &Element, name: &str) {
        if name == "value" {
            if let Some(input) = element.dyn_ref::<HtmlInputElement>() {
                input.set_value("");
                return;
            }
            if let Some(textarea) = element.dyn_ref::<HtmlTextAreaElement>() {
                textarea.set_value("");
                return;
            }
            if let Some(select) = element.dyn_ref::<HtmlSelectElement>() {
                select.set_value("");
                return;
            }
        }
        if name == "checked"
            && let Some(input) = element.dyn_ref::<HtmlInputElement>()
        {
            input.set_checked(false);
            return;
        }
        if name == "disabled" {
            if let Some(input) = element.dyn_ref::<HtmlInputElement>() {
                input.set_disabled(false);
                return;
            }
            if let Some(button) = element.dyn_ref::<HtmlButtonElement>() {
                button.set_disabled(false);
                return;
            }
            if let Some(select) = element.dyn_ref::<HtmlSelectElement>() {
                select.set_disabled(false);
                return;
            }
            if let Some(textarea) = element.dyn_ref::<HtmlTextAreaElement>() {
                textarea.set_disabled(false);
                return;
            }
        }
        if name == "selected"
            && let Some(option) = element.dyn_ref::<HtmlOptionElement>()
        {
            option.set_selected(false);
            return;
        }
        if name == "readonly" {
            if let Some(input) = element.dyn_ref::<HtmlInputElement>() {
                input.set_read_only(false);
                return;
            }
            if let Some(textarea) = element.dyn_ref::<HtmlTextAreaElement>() {
                textarea.set_read_only(false);
                return;
            }
        }
        let _ = element.remove_attribute(name);
    }

    /// Sets a DOM attribute or property, depending on the attribute name.
    ///
    /// For `value`, uses the DOM property to ensure input elements update correctly.
    /// For boolean attributes (`checked`, `disabled`, `selected`, `readonly`),
    /// uses the DOM property so that the browser honors the value correctly
    /// (HTML attributes are present-or-absent, not true/false strings).
    /// For all other attributes, uses `set_attribute`.
    fn set_dom_attribute_or_property(element: &Element, name: &str, value: &str) {
        if name == "value" {
            if let Some(input) = element.dyn_ref::<HtmlInputElement>() {
                input.set_value(value);
                return;
            }
            if let Some(textarea) = element.dyn_ref::<HtmlTextAreaElement>() {
                textarea.set_value(value);
                return;
            }
            if let Some(select) = element.dyn_ref::<HtmlSelectElement>() {
                select.set_value(value);
                return;
            }
        }
        if name == "checked"
            && let Some(input) = element.dyn_ref::<HtmlInputElement>()
        {
            input.set_checked(value == "true");
            return;
        }
        if name == "disabled" {
            if let Some(input) = element.dyn_ref::<HtmlInputElement>() {
                input.set_disabled(value == "true");
                return;
            }
            if let Some(button) = element.dyn_ref::<HtmlButtonElement>() {
                button.set_disabled(value == "true");
                return;
            }
            if let Some(select) = element.dyn_ref::<HtmlSelectElement>() {
                select.set_disabled(value == "true");
                return;
            }
            if let Some(textarea) = element.dyn_ref::<HtmlTextAreaElement>() {
                textarea.set_disabled(value == "true");
                return;
            }
        }
        if name == "selected"
            && let Some(option) = element.dyn_ref::<HtmlOptionElement>()
        {
            option.set_selected(value == "true");
            return;
        }
        if name == "readonly" {
            if let Some(input) = element.dyn_ref::<HtmlInputElement>() {
                input.set_read_only(value == "true");
                return;
            }
            if let Some(textarea) = element.dyn_ref::<HtmlTextAreaElement>() {
                textarea.set_read_only(value == "true");
                return;
            }
        }
        let _ = element.set_attribute(name, value);
    }

    /// Compares two tags for equality.
    fn tags_equal(a: &Tag, b: &Tag) -> bool {
        match (a, b) {
            (Tag::Element(a_name), Tag::Element(b_name)) => a_name == b_name,
            (Tag::Component(a_name), Tag::Component(b_name)) => a_name == b_name,
            _ => false,
        }
    }

    /// Compares two attribute values for equality.
    ///
    /// Event attributes are always considered unequal to ensure that
    /// event listeners are re-bound on every patch. This is critical
    /// because the underlying closure may capture different signal
    /// references after a route change, even though the event name
    /// remains the same.
    fn attribute_values_equal(a: &AttributeValue, b: &AttributeValue) -> bool {
        match (a, b) {
            (AttributeValue::Text(a_val), AttributeValue::Text(b_val)) => a_val == b_val,
            (AttributeValue::Signal(_a_sig), AttributeValue::Signal(_b_sig)) => false,
            (AttributeValue::Event(_a_ev), AttributeValue::Event(_b_ev)) => false,
            (AttributeValue::Dynamic(a_dyn), AttributeValue::Dynamic(b_dyn)) => a_dyn == b_dyn,
            (AttributeValue::Css(a_css), AttributeValue::Css(b_css)) => {
                a_css.get_name() == b_css.get_name()
            }
            _ => false,
        }
    }

    /// Gets a child node at the given index by traversing child nodes.
    fn get_child_node(parent: &Element, index: u32) -> Option<Node> {
        let mut current: Option<Node> = parent.first_child();
        let mut current_index: u32 = 0;
        while let Some(node) = current {
            if current_index == index {
                return Some(node);
            }
            current = node.next_sibling();
            current_index += 1;
        }
        None
    }

    /// Patches children of an element using a positional diff algorithm.
    ///
    /// For each position, patches the old child into the new child in-place.
    /// Text nodes are updated by modifying their text content rather than
    /// being replaced, which preserves any reactive signal subscriptions
    /// already wired to the existing DOM text node.
    /// Appends any extra new children, and removes any trailing old children.
    fn patch_children(
        &mut self,
        parent: &Element,
        old_children: &[VirtualNode],
        new_children: &[VirtualNode],
    ) {
        let old_len: usize = old_children.len();
        let new_len: usize = new_children.len();
        let common_len: usize = old_len.min(new_len);
        for index in 0..common_len {
            let old_child: &VirtualNode = &old_children[index];
            let new_child: &VirtualNode = &new_children[index];
            if let Some(dom_child) = Self::get_child_node(parent, index as u32) {
                if let Some(element) = dom_child.dyn_ref::<Element>() {
                    self.patch_node(old_child, new_child, element);
                } else if let (VirtualNode::Text(old_text), VirtualNode::Text(new_text)) =
                    (old_child, new_child)
                {
                    if old_text.get_content() != new_text.get_content() {
                        dom_child.set_text_content(Some(new_text.get_content()));
                    }
                } else {
                    let new_dom: Node = self.create_dom_node(new_child);
                    if let Some(parent_node) = dom_child.parent_node() {
                        let _ = parent_node.replace_child(&new_dom, &dom_child);
                    }
                }
            }
        }
        if new_len > old_len {
            for new_child in new_children.iter().skip(common_len) {
                let new_dom: Node = self.create_dom_node(new_child);
                parent.append_child(&new_dom).unwrap();
            }
        } else if old_len > new_len {
            for _ in common_len..old_len {
                if let Some(last_child) = parent.last_child() {
                    parent.remove_child(&last_child).unwrap();
                }
            }
        }
    }

    /// Creates a real DOM node from a virtual node.
    fn create_dom_node(&mut self, node: &VirtualNode) -> Node {
        match node {
            VirtualNode::Element {
                tag,
                attributes,
                children,
                ..
            } => {
                let document: Document = window().unwrap().document().unwrap();
                let element: Element = match tag {
                    Tag::Element(name) => document.create_element(name).unwrap(),
                    Tag::Component(_) => {
                        let unwrapped: VirtualNode = self.unwrap_component(node);
                        return self.create_dom_node(&unwrapped);
                    }
                };
                for attr in attributes {
                    match attr.get_value() {
                        AttributeValue::Text(value) => {
                            if !value.is_empty() || Self::is_boolean_property(attr.get_name()) {
                                Self::set_dom_attribute_or_property(
                                    &element,
                                    attr.get_name(),
                                    value,
                                );
                            }
                        }
                        AttributeValue::Signal(signal) => {
                            let initial_value: String = signal.get();
                            if !initial_value.is_empty()
                                || Self::is_boolean_property(attr.get_name())
                            {
                                Self::set_dom_attribute_or_property(
                                    &element,
                                    attr.get_name(),
                                    &initial_value,
                                );
                            }
                            let attr_name: String = attr.get_name().clone();
                            let element_clone: Element = element.clone();
                            let signal_for_sub: Signal<String> = *signal;
                            let signal_inner: Signal<String> = signal_for_sub;
                            signal_for_sub.subscribe(move || {
                                let new_value: String = signal_inner.get();
                                if new_value.is_empty() && !Self::is_boolean_property(&attr_name) {
                                    Self::remove_dom_attribute_or_property(
                                        &element_clone,
                                        &attr_name,
                                    );
                                } else {
                                    Self::set_dom_attribute_or_property(
                                        &element_clone,
                                        &attr_name,
                                        &new_value,
                                    );
                                }
                            });
                        }
                        AttributeValue::Event(handler) => {
                            self.attach_event_listener(&element, handler);
                        }
                        AttributeValue::Dynamic(_) => {}
                        AttributeValue::Css(css_class) => {
                            css_class.inject_style();
                            Self::set_dom_attribute_or_property(
                                &element,
                                attr.get_name(),
                                css_class.get_name(),
                            );
                        }
                    }
                }
                for child in children {
                    let child_node: Node = self.create_dom_node(child);
                    element.append_child(&child_node).unwrap();
                }
                element.into()
            }
            VirtualNode::Text(text_node) => {
                let document: Document = window().unwrap().document().unwrap();
                let text: Text = document.create_text_node(text_node.get_content());
                if let Some(signal) = text_node.try_get_signal() {
                    let text_clone: Text = text.clone();
                    let signal_clone: Signal<String> = *signal;
                    signal_clone.subscribe({
                        let signal_inner: Signal<String> = signal_clone;
                        move || {
                            let new_value: String = signal_inner.get();
                            text_clone.set_text_content(Some(&new_value));
                        }
                    });
                }
                text.into()
            }
            VirtualNode::Fragment(children) => {
                let document: Document = window().unwrap().document().unwrap();
                let fragment: Element = document.create_element("div").unwrap();
                for child in children {
                    let child_node: Node = self.create_dom_node(child);
                    fragment.append_child(&child_node).unwrap();
                }
                fragment.into()
            }
            VirtualNode::Dynamic(dynamic_node) => {
                let document: Document = window().unwrap().document().unwrap();
                let placeholder: Element = document.create_element("div").unwrap();
                let style: &str = "display: contents;";
                let _ = placeholder.set_attribute("style", style);
                let mut hook_context: HookContext = dynamic_node.hook_context;
                hook_context.reset_hook_index();
                let initial_vnode: VirtualNode = with_hook_context(hook_context, || {
                    let mut borrowed = dynamic_node.render_fn.borrow_mut();
                    borrowed()
                });
                let initial_unwrapped: VirtualNode = self.unwrap_component(&initial_vnode);
                let initial_dom: Node = self.create_dom_node(&initial_unwrapped);
                placeholder.append_child(&initial_dom).unwrap();
                let render_fn_clone: Rc<RefCell<dyn FnMut() -> VirtualNode>> =
                    Rc::clone(&dynamic_node.render_fn);
                let placeholder_clone: Element = placeholder.clone();
                let mut renderer_for_sub: Renderer = Renderer::new(placeholder_clone.clone());
                renderer_for_sub.set_current_tree(Some(initial_unwrapped));
                let renderer_ref: Rc<RefCell<Renderer>> = Rc::new(RefCell::new(renderer_for_sub));
                let renderer_ref_for_sub: Rc<RefCell<Renderer>> = Rc::clone(&renderer_ref);
                let render_fn_for_sub: Rc<RefCell<dyn FnMut() -> VirtualNode>> =
                    Rc::clone(&render_fn_clone);
                let window: Window = window().unwrap();
                let closure: Closure<dyn FnMut()> = Closure::wrap(Box::new(move || {
                    if placeholder_clone.parent_node().is_none() {
                        return;
                    }
                    hook_context.reset_hook_index();
                    let new_vnode: VirtualNode = with_hook_context(hook_context, || {
                        let mut borrowed = render_fn_for_sub.borrow_mut();
                        borrowed()
                    });
                    let mut renderer = renderer_ref_for_sub.borrow_mut();
                    renderer.render(new_vnode);
                }));
                window
                    .add_event_listener_with_callback(
                        &NativeEventName::EuvSignalUpdate.to_string(),
                        closure.as_ref().unchecked_ref(),
                    )
                    .unwrap();
                closure.forget();
                placeholder.into()
            }
            VirtualNode::Empty => {
                let document: Document = window().unwrap().document().unwrap();
                document.create_text_node("").into()
            }
        }
    }

    /// Recursively unwraps component nodes into their rendered output.
    fn unwrap_component(&self, node: &VirtualNode) -> VirtualNode {
        match node {
            VirtualNode::Element {
                tag: Tag::Component(_),
                children,
                ..
            } => {
                if children.len() == 1 {
                    self.unwrap_component(&children[0])
                } else {
                    VirtualNode::Fragment(children.clone())
                }
            }
            VirtualNode::Element {
                tag,
                attributes,
                children,
                key,
            } => {
                let unwrapped_children: Vec<VirtualNode> = children
                    .iter()
                    .map(|child| self.unwrap_component(child))
                    .collect();
                VirtualNode::Element {
                    tag: tag.clone(),
                    attributes: attributes.clone(),
                    children: unwrapped_children,
                    key: key.clone(),
                }
            }
            VirtualNode::Fragment(children) => {
                let unwrapped_children: Vec<VirtualNode> = children
                    .iter()
                    .map(|child| self.unwrap_component(child))
                    .collect();
                VirtualNode::Fragment(unwrapped_children)
            }
            other => other.clone(),
        }
    }

    /// Attaches an event listener to a DOM element.
    ///
    /// Uses a global auto-incrementing ID stored as `data-euv-id` on the element
    /// to uniquely identify it in the handler registry. This avoids the bug where
    /// `element.as_ref() as *const JsValue as usize` returns the address of the
    /// Rust-side temporary `JsValue` wrapper rather than a stable JS object identity,
    /// causing different DOM elements to collide on the same key.
    ///
    /// On first attach, allocates a new ID, creates a wrapper
    /// `Rc<RefCell<Option<NativeEventHandler>>>`, and registers a DOM
    /// `addEventListener` closure that reads from it. On subsequent patches
    /// for the same element+event, only updates the wrapper content.
    fn attach_event_listener(&self, element: &Element, handler: &NativeEventHandler) {
        let euv_id: usize = match element.get_attribute("data-euv-id") {
            Some(id_str) => id_str.parse::<usize>().unwrap_or_else(|_| {
                let new_id: usize = NEXT_EUV_ID.fetch_add(1, Ordering::Relaxed);
                let _ = element.set_attribute("data-euv-id", &new_id.to_string());
                new_id
            }),
            None => {
                let new_id: usize = NEXT_EUV_ID.fetch_add(1, Ordering::Relaxed);
                let _ = element.set_attribute("data-euv-id", &new_id.to_string());
                new_id
            }
        };
        let event_name: String = handler.get_event_name().clone();
        let key: (usize, String) = (euv_id, event_name.clone());
        // SAFETY: WASM is single-threaded; no concurrent access to HANDLER_REGISTRY.
        let registry: &mut HashMap<(usize, String), Rc<RefCell<Option<NativeEventHandler>>>> =
            get_handler_registry();
        if let Some(existing_wrapper) = registry.get(&key) {
            let mut wrapper: RefMut<Option<NativeEventHandler>> = existing_wrapper.borrow_mut();
            *wrapper = Some(handler.clone());
        } else {
            let handler_wrapper: Rc<RefCell<Option<NativeEventHandler>>> =
                Rc::new(RefCell::new(Some(handler.clone())));
            let wrapper_for_closure: Rc<RefCell<Option<NativeEventHandler>>> =
                Rc::clone(&handler_wrapper);
            let event_name_for_closure: String = event_name.clone();
            let closure: Closure<dyn FnMut(Event)> =
                Closure::wrap(Box::new(move |event: Event| {
                    if let Some(active_handler) = wrapper_for_closure.borrow_mut().as_ref() {
                        let euv_event: NativeEvent =
                            super::r#fn::convert_web_event(&event, &event_name_for_closure);
                        active_handler.handle(euv_event);
                    }
                }));
            element
                .add_event_listener_with_callback(&event_name, closure.as_ref().unchecked_ref())
                .unwrap();
            closure.forget();
            registry.insert(key, handler_wrapper);
        }
    }
}