use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
use web_sys::Node;
#[derive(Clone, PartialEq, Eq)]
pub struct NodeRef(Rc<RefCell<Option<Node>>>);
impl NodeRef {
pub fn new() -> Self {
Self(Rc::new(RefCell::new(None)))
}
pub fn get(&self) -> Node {
self.try_get().expect("NodeRef is not set")
}
pub fn try_get(&self) -> Option<Node> {
self.0.borrow().clone()
}
pub fn set(&self, node: Node) {
*self.0.borrow_mut() = Some(node);
}
}
impl Default for NodeRef {
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for NodeRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("NodeRef").field(&self.0.borrow()).finish()
}
}