2022-11-08 19:39:37 -08:00
|
|
|
use crate::{nodes::VNode, virtual_dom::VirtualDom};
|
2022-11-01 18:42:29 -07:00
|
|
|
|
2022-10-27 21:58:47 -07:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
|
|
|
pub struct ElementId(pub usize);
|
|
|
|
|
2022-11-01 18:42:29 -07:00
|
|
|
pub struct ElementPath {
|
|
|
|
pub template: *mut VNode<'static>,
|
|
|
|
pub element: usize,
|
|
|
|
}
|
2022-10-27 21:58:47 -07:00
|
|
|
|
2022-11-01 18:42:29 -07:00
|
|
|
impl VirtualDom {
|
|
|
|
pub fn next_element(&mut self, template: &VNode) -> ElementId {
|
|
|
|
let entry = self.elements.vacant_entry();
|
|
|
|
let id = entry.key();
|
2022-10-27 21:58:47 -07:00
|
|
|
|
2022-11-01 18:42:29 -07:00
|
|
|
entry.insert(ElementPath {
|
|
|
|
template: template as *const _ as *mut _,
|
|
|
|
element: id,
|
|
|
|
});
|
2022-10-27 21:58:47 -07:00
|
|
|
|
2022-11-01 18:42:29 -07:00
|
|
|
ElementId(id)
|
2022-10-27 21:58:47 -07:00
|
|
|
}
|
|
|
|
|
2022-11-01 18:42:29 -07:00
|
|
|
pub fn cleanup_element(&mut self, id: ElementId) {
|
|
|
|
self.elements.remove(id.0);
|
2022-10-27 21:58:47 -07:00
|
|
|
}
|
|
|
|
}
|