2022-11-16 02:32:48 +00:00
|
|
|
use crate::{any_props::AnyProps, arena::ElementId, ScopeId, ScopeState, UiEvent};
|
2022-11-03 00:29:18 +00:00
|
|
|
use std::{
|
2022-11-04 00:34:42 +00:00
|
|
|
any::{Any, TypeId},
|
2022-11-03 00:29:18 +00:00
|
|
|
cell::{Cell, RefCell},
|
|
|
|
hash::Hasher,
|
2022-11-16 02:32:48 +00:00
|
|
|
rc::Rc,
|
2022-11-03 00:29:18 +00:00
|
|
|
};
|
2022-10-28 04:58:47 +00:00
|
|
|
|
|
|
|
pub type TemplateId = &'static str;
|
|
|
|
|
|
|
|
/// A reference to a template along with any context needed to hydrate it
|
2022-11-02 01:42:29 +00:00
|
|
|
pub struct VNode<'a> {
|
2022-10-28 04:58:47 +00:00
|
|
|
// The ID assigned for the root of this template
|
|
|
|
pub node_id: Cell<ElementId>,
|
|
|
|
|
2022-11-03 08:24:20 +00:00
|
|
|
pub key: Option<&'a str>,
|
|
|
|
|
2022-11-03 00:29:18 +00:00
|
|
|
// When rendered, this template will be linked to its parent manually
|
2022-11-16 00:05:22 +00:00
|
|
|
pub parent: Option<*const DynamicNode<'a>>,
|
2022-11-02 01:42:29 +00:00
|
|
|
|
2022-11-02 08:00:37 +00:00
|
|
|
pub template: Template<'static>,
|
2022-10-28 04:58:47 +00:00
|
|
|
|
|
|
|
pub root_ids: &'a [Cell<ElementId>],
|
|
|
|
|
2022-11-03 00:29:18 +00:00
|
|
|
pub dynamic_nodes: &'a [DynamicNode<'a>],
|
2022-10-28 04:58:47 +00:00
|
|
|
|
2022-11-03 00:29:18 +00:00
|
|
|
pub dynamic_attrs: &'a [Attribute<'a>],
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 22:28:41 +00:00
|
|
|
impl<'a> VNode<'a> {
|
|
|
|
pub fn single_component(
|
|
|
|
cx: &'a ScopeState,
|
|
|
|
node: DynamicNode<'a>,
|
|
|
|
id: &'static str,
|
|
|
|
) -> Option<Self> {
|
|
|
|
Some(VNode {
|
|
|
|
node_id: Cell::new(ElementId(0)),
|
|
|
|
key: None,
|
|
|
|
parent: None,
|
|
|
|
root_ids: &[],
|
|
|
|
dynamic_nodes: cx.bump().alloc([node]),
|
|
|
|
dynamic_attrs: &[],
|
|
|
|
template: Template {
|
|
|
|
id,
|
|
|
|
roots: &[TemplateNode::Dynamic(0)],
|
|
|
|
node_paths: &[&[0]],
|
|
|
|
attr_paths: &[],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2022-11-08 06:55:22 +00:00
|
|
|
|
|
|
|
pub fn single_text(
|
2022-11-09 18:58:11 +00:00
|
|
|
_cx: &'a ScopeState,
|
2022-11-08 06:55:22 +00:00
|
|
|
text: &'static [TemplateNode<'static>],
|
|
|
|
id: &'static str,
|
|
|
|
) -> Option<Self> {
|
|
|
|
Some(VNode {
|
|
|
|
node_id: Cell::new(ElementId(0)),
|
|
|
|
key: None,
|
|
|
|
parent: None,
|
|
|
|
root_ids: &[],
|
|
|
|
dynamic_nodes: &[],
|
|
|
|
dynamic_attrs: &[],
|
|
|
|
template: Template {
|
|
|
|
id,
|
|
|
|
roots: text,
|
|
|
|
node_paths: &[&[0]],
|
|
|
|
attr_paths: &[],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2022-11-06 22:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 04:58:47 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2022-11-02 08:00:37 +00:00
|
|
|
pub struct Template<'a> {
|
|
|
|
pub id: &'a str,
|
|
|
|
pub roots: &'a [TemplateNode<'a>],
|
2022-11-03 08:24:20 +00:00
|
|
|
pub node_paths: &'a [&'a [u8]],
|
|
|
|
pub attr_paths: &'a [&'a [u8]],
|
2022-11-02 08:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> std::hash::Hash for Template<'a> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.id.hash(state);
|
|
|
|
}
|
|
|
|
}
|
2022-11-08 06:55:22 +00:00
|
|
|
impl Eq for Template<'_> {}
|
2022-11-02 08:00:37 +00:00
|
|
|
impl PartialEq for Template<'_> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.id == other.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 04:58:47 +00:00
|
|
|
/// A weird-ish variant of VNodes with way more limited types
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum TemplateNode<'a> {
|
|
|
|
Element {
|
|
|
|
tag: &'a str,
|
|
|
|
namespace: Option<&'a str>,
|
|
|
|
attrs: &'a [TemplateAttribute<'a>],
|
|
|
|
children: &'a [TemplateNode<'a>],
|
2022-11-16 00:05:22 +00:00
|
|
|
inner_opt: bool,
|
2022-10-28 04:58:47 +00:00
|
|
|
},
|
|
|
|
Text(&'a str),
|
|
|
|
Dynamic(usize),
|
|
|
|
DynamicText(usize),
|
|
|
|
}
|
|
|
|
|
2022-11-03 08:24:20 +00:00
|
|
|
pub enum DynamicNode<'a> {
|
2022-10-28 04:58:47 +00:00
|
|
|
Component {
|
|
|
|
name: &'static str,
|
2022-11-06 22:28:41 +00:00
|
|
|
static_props: bool,
|
2022-11-04 00:34:42 +00:00
|
|
|
props: Cell<*mut dyn AnyProps<'a>>,
|
2022-11-04 05:30:26 +00:00
|
|
|
placeholder: Cell<Option<ElementId>>,
|
2022-11-16 00:05:22 +00:00
|
|
|
scope: Cell<Option<ScopeId>>,
|
2022-10-28 04:58:47 +00:00
|
|
|
},
|
|
|
|
Text {
|
|
|
|
id: Cell<ElementId>,
|
2022-11-02 08:00:37 +00:00
|
|
|
value: &'a str,
|
2022-11-16 00:05:22 +00:00
|
|
|
inner: bool,
|
|
|
|
},
|
|
|
|
Fragment {
|
|
|
|
nodes: &'a [VNode<'a>],
|
|
|
|
inner: bool,
|
2022-10-28 04:58:47 +00:00
|
|
|
},
|
2022-11-04 00:34:42 +00:00
|
|
|
Placeholder(Cell<ElementId>),
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2022-11-02 08:00:37 +00:00
|
|
|
pub enum TemplateAttribute<'a> {
|
|
|
|
Static {
|
|
|
|
name: &'static str,
|
|
|
|
value: &'a str,
|
|
|
|
namespace: Option<&'static str>,
|
|
|
|
volatile: bool,
|
|
|
|
},
|
2022-11-03 08:24:20 +00:00
|
|
|
Dynamic(usize),
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Attribute<'a> {
|
2022-11-03 00:29:18 +00:00
|
|
|
pub name: &'a str,
|
|
|
|
pub value: AttributeValue<'a>,
|
2022-10-28 04:58:47 +00:00
|
|
|
pub namespace: Option<&'static str>,
|
2022-11-03 00:29:18 +00:00
|
|
|
pub mounted_element: Cell<ElementId>,
|
2022-11-03 08:24:20 +00:00
|
|
|
pub volatile: bool,
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum AttributeValue<'a> {
|
|
|
|
Text(&'a str),
|
|
|
|
Float(f32),
|
|
|
|
Int(i32),
|
|
|
|
Bool(bool),
|
2022-11-03 00:29:18 +00:00
|
|
|
Listener(RefCell<&'a mut dyn FnMut(&dyn Any)>),
|
2022-10-28 04:58:47 +00:00
|
|
|
Any(&'a dyn AnyValue),
|
2022-11-03 00:29:18 +00:00
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2022-11-16 02:32:48 +00:00
|
|
|
impl<'a> AttributeValue<'a> {
|
|
|
|
pub fn new_listener<T: 'static>(
|
|
|
|
cx: &'a ScopeState,
|
|
|
|
mut f: impl FnMut(UiEvent<T>) + 'a,
|
|
|
|
) -> AttributeValue<'a> {
|
|
|
|
let f = cx.bump().alloc(move |a: &dyn Any| {
|
|
|
|
a.downcast_ref::<UiEvent<T>>()
|
|
|
|
.map(|a| f(a.clone()))
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
panic!(
|
|
|
|
"Expected UiEvent<{}>, got {:?}",
|
|
|
|
std::any::type_name::<T>(),
|
|
|
|
a
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}) as &mut dyn FnMut(&dyn Any);
|
|
|
|
|
|
|
|
AttributeValue::Listener(RefCell::new(f))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 00:34:42 +00:00
|
|
|
impl<'a> std::fmt::Debug for AttributeValue<'a> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Text(arg0) => f.debug_tuple("Text").field(arg0).finish(),
|
|
|
|
Self::Float(arg0) => f.debug_tuple("Float").field(arg0).finish(),
|
|
|
|
Self::Int(arg0) => f.debug_tuple("Int").field(arg0).finish(),
|
|
|
|
Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
|
2022-11-08 06:55:22 +00:00
|
|
|
Self::Listener(_) => f.debug_tuple("Listener").finish(),
|
|
|
|
Self::Any(_) => f.debug_tuple("Any").finish(),
|
2022-11-04 00:34:42 +00:00
|
|
|
Self::None => write!(f, "None"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PartialEq for AttributeValue<'a> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(Self::Text(l0), Self::Text(r0)) => l0 == r0,
|
|
|
|
(Self::Float(l0), Self::Float(r0)) => l0 == r0,
|
|
|
|
(Self::Int(l0), Self::Int(r0)) => l0 == r0,
|
|
|
|
(Self::Bool(l0), Self::Bool(r0)) => l0 == r0,
|
2022-11-08 06:55:22 +00:00
|
|
|
(Self::Listener(_), Self::Listener(_)) => true,
|
2022-11-04 00:34:42 +00:00
|
|
|
(Self::Any(l0), Self::Any(r0)) => l0.any_cmp(*r0),
|
|
|
|
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 00:29:18 +00:00
|
|
|
impl<'a> AttributeValue<'a> {
|
2022-11-04 00:34:42 +00:00
|
|
|
pub fn matches_type(&self, other: &'a AttributeValue<'a>) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(Self::Text(_), Self::Text(_)) => true,
|
|
|
|
(Self::Float(_), Self::Float(_)) => true,
|
|
|
|
(Self::Int(_), Self::Int(_)) => true,
|
|
|
|
(Self::Bool(_), Self::Bool(_)) => true,
|
|
|
|
(Self::Listener(_), Self::Listener(_)) => true,
|
|
|
|
(Self::Any(_), Self::Any(_)) => true,
|
|
|
|
_ => return false,
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait AnyValue {
|
2022-11-04 00:34:42 +00:00
|
|
|
fn any_cmp(&self, other: &dyn AnyValue) -> bool;
|
|
|
|
fn our_typeid(&self) -> TypeId;
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
2022-11-08 06:55:22 +00:00
|
|
|
|
|
|
|
impl<T: PartialEq + Any> AnyValue for T {
|
2022-11-04 00:34:42 +00:00
|
|
|
fn any_cmp(&self, other: &dyn AnyValue) -> bool {
|
|
|
|
if self.type_id() != other.our_typeid() {
|
2022-10-28 04:58:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self == unsafe { &*(other as *const _ as *const T) }
|
|
|
|
}
|
2022-11-04 00:34:42 +00:00
|
|
|
|
|
|
|
fn our_typeid(&self) -> TypeId {
|
|
|
|
self.type_id()
|
|
|
|
}
|
2022-10-28 04:58:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn what_are_the_sizes() {
|
2022-11-02 01:42:29 +00:00
|
|
|
dbg!(std::mem::size_of::<VNode>());
|
2022-10-28 04:58:47 +00:00
|
|
|
dbg!(std::mem::size_of::<Template>());
|
|
|
|
dbg!(std::mem::size_of::<TemplateNode>());
|
|
|
|
}
|
2022-11-16 00:05:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
SSR includes data-id which allows O(1) hydration
|
|
|
|
|
|
|
|
|
|
|
|
we read the edit stream dn then we can just rehydare
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ideas:
|
|
|
|
- IDs for lookup
|
|
|
|
- use edit stream to hydrate
|
|
|
|
- write comments to dom that specify size of children
|
|
|
|
|
|
|
|
IDs for lookups
|
|
|
|
- adds noise to generated html
|
|
|
|
- doesnt work for text nodes
|
|
|
|
- suspense could cause ordering to be weird
|
|
|
|
|
|
|
|
Names for lookups:
|
|
|
|
- label each root or something with the template name
|
|
|
|
- label each dynamic node with a path
|
|
|
|
- noisy too
|
|
|
|
- allows reverse lookups
|
|
|
|
|
|
|
|
Ideal:
|
|
|
|
- no noise in the dom
|
|
|
|
- fast, ideally O(1)
|
|
|
|
- able to pick apart text nodes that get merged during SSR
|
|
|
|
|
|
|
|
|
|
|
|
--> render vdom
|
|
|
|
--> traverse vdom and real dom simultaneously
|
|
|
|
|
|
|
|
IE
|
|
|
|
|
|
|
|
div {
|
|
|
|
div {
|
|
|
|
div {
|
|
|
|
"thing"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/
|