1use std::cell::RefCell;
2use std::fmt;
3use std::rc::Rc;
4
5use web_sys::Node;
6
7#[derive(Clone, PartialEq, Eq)]
9pub struct NodeRef(Rc<RefCell<Option<Node>>>);
10
11impl NodeRef {
12 pub fn new() -> Self {
14 Self(Rc::new(RefCell::new(None)))
15 }
16
17 pub fn get(&self) -> Node {
24 self.try_get().expect("NodeRef is not set")
25 }
26
27 pub fn try_get(&self) -> Option<Node> {
31 self.0.borrow().clone()
32 }
33
34 pub fn set(&self, node: Node) {
36 *self.0.borrow_mut() = Some(node);
37 }
38}
39
40impl Default for NodeRef {
41 fn default() -> Self {
42 Self::new()
43 }
44}
45
46impl fmt::Debug for NodeRef {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 f.debug_tuple("NodeRef").field(&self.0.borrow()).finish()
49 }
50}