2022-11-08 19:39:37 -08:00
|
|
|
use crate::{nodes::VNode, virtual_dom::VirtualDom};
|
2022-11-01 18:42:29 -07:00
|
|
|
|
2022-11-15 16:05:22 -08:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
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-17 20:00:39 -08:00
|
|
|
pub struct ElementRef {
|
|
|
|
// the pathway of the real element inside the template
|
|
|
|
pub path: &'static [u8],
|
|
|
|
|
|
|
|
// The actual template
|
2022-11-01 18:42:29 -07:00
|
|
|
pub template: *mut VNode<'static>,
|
|
|
|
}
|
2022-10-27 21:58:47 -07:00
|
|
|
|
2022-11-17 20:00:39 -08:00
|
|
|
impl ElementRef {
|
2022-11-11 18:29:27 -08:00
|
|
|
pub fn null() -> Self {
|
|
|
|
Self {
|
|
|
|
template: std::ptr::null_mut(),
|
2022-11-17 20:00:39 -08:00
|
|
|
path: &[],
|
2022-11-11 18:29:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 18:42:29 -07:00
|
|
|
impl VirtualDom {
|
2022-11-17 20:00:39 -08:00
|
|
|
pub fn next_element(&mut self, template: &VNode, path: &'static [u8]) -> ElementId {
|
2022-11-01 18:42:29 -07:00
|
|
|
let entry = self.elements.vacant_entry();
|
|
|
|
let id = entry.key();
|
2022-10-27 21:58:47 -07:00
|
|
|
|
2022-11-17 20:00:39 -08:00
|
|
|
entry.insert(ElementRef {
|
2022-11-01 18:42:29 -07:00
|
|
|
template: template as *const _ as *mut _,
|
2022-11-17 20:00:39 -08:00
|
|
|
path,
|
2022-11-01 18:42:29 -07:00
|
|
|
});
|
2022-10-27 21:58:47 -07:00
|
|
|
|
2022-11-25 02:12:29 -05:00
|
|
|
println!("Claiming {}", id);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-11-17 20:00:39 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
now......
|
|
|
|
|
|
|
|
an ID is mostly a pointer to a node in the real dom.
|
|
|
|
We need to
|
|
|
|
*/
|