wip: remove portals completely
This commit is contained in:
1 parent
8daf7a6ed8
commit
2fd56e7619
17 files changed
+259
-404
No files matched your search
@@ -5,7 +5,7 @@
|
||||
//! if the type supports PartialEq. The Properties trait is used by the rsx! and html! macros to generate the type-safe builder
|
||||
//! that ensures compile-time required and optional fields on cx.
|
||||
|
||||
use crate::innerlude::{Element, LazyNodes, Scope, VPortal};
|
||||
use crate::innerlude::{Element, LazyNodes, Scope};
|
||||
|
||||
pub struct FragmentProps<'a>(Element<'a>);
|
||||
pub struct FragmentBuilder<'a, const BUILT: bool>(Element<'a>);
|
||||
|
||||
+22
-71
@@ -239,10 +239,10 @@ impl<'bump> DiffStack<'bump> {
|
||||
}
|
||||
|
||||
impl<'bump> DiffState<'bump> {
|
||||
pub fn diff_scope(&mut self, id: &ScopeId) {
|
||||
pub fn diff_scope(&mut self, id: ScopeId) {
|
||||
let (old, new) = (self.scopes.wip_head(id), self.scopes.fin_head(id));
|
||||
self.stack.push(DiffInstruction::Diff { old, new });
|
||||
self.stack.scope_stack.push(*id);
|
||||
self.stack.scope_stack.push(id);
|
||||
let scope = self.scopes.get_scope(id).unwrap();
|
||||
self.stack.element_stack.push(scope.container);
|
||||
self.work(|| false);
|
||||
@@ -288,12 +288,6 @@ impl<'bump> DiffState<'bump> {
|
||||
1
|
||||
}
|
||||
|
||||
VNode::Portal(linked) => {
|
||||
let node = unsafe { &*linked.node };
|
||||
let node: &VNode = unsafe { std::mem::transmute(node) };
|
||||
self.push_all_nodes(node)
|
||||
}
|
||||
|
||||
VNode::Fragment(_) | VNode::Component(_) => {
|
||||
//
|
||||
let mut added = 0;
|
||||
@@ -357,7 +351,6 @@ impl<'bump> DiffState<'bump> {
|
||||
VNode::Element(element) => self.create_element_node(element, node),
|
||||
VNode::Fragment(frag) => self.create_fragment_node(frag),
|
||||
VNode::Component(component) => self.create_component_node(*component),
|
||||
VNode::Portal(linked) => self.create_linked_node(linked),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -406,7 +399,7 @@ impl<'bump> DiffState<'bump> {
|
||||
self.stack.add_child_count(1);
|
||||
|
||||
if let Some(cur_scope_id) = self.stack.current_scope() {
|
||||
let scope = self.scopes.get_scope(&cur_scope_id).unwrap();
|
||||
let scope = self.scopes.get_scope(cur_scope_id).unwrap();
|
||||
|
||||
for listener in *listeners {
|
||||
self.attach_listener_to_scope(listener, scope);
|
||||
@@ -443,11 +436,11 @@ impl<'bump> DiffState<'bump> {
|
||||
let parent_idx = self.stack.current_scope().unwrap();
|
||||
|
||||
// Insert a new scope into our component list
|
||||
let parent_scope = self.scopes.get_scope(&parent_idx).unwrap();
|
||||
let parent_scope = self.scopes.get_scope(parent_idx).unwrap();
|
||||
let height = parent_scope.height + 1;
|
||||
let subtree = parent_scope.subtree.get();
|
||||
|
||||
let parent_scope = unsafe { self.scopes.get_scope_raw(&parent_idx) };
|
||||
let parent_scope = unsafe { self.scopes.get_scope_raw(parent_idx) };
|
||||
let caller = unsafe { std::mem::transmute(vcomponent.caller as *const _) };
|
||||
let fc_ptr = vcomponent.user_fc;
|
||||
|
||||
@@ -461,7 +454,7 @@ impl<'bump> DiffState<'bump> {
|
||||
vcomponent.associated_scope.set(Some(new_idx));
|
||||
|
||||
if !vcomponent.can_memoize {
|
||||
let cur_scope = self.scopes.get_scope(&parent_idx).unwrap();
|
||||
let cur_scope = self.scopes.get_scope(parent_idx).unwrap();
|
||||
let extended = unsafe { std::mem::transmute(vcomponent) };
|
||||
cur_scope.items.borrow_mut().borrowed_props.push(extended);
|
||||
} else {
|
||||
@@ -469,7 +462,7 @@ impl<'bump> DiffState<'bump> {
|
||||
}
|
||||
|
||||
// TODO: add noderefs to current noderef list Noderefs
|
||||
let _new_component = self.scopes.get_scope(&new_idx).unwrap();
|
||||
let _new_component = self.scopes.get_scope(new_idx).unwrap();
|
||||
|
||||
log::debug!(
|
||||
"initializing component {:?} with height {:?}",
|
||||
@@ -478,9 +471,9 @@ impl<'bump> DiffState<'bump> {
|
||||
);
|
||||
|
||||
// Run the scope for one iteration to initialize it
|
||||
if self.scopes.run_scope(&new_idx) {
|
||||
if self.scopes.run_scope(new_idx) {
|
||||
// Take the node that was just generated from running the component
|
||||
let nextnode = self.scopes.fin_head(&new_idx);
|
||||
let nextnode = self.scopes.fin_head(new_idx);
|
||||
self.stack.create_component(new_idx, nextnode);
|
||||
|
||||
// todo: subtrees
|
||||
@@ -493,16 +486,6 @@ impl<'bump> DiffState<'bump> {
|
||||
self.mutations.dirty_scopes.insert(new_idx);
|
||||
}
|
||||
|
||||
fn create_linked_node(&mut self, link: &'bump VPortal) {
|
||||
if link.scope_id.get().is_none() {
|
||||
if let Some(cur_scope) = self.stack.current_scope() {
|
||||
link.scope_id.set(Some(cur_scope));
|
||||
}
|
||||
}
|
||||
let node: &'bump VNode<'static> = unsafe { &*link.node };
|
||||
self.create_node(unsafe { std::mem::transmute(node) });
|
||||
}
|
||||
|
||||
// =================================
|
||||
// Tools for diffing nodes
|
||||
// =================================
|
||||
@@ -520,12 +503,11 @@ impl<'bump> DiffState<'bump> {
|
||||
(Fragment(old), Fragment(new)) => self.diff_fragment_nodes(old, new),
|
||||
(Placeholder(old), Placeholder(new)) => new.dom_id.set(old.dom_id.get()),
|
||||
(Element(old), Element(new)) => self.diff_element_nodes(old, new, old_node, new_node),
|
||||
(Portal(old), Portal(new)) => self.diff_linked_nodes(old, new),
|
||||
|
||||
// Anything else is just a basic replace and create
|
||||
(
|
||||
Portal(_) | Component(_) | Fragment(_) | Text(_) | Element(_) | Placeholder(_),
|
||||
Portal(_) | Component(_) | Fragment(_) | Text(_) | Element(_) | Placeholder(_),
|
||||
Component(_) | Fragment(_) | Text(_) | Element(_) | Placeholder(_),
|
||||
Component(_) | Fragment(_) | Text(_) | Element(_) | Placeholder(_),
|
||||
) => self
|
||||
.stack
|
||||
.create_node(new_node, MountType::Replace { old: old_node }),
|
||||
@@ -613,7 +595,7 @@ impl<'bump> DiffState<'bump> {
|
||||
//
|
||||
// TODO: take a more efficient path than this
|
||||
if let Some(cur_scope_id) = self.stack.current_scope() {
|
||||
let scope = self.scopes.get_scope(&cur_scope_id).unwrap();
|
||||
let scope = self.scopes.get_scope(cur_scope_id).unwrap();
|
||||
|
||||
if old.listeners.len() == new.listeners.len() {
|
||||
for (old_l, new_l) in old.listeners.iter().zip(new.listeners.iter()) {
|
||||
@@ -736,7 +718,7 @@ impl<'bump> DiffState<'bump> {
|
||||
// make sure the component's caller function is up to date
|
||||
let scope = unsafe {
|
||||
self.scopes
|
||||
.get_scope_mut(&scope_addr)
|
||||
.get_scope_mut(scope_addr)
|
||||
.unwrap_or_else(|| panic!("could not find {:?}", scope_addr))
|
||||
};
|
||||
|
||||
@@ -745,10 +727,10 @@ impl<'bump> DiffState<'bump> {
|
||||
// React doesn't automatically memoize, but we do.
|
||||
let props_are_the_same = old.comparator.unwrap();
|
||||
|
||||
if (self.force_diff || !props_are_the_same(new)) && self.scopes.run_scope(&scope_addr) {
|
||||
if (self.force_diff || !props_are_the_same(new)) && self.scopes.run_scope(scope_addr) {
|
||||
self.diff_node(
|
||||
self.scopes.wip_head(&scope_addr),
|
||||
self.scopes.fin_head(&scope_addr),
|
||||
self.scopes.wip_head(scope_addr),
|
||||
self.scopes.fin_head(scope_addr),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -773,19 +755,6 @@ impl<'bump> DiffState<'bump> {
|
||||
self.diff_children(old.children, new.children);
|
||||
}
|
||||
|
||||
fn diff_linked_nodes(&mut self, old: &'bump VPortal, new: &'bump VPortal) {
|
||||
if !std::ptr::eq(old.node, new.node) {
|
||||
// if the ptrs are the same then theyr're the same
|
||||
let old: &VNode = unsafe { std::mem::transmute(&*old.node) };
|
||||
let new: &VNode = unsafe { std::mem::transmute(&*new.node) };
|
||||
self.diff_node(old, new);
|
||||
}
|
||||
|
||||
if new.scope_id.get().is_none() {
|
||||
todo!("attach the link to the scope - when children are not created");
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// Utilities for creating new diff instructions
|
||||
// =============================================
|
||||
@@ -1226,16 +1195,12 @@ impl<'bump> DiffState<'bump> {
|
||||
VNode::Text(t) => break t.dom_id.get(),
|
||||
VNode::Element(t) => break t.dom_id.get(),
|
||||
VNode::Placeholder(t) => break t.dom_id.get(),
|
||||
VNode::Portal(l) => {
|
||||
let node: &VNode = unsafe { std::mem::transmute(&*l.node) };
|
||||
self.find_last_element(node);
|
||||
}
|
||||
VNode::Fragment(frag) => {
|
||||
search_node = frag.children.last();
|
||||
}
|
||||
VNode::Component(el) => {
|
||||
let scope_id = el.associated_scope.get().unwrap();
|
||||
search_node = Some(self.scopes.root_node(&scope_id));
|
||||
search_node = Some(self.scopes.root_node(scope_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1252,11 +1217,7 @@ impl<'bump> DiffState<'bump> {
|
||||
}
|
||||
VNode::Component(el) => {
|
||||
let scope_id = el.associated_scope.get().unwrap();
|
||||
search_node = Some(self.scopes.root_node(&scope_id));
|
||||
}
|
||||
VNode::Portal(link) => {
|
||||
let node = unsafe { std::mem::transmute(&*link.node) };
|
||||
search_node = Some(node);
|
||||
search_node = Some(self.scopes.root_node(scope_id));
|
||||
}
|
||||
VNode::Text(t) => break t.dom_id.get(),
|
||||
VNode::Element(t) => break t.dom_id.get(),
|
||||
@@ -1292,17 +1253,12 @@ impl<'bump> DiffState<'bump> {
|
||||
}
|
||||
|
||||
VNode::Component(c) => {
|
||||
let node = self.scopes.fin_head(&c.associated_scope.get().unwrap());
|
||||
let node = self.scopes.fin_head(c.associated_scope.get().unwrap());
|
||||
self.replace_node(node, nodes_created);
|
||||
|
||||
let scope_id = c.associated_scope.get().unwrap();
|
||||
log::debug!("Destroying scope {:?}", scope_id);
|
||||
self.scopes.try_remove(&scope_id).unwrap();
|
||||
}
|
||||
|
||||
VNode::Portal(l) => {
|
||||
let node: &'bump VNode<'bump> = unsafe { std::mem::transmute(&*l.node) };
|
||||
self.replace_node(node, nodes_created);
|
||||
self.scopes.try_remove(scope_id).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1349,11 +1305,6 @@ impl<'bump> DiffState<'bump> {
|
||||
self.remove_nodes(f.children, gen_muts);
|
||||
}
|
||||
|
||||
VNode::Portal(l) => {
|
||||
let node = unsafe { std::mem::transmute(&*l.node) };
|
||||
self.remove_nodes(Some(node), gen_muts);
|
||||
}
|
||||
|
||||
VNode::Component(c) => {
|
||||
self.destroy_vomponent(c, gen_muts);
|
||||
}
|
||||
@@ -1363,10 +1314,10 @@ impl<'bump> DiffState<'bump> {
|
||||
|
||||
fn destroy_vomponent(&mut self, vc: &VComponent, gen_muts: bool) {
|
||||
let scope_id = vc.associated_scope.get().unwrap();
|
||||
let root = self.scopes.root_node(&scope_id);
|
||||
let root = self.scopes.root_node(scope_id);
|
||||
self.remove_nodes(Some(root), gen_muts);
|
||||
log::debug!("Destroying scope {:?}", scope_id);
|
||||
self.scopes.try_remove(&scope_id).unwrap();
|
||||
self.scopes.try_remove(scope_id).unwrap();
|
||||
}
|
||||
|
||||
/// Adds a listener closure to a scope during diff.
|
||||
|
||||
@@ -114,22 +114,6 @@ pub enum VNode<'src> {
|
||||
/// }
|
||||
/// ```
|
||||
Placeholder(&'src VPlaceholder),
|
||||
|
||||
/// A VNode that is actually a pointer to some nodes rather than the nodes directly. Useful when rendering portals
|
||||
/// or eliding lifetimes on VNodes through runtime checks.
|
||||
///
|
||||
/// Linked VNodes can only be made through the [`Context::render`] method
|
||||
///
|
||||
/// Typically, linked nodes are found *not* in a VNode. When NodeLinks are in a VNode, the NodeLink was passed into
|
||||
/// an `rsx!` call.
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust, ignore
|
||||
/// let mut vdom = VirtualDom::new();
|
||||
///
|
||||
/// let node: NodeLink = vdom.render_vnode(rsx!( "hello" ));
|
||||
/// ```
|
||||
Portal(VPortal),
|
||||
}
|
||||
|
||||
impl<'src> VNode<'src> {
|
||||
@@ -141,7 +125,6 @@ impl<'src> VNode<'src> {
|
||||
VNode::Fragment(f) => f.key,
|
||||
VNode::Text(_t) => None,
|
||||
VNode::Placeholder(_f) => None,
|
||||
VNode::Portal(_c) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +143,6 @@ impl<'src> VNode<'src> {
|
||||
VNode::Text(el) => el.dom_id.get(),
|
||||
VNode::Element(el) => el.dom_id.get(),
|
||||
VNode::Placeholder(el) => el.dom_id.get(),
|
||||
VNode::Portal(_) => None,
|
||||
VNode::Fragment(_) => None,
|
||||
VNode::Component(_) => None,
|
||||
}
|
||||
@@ -184,11 +166,6 @@ impl<'src> VNode<'src> {
|
||||
children: f.children,
|
||||
key: f.key,
|
||||
}),
|
||||
VNode::Portal(c) => VNode::Portal(VPortal {
|
||||
scope_id: c.scope_id.clone(),
|
||||
link_idx: c.link_idx.clone(),
|
||||
node: c.node,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,7 +184,6 @@ impl Debug for VNode<'_> {
|
||||
write!(s, "VNode::VFragment {{ children: {:?} }}", frag.children)
|
||||
}
|
||||
VNode::Component(comp) => write!(s, "VNode::VComponent {{ fc: {:?}}}", comp.user_fc),
|
||||
VNode::Portal(c) => write!(s, "VNode::VCached {{ scope_id: {:?} }}", c.scope_id.get()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -368,32 +344,6 @@ impl Clone for EventHandler<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A cached node is a "pointer" to a "rendered" node in a particular scope
|
||||
///
|
||||
/// It does not provide direct access to the node, so it doesn't carry any lifetime information with it
|
||||
///
|
||||
/// It is used during the diffing/rendering process as a runtime key into an existing set of nodes. The "render" key
|
||||
/// is essentially a unique key to guarantee safe usage of the Node.
|
||||
///
|
||||
/// Linked VNodes can only be made through the [`Context::render`] method
|
||||
///
|
||||
/// Typically, NodeLinks are found *not* in a VNode. When NodeLinks are in a VNode, the NodeLink was passed into
|
||||
/// an `rsx!` call.
|
||||
///
|
||||
/// todo: remove the raw pointer and use runtime checks instead
|
||||
#[derive(Debug)]
|
||||
pub struct VPortal {
|
||||
pub(crate) link_idx: Cell<usize>,
|
||||
pub(crate) scope_id: Cell<Option<ScopeId>>,
|
||||
pub(crate) node: *const VNode<'static>,
|
||||
}
|
||||
|
||||
impl PartialEq for VPortal {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.node == other.node
|
||||
}
|
||||
}
|
||||
|
||||
/// Virtual Components for custom user-defined components
|
||||
/// Only supports the functional syntax
|
||||
pub struct VComponent<'src> {
|
||||
@@ -824,49 +774,3 @@ impl IntoVNode<'_> for Arguments<'_> {
|
||||
cx.text(self)
|
||||
}
|
||||
}
|
||||
|
||||
// called cx.render from a helper function
|
||||
impl IntoVNode<'_> for Option<VPortal> {
|
||||
fn into_vnode(self, _cx: NodeFactory) -> VNode {
|
||||
match self {
|
||||
Some(node) => VNode::Portal(node),
|
||||
None => {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// essentially passing elements through props
|
||||
// just build a new element in place
|
||||
impl IntoVNode<'_> for &Option<VPortal> {
|
||||
fn into_vnode(self, _cx: NodeFactory) -> VNode {
|
||||
match self {
|
||||
Some(node) => VNode::Portal(VPortal {
|
||||
link_idx: node.link_idx.clone(),
|
||||
scope_id: node.scope_id.clone(),
|
||||
node: node.node,
|
||||
}),
|
||||
None => {
|
||||
//
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoVNode<'_> for VPortal {
|
||||
fn into_vnode(self, _cx: NodeFactory) -> VNode {
|
||||
VNode::Portal(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoVNode<'_> for &VPortal {
|
||||
fn into_vnode(self, _cx: NodeFactory) -> VNode {
|
||||
VNode::Portal(VPortal {
|
||||
link_idx: self.link_idx.clone(),
|
||||
scope_id: self.scope_id.clone(),
|
||||
node: self.node,
|
||||
})
|
||||
}
|
||||
}
|
||||
+32
-66
@@ -36,33 +36,7 @@ pub struct Scope<'a, P> {
|
||||
pub props: &'a P,
|
||||
}
|
||||
|
||||
impl<'a, P> Scope<'a, P> {
|
||||
/// Take a lazy VNode structure and actually build it with the context of the VDom's efficient VNode allocator.
|
||||
///
|
||||
/// This function consumes the context and absorb the lifetime, so these VNodes *must* be returned.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// fn Component(cx: Scope, props: &Props) -> Element {
|
||||
/// // Lazy assemble the VNode tree
|
||||
/// let lazy_nodes = rsx!("hello world");
|
||||
///
|
||||
/// // Actually build the tree and allocate it
|
||||
/// cx.render(lazy_tree)
|
||||
/// }
|
||||
///```
|
||||
pub fn render(self, rsx: Option<LazyNodes<'a, '_>>) -> Option<VNode<'a>> {
|
||||
let fac = NodeFactory {
|
||||
bump: &self.scope.wip_frame().bump,
|
||||
};
|
||||
match rsx {
|
||||
Some(s) => Some(s.call(fac)),
|
||||
None => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> Copy for Scope<'_, P> {}
|
||||
impl<P> Clone for Scope<'_, P> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
@@ -71,9 +45,10 @@ impl<P> Clone for Scope<'_, P> {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<P> Copy for Scope<'_, P> {}
|
||||
impl<P> std::ops::Deref for Scope<'_, P> {
|
||||
type Target = ScopeState;
|
||||
|
||||
impl<'a, P> std::ops::Deref for Scope<'a, P> {
|
||||
// rust will auto deref again to the original 'a lifetime at the call site
|
||||
type Target = &'a ScopeState;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.scope
|
||||
}
|
||||
@@ -362,31 +337,31 @@ impl ScopeState {
|
||||
items.tasks.len() - 1
|
||||
}
|
||||
|
||||
// /// Take a lazy VNode structure and actually build it with the context of the VDom's efficient VNode allocator.
|
||||
// ///
|
||||
// /// This function consumes the context and absorb the lifetime, so these VNodes *must* be returned.
|
||||
// ///
|
||||
// /// ## Example
|
||||
// ///
|
||||
// /// ```ignore
|
||||
// /// fn Component(cx: Scope, props: &Props) -> Element {
|
||||
// /// // Lazy assemble the VNode tree
|
||||
// /// let lazy_nodes = rsx!("hello world");
|
||||
// ///
|
||||
// /// // Actually build the tree and allocate it
|
||||
// /// cx.render(lazy_tree)
|
||||
// /// }
|
||||
// ///```
|
||||
// pub fn render<'src>(&self, rsx: Option<LazyNodes<'src, '_>>) -> Option<VNode<'src>> {
|
||||
// let fac = NodeFactory {
|
||||
// bump: &self.wip_frame().bump,
|
||||
// };
|
||||
// match rsx {
|
||||
// Some(s) => Some(s.call(fac)),
|
||||
// None => todo!(),
|
||||
// }
|
||||
// // rsx.map(|f| f.call(fac))
|
||||
// }
|
||||
/// Take a lazy VNode structure and actually build it with the context of the VDom's efficient VNode allocator.
|
||||
///
|
||||
/// This function consumes the context and absorb the lifetime, so these VNodes *must* be returned.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// fn Component(cx: Scope, props: &Props) -> Element {
|
||||
/// // Lazy assemble the VNode tree
|
||||
/// let lazy_nodes = rsx!("hello world");
|
||||
///
|
||||
/// // Actually build the tree and allocate it
|
||||
/// cx.render(lazy_tree)
|
||||
/// }
|
||||
///```
|
||||
pub fn render<'src>(&'src self, rsx: Option<LazyNodes<'src, '_>>) -> Option<VNode<'src>> {
|
||||
let fac = NodeFactory {
|
||||
bump: &self.wip_frame().bump,
|
||||
};
|
||||
match rsx {
|
||||
Some(s) => Some(s.call(fac)),
|
||||
None => todo!(),
|
||||
}
|
||||
// rsx.map(|f| f.call(fac))
|
||||
}
|
||||
|
||||
/// Store a value between renders
|
||||
///
|
||||
@@ -492,7 +467,7 @@ impl ScopeState {
|
||||
|
||||
pub(crate) struct BumpFrame {
|
||||
pub bump: Bump,
|
||||
pub nodes: RefCell<Vec<*const VNode<'static>>>,
|
||||
pub nodes: Cell<*const VNode<'static>>,
|
||||
}
|
||||
impl BumpFrame {
|
||||
pub(crate) fn new(capacity: usize) -> Self {
|
||||
@@ -504,18 +479,9 @@ impl BumpFrame {
|
||||
is_static: false,
|
||||
});
|
||||
let node = bump.alloc(VNode::Text(unsafe { std::mem::transmute(node) }));
|
||||
let nodes = RefCell::new(vec![node as *const _]);
|
||||
let nodes = Cell::new(node as *const _);
|
||||
Self { bump, nodes }
|
||||
}
|
||||
|
||||
pub(crate) fn assign_nodelink(&self, node: &VPortal) {
|
||||
let mut nodes = self.nodes.borrow_mut();
|
||||
|
||||
let len = nodes.len();
|
||||
nodes.push(node.node);
|
||||
|
||||
node.link_idx.set(len);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -68,16 +68,16 @@ impl ScopeArena {
|
||||
/// Safety:
|
||||
/// - Obtaining a mutable refernece to any Scope is unsafe
|
||||
/// - Scopes use interior mutability when sharing data into components
|
||||
pub(crate) fn get_scope(&self, id: &ScopeId) -> Option<&ScopeState> {
|
||||
unsafe { self.scopes.borrow().get(id).map(|f| &**f) }
|
||||
pub(crate) fn get_scope(&self, id: ScopeId) -> Option<&ScopeState> {
|
||||
unsafe { self.scopes.borrow().get(&id).map(|f| &**f) }
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn get_scope_raw(&self, id: &ScopeId) -> Option<*mut ScopeState> {
|
||||
self.scopes.borrow().get(id).copied()
|
||||
pub(crate) unsafe fn get_scope_raw(&self, id: ScopeId) -> Option<*mut ScopeState> {
|
||||
self.scopes.borrow().get(&id).copied()
|
||||
}
|
||||
|
||||
pub(crate) unsafe fn get_scope_mut(&self, id: &ScopeId) -> Option<&mut ScopeState> {
|
||||
self.scopes.borrow().get(id).map(|s| &mut **s)
|
||||
pub(crate) unsafe fn get_scope_mut(&self, id: ScopeId) -> Option<&mut ScopeState> {
|
||||
self.scopes.borrow().get(&id).map(|s| &mut **s)
|
||||
}
|
||||
|
||||
pub(crate) fn new_with_key(
|
||||
@@ -102,27 +102,27 @@ impl ScopeArena {
|
||||
scope.our_arena_idx = new_scope_id;
|
||||
scope.container = container;
|
||||
|
||||
scope.frames[0].nodes.get_mut().push({
|
||||
let vnode = scope.frames[0]
|
||||
.bump
|
||||
.alloc(VNode::Text(scope.frames[0].bump.alloc(VText {
|
||||
dom_id: Default::default(),
|
||||
is_static: false,
|
||||
text: "",
|
||||
})));
|
||||
unsafe { std::mem::transmute(vnode as *mut VNode) }
|
||||
});
|
||||
// scope.frames[0].nodes.get_mut().push({
|
||||
// let vnode = scope.frames[0]
|
||||
// .bump
|
||||
// .alloc(VNode::Text(scope.frames[0].bump.alloc(VText {
|
||||
// dom_id: Default::default(),
|
||||
// is_static: false,
|
||||
// text: "",
|
||||
// })));
|
||||
// unsafe { std::mem::transmute(vnode as *mut VNode) }
|
||||
// });
|
||||
|
||||
scope.frames[1].nodes.get_mut().push({
|
||||
let vnode = scope.frames[1]
|
||||
.bump
|
||||
.alloc(VNode::Text(scope.frames[1].bump.alloc(VText {
|
||||
dom_id: Default::default(),
|
||||
is_static: false,
|
||||
text: "",
|
||||
})));
|
||||
unsafe { std::mem::transmute(vnode as *mut VNode) }
|
||||
});
|
||||
// scope.frames[1].nodes.get_mut().push({
|
||||
// let vnode = scope.frames[1]
|
||||
// .bump
|
||||
// .alloc(VNode::Text(scope.frames[1].bump.alloc(VText {
|
||||
// dom_id: Default::default(),
|
||||
// is_static: false,
|
||||
// text: "",
|
||||
// })));
|
||||
// unsafe { std::mem::transmute(vnode as *mut VNode) }
|
||||
// });
|
||||
|
||||
let any_item = self.scopes.borrow_mut().insert(new_scope_id, scope);
|
||||
debug_assert!(any_item.is_none());
|
||||
@@ -138,27 +138,28 @@ impl ScopeArena {
|
||||
|
||||
let mut frames = [BumpFrame::new(node_capacity), BumpFrame::new(node_capacity)];
|
||||
|
||||
frames[0].nodes.get_mut().push({
|
||||
let vnode = frames[0]
|
||||
.bump
|
||||
.alloc(VNode::Text(frames[0].bump.alloc(VText {
|
||||
dom_id: Default::default(),
|
||||
is_static: false,
|
||||
text: "",
|
||||
})));
|
||||
unsafe { std::mem::transmute(vnode as *mut VNode) }
|
||||
});
|
||||
// todo - add the node
|
||||
// frames[0].nodes.get_mut().push({
|
||||
// let vnode = frames[0]
|
||||
// .bump
|
||||
// .alloc(VNode::Text(frames[0].bump.alloc(VText {
|
||||
// dom_id: Default::default(),
|
||||
// is_static: false,
|
||||
// text: "",
|
||||
// })));
|
||||
// unsafe { std::mem::transmute(vnode as *mut VNode) }
|
||||
// });
|
||||
|
||||
frames[1].nodes.get_mut().push({
|
||||
let vnode = frames[1]
|
||||
.bump
|
||||
.alloc(VNode::Text(frames[1].bump.alloc(VText {
|
||||
dom_id: Default::default(),
|
||||
is_static: false,
|
||||
text: "",
|
||||
})));
|
||||
unsafe { std::mem::transmute(vnode as *mut VNode) }
|
||||
});
|
||||
// frames[1].nodes.get_mut().push({
|
||||
// let vnode = frames[1]
|
||||
// .bump
|
||||
// .alloc(VNode::Text(frames[1].bump.alloc(VText {
|
||||
// dom_id: Default::default(),
|
||||
// is_static: false,
|
||||
// text: "",
|
||||
// })));
|
||||
// unsafe { std::mem::transmute(vnode as *mut VNode) }
|
||||
// });
|
||||
|
||||
let scope = self.bump.alloc(ScopeState {
|
||||
sender: self.sender.clone(),
|
||||
@@ -193,13 +194,13 @@ impl ScopeArena {
|
||||
new_scope_id
|
||||
}
|
||||
|
||||
pub fn try_remove(&self, id: &ScopeId) -> Option<()> {
|
||||
pub fn try_remove(&self, id: ScopeId) -> Option<()> {
|
||||
self.ensure_drop_safety(id);
|
||||
|
||||
// Safety:
|
||||
// - ensure_drop_safety ensures that no references to this scope are in use
|
||||
// - this raw pointer is removed from the map
|
||||
let scope = unsafe { &mut *self.scopes.borrow_mut().remove(id).unwrap() };
|
||||
let scope = unsafe { &mut *self.scopes.borrow_mut().remove(&id).unwrap() };
|
||||
|
||||
// we're just reusing scopes so we need to clear it out
|
||||
scope.hook_vals.get_mut().drain(..).for_each(|state| {
|
||||
@@ -216,8 +217,8 @@ impl ScopeArena {
|
||||
scope.is_subtree_root.set(false);
|
||||
scope.subtree.set(0);
|
||||
|
||||
scope.frames[0].nodes.get_mut().clear();
|
||||
scope.frames[1].nodes.get_mut().clear();
|
||||
// scope.frames[0].nodes.get_mut().clear();
|
||||
// scope.frames[1].nodes.get_mut().clear();
|
||||
|
||||
scope.frames[0].bump.reset();
|
||||
scope.frames[1].bump.reset();
|
||||
@@ -271,7 +272,7 @@ impl ScopeArena {
|
||||
///
|
||||
/// This also makes sure that drop order is consistent and predictable. All resources that rely on being dropped will
|
||||
/// be dropped.
|
||||
pub(crate) fn ensure_drop_safety(&self, scope_id: &ScopeId) {
|
||||
pub(crate) fn ensure_drop_safety(&self, scope_id: ScopeId) {
|
||||
if let Some(scope) = self.get_scope(scope_id) {
|
||||
let mut items = scope.items.borrow_mut();
|
||||
|
||||
@@ -284,7 +285,7 @@ impl ScopeArena {
|
||||
.get()
|
||||
.expect("VComponents should be associated with a valid Scope");
|
||||
|
||||
self.ensure_drop_safety(&scope_id);
|
||||
self.ensure_drop_safety(scope_id);
|
||||
|
||||
let mut drop_props = comp.drop_props.borrow_mut().take().unwrap();
|
||||
drop_props();
|
||||
@@ -298,7 +299,7 @@ impl ScopeArena {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn run_scope(&self, id: &ScopeId) -> bool {
|
||||
pub(crate) fn run_scope(&self, id: ScopeId) -> bool {
|
||||
// Cycle to the next frame and then reset it
|
||||
// This breaks any latent references, invalidating every pointer referencing into it.
|
||||
// Remove all the outdated listeners
|
||||
@@ -327,16 +328,20 @@ impl ScopeArena {
|
||||
debug_assert!(items.tasks.is_empty());
|
||||
|
||||
// Todo: see if we can add stronger guarantees around internal bookkeeping and failed component renders.
|
||||
scope.wip_frame().nodes.borrow_mut().clear();
|
||||
// scope.wip_frame().nodes
|
||||
}
|
||||
|
||||
let render: &dyn Fn(&ScopeState) -> Element = unsafe { &*scope.caller };
|
||||
|
||||
if let Some(link) = render(scope) {
|
||||
if let Some(node) = render(scope) {
|
||||
if !scope.items.borrow().tasks.is_empty() {
|
||||
self.pending_futures.borrow_mut().insert(*id);
|
||||
self.pending_futures.borrow_mut().insert(id);
|
||||
}
|
||||
|
||||
let frame = scope.wip_frame();
|
||||
let node = frame.bump.alloc(node);
|
||||
frame.nodes.set(unsafe { std::mem::transmute(node) });
|
||||
|
||||
// make the "wip frame" contents the "finished frame"
|
||||
// any future dipping into completed nodes after "render" will go through "fin head"
|
||||
scope.cycle_frame();
|
||||
@@ -371,24 +376,23 @@ impl ScopeArena {
|
||||
}
|
||||
|
||||
// The head of the bumpframe is the first linked NodeLink
|
||||
pub fn wip_head(&self, id: &ScopeId) -> &VNode {
|
||||
pub fn wip_head(&self, id: ScopeId) -> &VNode {
|
||||
let scope = self.get_scope(id).unwrap();
|
||||
let frame = scope.wip_frame();
|
||||
let nodes = frame.nodes.borrow();
|
||||
let node: &VNode = unsafe { &**nodes.get(0).unwrap() };
|
||||
let node = unsafe { &*frame.nodes.get() };
|
||||
unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
|
||||
}
|
||||
|
||||
// The head of the bumpframe is the first linked NodeLink
|
||||
pub fn fin_head(&self, id: &ScopeId) -> &VNode {
|
||||
pub fn fin_head(&self, id: ScopeId) -> &VNode {
|
||||
let scope = self.get_scope(id).unwrap();
|
||||
let frame = scope.fin_frame();
|
||||
let nodes = frame.nodes.borrow();
|
||||
let node: &VNode = unsafe { &**nodes.get(0).unwrap() };
|
||||
let node = unsafe { &*frame.nodes.get() };
|
||||
unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
|
||||
}
|
||||
|
||||
pub fn root_node(&self, id: &ScopeId) -> &VNode {
|
||||
pub fn root_node(&self, id: ScopeId) -> &VNode {
|
||||
self.fin_head(id)
|
||||
}
|
||||
}
|
||||
@@ -105,7 +105,8 @@ use std::{any::Any, collections::VecDeque, pin::Pin, sync::Arc, task::Poll};
|
||||
pub struct VirtualDom {
|
||||
base_scope: ScopeId,
|
||||
|
||||
_root_props: Box<dyn Any>,
|
||||
// it's stored here so the props are dropped when the VirtualDom is dropped
|
||||
_root_caller: Box<dyn for<'r> Fn(&'r ScopeState) -> Element<'r> + 'static>,
|
||||
|
||||
scopes: Box<ScopeArena>,
|
||||
|
||||
@@ -132,7 +133,7 @@ impl VirtualDom {
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust, ignore
|
||||
/// fn Example(cx: Context, props: &()) -> Element {
|
||||
/// fn Example(cx: Scope<()>) -> Element {
|
||||
/// cx.render(rsx!( div { "hello world" } ))
|
||||
/// }
|
||||
///
|
||||
@@ -161,8 +162,8 @@ impl VirtualDom {
|
||||
/// name: &'static str
|
||||
/// }
|
||||
///
|
||||
/// fn Example(cx: Context, props: &SomeProps) -> Element {
|
||||
/// cx.render(rsx!{ div{ "hello {cx.name}" } })
|
||||
/// fn Example(cx: Scope<SomeProps>) -> Element {
|
||||
/// cx.render(rsx!{ div{ "hello {cx.props.name}" } })
|
||||
/// }
|
||||
///
|
||||
/// let dom = VirtualDom::new(Example);
|
||||
@@ -194,31 +195,36 @@ impl VirtualDom {
|
||||
sender: UnboundedSender<SchedulerMsg>,
|
||||
receiver: UnboundedReceiver<SchedulerMsg>,
|
||||
) -> Self {
|
||||
let scopes = ScopeArena::new(sender.clone());
|
||||
|
||||
// move these two things onto the heap so we have stable ptrs
|
||||
let scopes = Box::new(ScopeArena::new(sender.clone()));
|
||||
let root_props = Box::new(root_props);
|
||||
let props_ref: *const P = root_props.as_ref();
|
||||
let mut caller: Box<dyn Fn(&ScopeState) -> Element> =
|
||||
Box::new(move |scp: &ScopeState| -> Element {
|
||||
let p = unsafe { &*props_ref };
|
||||
todo!()
|
||||
// root(Context {
|
||||
// scope: scp,
|
||||
// props: p,
|
||||
// })
|
||||
});
|
||||
let caller_ref: *mut dyn Fn(&ScopeState) -> Element = caller.as_mut();
|
||||
let base_scope = scopes.new_with_key(root as _, caller_ref, None, ElementId(0), 0, 0);
|
||||
|
||||
let pending_messages = VecDeque::new();
|
||||
// create the root caller which will properly drop its props when the VirtualDom is dropped
|
||||
let mut _root_caller: Box<dyn for<'r> Fn(&'r ScopeState) -> Element<'r> + 'static> =
|
||||
Box::new(move |scope: &ScopeState| -> Element {
|
||||
// Safety: The props at this pointer can never be moved.
|
||||
// Also, this closure will never be ran when the VirtualDom is destroyed.
|
||||
// This is where the root lifetime of the VirtualDom originates.
|
||||
let props: *const P = root_props.as_ref();
|
||||
let props = unsafe { &*props };
|
||||
root(Scope { scope, props })
|
||||
});
|
||||
|
||||
// safety: the raw pointer is aliased or used after this point.
|
||||
let caller: *mut dyn Fn(&ScopeState) -> Element = _root_caller.as_mut();
|
||||
let base_scope = scopes.new_with_key(root as _, caller, None, ElementId(0), 0, 0);
|
||||
|
||||
let mut dirty_scopes = IndexSet::new();
|
||||
dirty_scopes.insert(base_scope);
|
||||
|
||||
// todo: add a pending message to the scheduler to start the scheduler?
|
||||
let pending_messages = VecDeque::new();
|
||||
|
||||
Self {
|
||||
scopes: Box::new(scopes),
|
||||
scopes,
|
||||
base_scope,
|
||||
receiver,
|
||||
_root_props: root_props,
|
||||
_root_caller,
|
||||
pending_messages,
|
||||
dirty_scopes,
|
||||
sender,
|
||||
@@ -232,16 +238,16 @@ impl VirtualDom {
|
||||
///
|
||||
/// # Example
|
||||
pub fn base_scope(&self) -> &ScopeState {
|
||||
self.get_scope(&self.base_scope).unwrap()
|
||||
self.get_scope(self.base_scope).unwrap()
|
||||
}
|
||||
|
||||
/// Get the [`Scope`] for a component given its [`ScopeId`]
|
||||
/// Get the [`ScopeState`] for a component given its [`ScopeId`]
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
///
|
||||
///
|
||||
pub fn get_scope<'a>(&'a self, id: &ScopeId) -> Option<&'a ScopeState> {
|
||||
pub fn get_scope<'a>(&'a self, id: ScopeId) -> Option<&'a ScopeState> {
|
||||
self.scopes.get_scope(id)
|
||||
}
|
||||
|
||||
@@ -398,11 +404,11 @@ impl VirtualDom {
|
||||
|
||||
// Sort the scopes by height. Theoretically, we'll de-duplicate scopes by height
|
||||
self.dirty_scopes
|
||||
.retain(|id| scopes.get_scope(id).is_some());
|
||||
.retain(|id| scopes.get_scope(*id).is_some());
|
||||
|
||||
self.dirty_scopes.sort_by(|a, b| {
|
||||
let h1 = scopes.get_scope(a).unwrap().height;
|
||||
let h2 = scopes.get_scope(b).unwrap().height;
|
||||
let h1 = scopes.get_scope(*a).unwrap().height;
|
||||
let h2 = scopes.get_scope(*b).unwrap().height;
|
||||
h1.cmp(&h2).reverse()
|
||||
});
|
||||
|
||||
@@ -410,15 +416,13 @@ impl VirtualDom {
|
||||
if !ran_scopes.contains(&scopeid) {
|
||||
ran_scopes.insert(scopeid);
|
||||
|
||||
if self.scopes.run_scope(&scopeid) {
|
||||
let (old, new) = (
|
||||
self.scopes.wip_head(&scopeid),
|
||||
self.scopes.fin_head(&scopeid),
|
||||
);
|
||||
if self.scopes.run_scope(scopeid) {
|
||||
let (old, new) =
|
||||
(self.scopes.wip_head(scopeid), self.scopes.fin_head(scopeid));
|
||||
diff_state.stack.push(DiffInstruction::Diff { new, old });
|
||||
diff_state.stack.scope_stack.push(scopeid);
|
||||
|
||||
let scope = scopes.get_scope(&scopeid).unwrap();
|
||||
let scope = scopes.get_scope(scopeid).unwrap();
|
||||
diff_state.stack.element_stack.push(scope.container);
|
||||
}
|
||||
}
|
||||
@@ -465,10 +469,10 @@ impl VirtualDom {
|
||||
let mut diff_state = DiffState::new(&self.scopes);
|
||||
|
||||
let scope_id = self.base_scope;
|
||||
if self.scopes.run_scope(&scope_id) {
|
||||
if self.scopes.run_scope(scope_id) {
|
||||
diff_state
|
||||
.stack
|
||||
.create_node(self.scopes.fin_head(&scope_id), MountType::Append);
|
||||
.create_node(self.scopes.fin_head(scope_id), MountType::Append);
|
||||
|
||||
diff_state.stack.element_stack.push(ElementId(0));
|
||||
diff_state.stack.scope_stack.push(scope_id);
|
||||
@@ -507,7 +511,7 @@ impl VirtualDom {
|
||||
///
|
||||
/// let edits = dom.diff();
|
||||
/// ```
|
||||
pub fn hard_diff<'a>(&'a mut self, scope_id: &ScopeId) -> Option<Mutations<'a>> {
|
||||
pub fn hard_diff<'a>(&'a mut self, scope_id: ScopeId) -> Option<Mutations<'a>> {
|
||||
let mut diff_machine = DiffState::new(&self.scopes);
|
||||
if self.scopes.run_scope(scope_id) {
|
||||
diff_machine.force_diff = true;
|
||||
@@ -529,7 +533,7 @@ impl VirtualDom {
|
||||
/// let nodes = dom.render_nodes(rsx!("div"));
|
||||
/// ```
|
||||
pub fn render_vnodes<'a>(&'a self, lazy_nodes: Option<LazyNodes<'a, '_>>) -> &'a VNode<'a> {
|
||||
let scope = self.scopes.get_scope(&self.base_scope).unwrap();
|
||||
let scope = self.scopes.get_scope(self.base_scope).unwrap();
|
||||
let frame = scope.wip_frame();
|
||||
let factory = NodeFactory { bump: &frame.bump };
|
||||
let node = lazy_nodes.unwrap().call(factory);
|
||||
@@ -741,7 +745,7 @@ impl<'a> Future for PollTasks<'a> {
|
||||
let mut scopes_to_clear: SmallVec<[_; 10]> = smallvec::smallvec![];
|
||||
|
||||
// Poll every scope manually
|
||||
for fut in self.0.pending_futures.borrow().iter() {
|
||||
for fut in self.0.pending_futures.borrow().iter().copied() {
|
||||
let scope = self.0.get_scope(fut).expect("Scope should never be moved");
|
||||
|
||||
let mut items = scope.items.borrow_mut();
|
||||
@@ -756,7 +760,7 @@ impl<'a> Future for PollTasks<'a> {
|
||||
}
|
||||
|
||||
if unfinished_tasks.is_empty() {
|
||||
scopes_to_clear.push(*fut);
|
||||
scopes_to_clear.push(fut);
|
||||
}
|
||||
|
||||
items.tasks.extend(unfinished_tasks.drain(..));
|
||||
|
||||
Reference in new issue
Block a user