Merge branch 'master' of github.com:DioxusLabs/dioxus into router-2

This commit is contained in:
Adrian Wannenmacher 2022-12-17 19:02:26 +01:00
commit 13239d0e4b
No known key found for this signature in database
GPG key ID: 19D986ECB1E492D5
12 changed files with 272 additions and 295 deletions

View file

@ -48,7 +48,7 @@ fn App(cx: Scope) -> Element {
}} }}
" } " }
h1 {"Dioxus CRM Example"} h1 { "Dioxus CRM Example" }
Outlet { } Outlet { }
} }

View file

@ -9,7 +9,6 @@ fn main() {
let cfg = Config::new().with_window( let cfg = Config::new().with_window(
WindowBuilder::new() WindowBuilder::new()
.with_title("Spinsense Client")
.with_inner_size(LogicalSize::new(600, 1000)) .with_inner_size(LogicalSize::new(600, 1000))
.with_resizable(false), .with_resizable(false),
); );
@ -26,6 +25,7 @@ fn app(cx: Scope) -> Element {
}); });
render! { render! {
div {
Outlet { } Outlet { }
p { p {
@ -41,6 +41,7 @@ fn app(cx: Scope) -> Element {
} }
} }
} }
}
} }
fn Home(cx: Scope) -> Element { fn Home(cx: Scope) -> Element {

View file

@ -1,4 +1,7 @@
use crate::{nodes::RenderReturn, nodes::VNode, virtual_dom::VirtualDom, DynamicNode, ScopeId}; use crate::{
nodes::RenderReturn, nodes::VNode, virtual_dom::VirtualDom, AttributeValue, DynamicNode,
ScopeId,
};
use bumpalo::boxed::Box as BumpBox; use bumpalo::boxed::Box as BumpBox;
/// An Element's unique identifier. /// An Element's unique identifier.
@ -75,11 +78,18 @@ impl VirtualDom {
// Drop a scope and all its children // Drop a scope and all its children
pub(crate) fn drop_scope(&mut self, id: ScopeId) { pub(crate) fn drop_scope(&mut self, id: ScopeId) {
self.ensure_drop_safety(id);
if let Some(root) = self.scopes[id.0].as_ref().try_root_node() { if let Some(root) = self.scopes[id.0].as_ref().try_root_node() {
if let RenderReturn::Sync(Ok(node)) = unsafe { root.extend_lifetime_ref() } { if let RenderReturn::Sync(Ok(node)) = unsafe { root.extend_lifetime_ref() } {
self.drop_scope_inner(node) self.drop_scope_inner(node)
} }
} }
if let Some(root) = unsafe { self.scopes[id.0].as_ref().previous_frame().try_load_node() } {
if let RenderReturn::Sync(Ok(node)) = unsafe { root.extend_lifetime_ref() } {
self.drop_scope_inner(node)
}
}
self.scopes[id.0].props.take(); self.scopes[id.0].props.take();
@ -97,8 +107,9 @@ impl VirtualDom {
node.dynamic_nodes.iter().for_each(|node| match node { node.dynamic_nodes.iter().for_each(|node| match node {
DynamicNode::Component(c) => { DynamicNode::Component(c) => {
if let Some(f) = c.scope.get() { if let Some(f) = c.scope.get() {
self.drop_scope(f) self.drop_scope(f);
} }
c.props.take();
} }
DynamicNode::Fragment(nodes) => { DynamicNode::Fragment(nodes) => {
nodes.iter().for_each(|node| self.drop_scope_inner(node)) nodes.iter().for_each(|node| self.drop_scope_inner(node))
@ -122,31 +133,27 @@ impl VirtualDom {
/// Descend through the tree, removing any borrowed props and listeners /// Descend through the tree, removing any borrowed props and listeners
pub(crate) fn ensure_drop_safety(&self, scope: ScopeId) { pub(crate) fn ensure_drop_safety(&self, scope: ScopeId) {
let node = unsafe { self.scopes[scope.0].previous_frame().try_load_node() }; let scope = &self.scopes[scope.0];
// And now we want to make sure the previous frame has dropped anything that borrows self // make sure we drop all borrowed props manually to guarantee that their drop implementation is called before we
if let Some(RenderReturn::Sync(Ok(node))) = node { // run the hooks (which hold an &mut Reference)
self.ensure_drop_safety_inner(node); // recursively call ensure_drop_safety on all children
let mut props = scope.borrowed_props.borrow_mut();
props.drain(..).for_each(|comp| {
let comp = unsafe { &*comp };
if let Some(scope_id) = comp.scope.get() {
self.ensure_drop_safety(scope_id);
} }
drop(comp.props.take());
});
// Now that all the references are gone, we can safely drop our own references in our listeners.
let mut listeners = scope.listeners.borrow_mut();
listeners.drain(..).for_each(|listener| {
let listener = unsafe { &*listener };
if let AttributeValue::Listener(l) = &listener.value {
_ = l.take();
} }
fn ensure_drop_safety_inner(&self, node: &VNode) {
node.clear_listeners();
node.dynamic_nodes.iter().for_each(|child| match child {
// Only descend if the props are borrowed
DynamicNode::Component(c) if !c.static_props => {
if let Some(scope) = c.scope.get() {
self.ensure_drop_safety(scope);
}
c.props.take();
}
DynamicNode::Fragment(f) => f
.iter()
.for_each(|node| self.ensure_drop_safety_inner(node)),
_ => {}
}); });
} }
} }

View file

@ -1,3 +1,4 @@
use crate::any_props::AnyProps;
use crate::innerlude::{VComponent, VPlaceholder, VText}; use crate::innerlude::{VComponent, VPlaceholder, VText};
use crate::mutations::Mutation; use crate::mutations::Mutation;
use crate::mutations::Mutation::*; use crate::mutations::Mutation::*;
@ -43,7 +44,10 @@ impl<'b> VirtualDom {
.iter() .iter()
.enumerate() .enumerate()
.map(|(idx, root)| match root { .map(|(idx, root)| match root {
DynamicText { id } | Dynamic { id } => self.write_dynamic_root(node, *id), DynamicText { id } | Dynamic { id } => {
nodes.next().unwrap();
self.write_dynamic_root(node, *id)
}
Element { .. } => self.write_element_root(node, idx, &mut attrs, &mut nodes), Element { .. } => self.write_element_root(node, idx, &mut attrs, &mut nodes),
Text { .. } => self.write_static_text_root(node, idx), Text { .. } => self.write_static_text_root(node, idx),
}) })
@ -133,7 +137,7 @@ impl<'b> VirtualDom {
None => return, None => return,
}; };
for idx in start..=end { for idx in (start..=end).rev() {
let m = self.create_dynamic_node(template, &template.dynamic_nodes[idx], idx); let m = self.create_dynamic_node(template, &template.dynamic_nodes[idx], idx);
if m > 0 { if m > 0 {
// The path is one shorter because the top node is the root // The path is one shorter because the top node is the root
@ -170,12 +174,12 @@ impl<'b> VirtualDom {
attribute.mounted_element.set(id); attribute.mounted_element.set(id);
// Safety: we promise not to re-alias this text later on after committing it to the mutation // Safety: we promise not to re-alias this text later on after committing it to the mutation
let unbounded_name = unsafe { std::mem::transmute(attribute.name) }; let unbounded_name: &str = unsafe { std::mem::transmute(attribute.name) };
match &attribute.value { match &attribute.value {
AttributeValue::Text(value) => { AttributeValue::Text(value) => {
// Safety: we promise not to re-alias this text later on after committing it to the mutation // Safety: we promise not to re-alias this text later on after committing it to the mutation
let unbounded_value = unsafe { std::mem::transmute(*value) }; let unbounded_value: &str = unsafe { std::mem::transmute(*value) };
self.mutations.push(SetAttribute { self.mutations.push(SetAttribute {
name: unbounded_name, name: unbounded_name,
@ -334,7 +338,7 @@ impl<'b> VirtualDom {
) -> usize { ) -> usize {
let scope = match component.props.take() { let scope = match component.props.take() {
Some(props) => { Some(props) => {
let unbounded_props = unsafe { std::mem::transmute(props) }; let unbounded_props: Box<dyn AnyProps> = unsafe { std::mem::transmute(props) };
let scope = self.new_scope(unbounded_props, component.name); let scope = self.new_scope(unbounded_props, component.name);
scope.id scope.id
} }

View file

@ -1,4 +1,5 @@
use crate::{ use crate::{
any_props::AnyProps,
arena::ElementId, arena::ElementId,
innerlude::{DirtyScope, VComponent, VPlaceholder, VText}, innerlude::{DirtyScope, VComponent, VPlaceholder, VText},
mutations::Mutation, mutations::Mutation,
@ -141,32 +142,6 @@ impl<'b> VirtualDom {
} }
} }
fn replace_placeholder(&mut self, l: &'b VPlaceholder, r: &'b [VNode<'b>]) {
let m = self.create_children(r);
let id = l.id.get().unwrap();
self.mutations.push(Mutation::ReplaceWith { id, m });
self.reclaim(id);
}
fn node_to_placeholder(&mut self, l: &'b [VNode<'b>], r: &'b VPlaceholder) {
// Remove the old nodes, except for one
self.remove_nodes(&l[1..]);
// Now create the new one
let first = self.replace_inner(&l[0]);
// Create the placeholder first, ensuring we get a dedicated ID for the placeholder
let placeholder = self.next_element(&l[0], &[]);
r.id.set(Some(placeholder));
self.mutations
.push(Mutation::CreatePlaceholder { id: placeholder });
self.mutations
.push(Mutation::ReplaceWith { id: first, m: 1 });
self.try_reclaim(first);
}
fn diff_vcomponent( fn diff_vcomponent(
&mut self, &mut self,
left: &'b VComponent<'b>, left: &'b VComponent<'b>,
@ -186,33 +161,27 @@ impl<'b> VirtualDom {
.root_node() .root_node()
.extend_lifetime_ref() .extend_lifetime_ref()
}; };
let id = match head { let last = match head {
RenderReturn::Sync(Ok(node)) => self.replace_inner(node), RenderReturn::Sync(Ok(node)) => self.find_last_element(node),
_ => todo!(), _ => todo!(),
}; };
self.mutations self.mutations.push(Mutation::InsertAfter {
.push(Mutation::ReplaceWith { id, m: created }); id: last,
self.drop_scope(left.scope.get().unwrap()); m: created,
});
self.remove_component_node(left, true);
return; return;
} }
// Make sure the new vcomponent has the right scopeid associated to it // Make sure the new vcomponent has the right scopeid associated to it
let Some(scope_id) = left.scope.get() else { let scope_id = left.scope.get().unwrap();
return;
};
// let scope_id = left.scope.get().unwrap_or_else(|| {
// panic!(
// "A component should always have a scope associated to it. {:?}\n {:#?}",
// right.name,
// std::backtrace::Backtrace::force_capture()
// )
// });
right.scope.set(Some(scope_id)); right.scope.set(Some(scope_id));
// copy out the box for both // copy out the box for both
let old = self.scopes[scope_id.0].props.as_ref(); let old = self.scopes[scope_id.0].props.as_ref();
let new = right.props.take().unwrap(); let new: Box<dyn AnyProps> = right.props.take().unwrap();
let new: Box<dyn AnyProps> = unsafe { std::mem::transmute(new) };
// If the props are static, then we try to memoize by setting the new with the old // If the props are static, then we try to memoize by setting the new with the old
// The target scopestate still has the reference to the old props, so there's no need to update anything // The target scopestate still has the reference to the old props, so there's no need to update anything
@ -222,7 +191,7 @@ impl<'b> VirtualDom {
} }
// First, move over the props from the old to the new, dropping old props in the process // First, move over the props from the old to the new, dropping old props in the process
self.scopes[scope_id.0].props = unsafe { std::mem::transmute(new) }; self.scopes[scope_id.0].props = Some(new);
// Now run the component and diff it // Now run the component and diff it
self.run_scope(scope_id); self.run_scope(scope_id);
@ -273,7 +242,7 @@ impl<'b> VirtualDom {
/// ``` /// ```
fn light_diff_templates(&mut self, left: &'b VNode<'b>, right: &'b VNode<'b>) { fn light_diff_templates(&mut self, left: &'b VNode<'b>, right: &'b VNode<'b>) {
match matching_components(left, right) { match matching_components(left, right) {
None => self.replace(left, right), None => self.replace(left, [right]),
Some(components) => components Some(components) => components
.into_iter() .into_iter()
.enumerate() .enumerate()
@ -298,116 +267,6 @@ impl<'b> VirtualDom {
} }
} }
/// Remove all the top-level nodes, returning the firstmost root ElementId
///
/// All IDs will be garbage collected
fn replace_inner(&mut self, node: &'b VNode<'b>) -> ElementId {
let id = match node.dynamic_root(0) {
None => node.root_ids[0].get().unwrap(),
Some(Text(t)) => t.id.get().unwrap(),
Some(Placeholder(e)) => e.id.get().unwrap(),
Some(Fragment(nodes)) => {
let id = self.replace_inner(&nodes[0]);
self.remove_nodes(&nodes[1..]);
id
}
Some(Component(comp)) => {
let scope = comp.scope.get().unwrap();
match unsafe { self.scopes[scope.0].root_node().extend_lifetime_ref() } {
RenderReturn::Sync(Ok(t)) => self.replace_inner(t),
_ => todo!("cannot handle nonstandard nodes"),
}
}
};
// Just remove the rest from the dom
for (idx, _) in node.template.roots.iter().enumerate().skip(1) {
self.remove_root_node(node, idx);
}
// Garabge collect all of the nodes since this gets used in replace
self.clean_up_node(node);
id
}
/// Clean up the node, not generating mutations
///
/// Simply walks through the dynamic nodes
fn clean_up_node(&mut self, node: &'b VNode<'b>) {
for (idx, dyn_node) in node.dynamic_nodes.iter().enumerate() {
// Roots are cleaned up automatically?
if node.template.node_paths[idx].len() == 1 {
continue;
}
match dyn_node {
Component(comp) => {
if let Some(scope) = comp.scope.take() {
match unsafe { self.scopes[scope.0].root_node().extend_lifetime_ref() } {
RenderReturn::Sync(Ok(t)) => self.clean_up_node(t),
_ => todo!("cannot handle nonstandard nodes"),
};
}
}
Text(t) => {
if let Some(id) = t.id.take() {
self.reclaim(id)
}
}
Placeholder(t) => self.reclaim(t.id.take().unwrap()),
Fragment(nodes) => nodes.iter().for_each(|node| self.clean_up_node(node)),
};
}
// we clean up nodes with dynamic attributes, provided the node is unique and not a root node
let mut id = None;
for (idx, attr) in node.dynamic_attrs.iter().enumerate() {
// We'll clean up the root nodes either way, so don't worry
if node.template.attr_paths[idx].len() == 1 {
continue;
}
let next_id = attr.mounted_element.get();
if id == Some(next_id) {
continue;
}
id = Some(next_id);
self.reclaim(next_id);
}
}
fn remove_root_node(&mut self, node: &'b VNode<'b>, idx: usize) {
match node.dynamic_root(idx) {
Some(Text(i)) => {
let id = i.id.take().unwrap();
self.mutations.push(Mutation::Remove { id });
self.reclaim(id);
}
Some(Placeholder(e)) => {
let id = e.id.take().unwrap();
self.mutations.push(Mutation::Remove { id });
self.reclaim(id);
}
Some(Fragment(nodes)) => self.remove_nodes(nodes),
Some(Component(comp)) => {
let scope = comp.scope.take().unwrap();
match unsafe { self.scopes[scope.0].root_node().extend_lifetime_ref() } {
RenderReturn::Sync(Ok(t)) => self.remove_node(t),
_ => todo!("cannot handle nonstandard nodes"),
};
}
None => {
let id = node.root_ids[idx].get().unwrap();
self.mutations.push(Mutation::Remove { id });
self.reclaim(id);
}
};
}
fn diff_non_empty_fragment(&mut self, old: &'b [VNode<'b>], new: &'b [VNode<'b>]) { fn diff_non_empty_fragment(&mut self, old: &'b [VNode<'b>], new: &'b [VNode<'b>]) {
let new_is_keyed = new[0].key.is_some(); let new_is_keyed = new[0].key.is_some();
let old_is_keyed = old[0].key.is_some(); let old_is_keyed = old[0].key.is_some();
@ -658,7 +517,7 @@ impl<'b> VirtualDom {
if shared_keys.is_empty() { if shared_keys.is_empty() {
if old.get(0).is_some() { if old.get(0).is_some() {
self.remove_nodes(&old[1..]); self.remove_nodes(&old[1..]);
self.replace_many(&old[0], new); self.replace(&old[0], new);
} else { } else {
// I think this is wrong - why are we appending? // I think this is wrong - why are we appending?
// only valid of the if there are no trailing elements // only valid of the if there are no trailing elements
@ -674,7 +533,7 @@ impl<'b> VirtualDom {
for child in old { for child in old {
let key = child.key.unwrap(); let key = child.key.unwrap();
if !shared_keys.contains(&key) { if !shared_keys.contains(&key) {
self.remove_node(child); self.remove_node(child, true);
} }
} }
@ -777,54 +636,6 @@ impl<'b> VirtualDom {
} }
} }
/// Remove these nodes from the dom
/// Wont generate mutations for the inner nodes
fn remove_nodes(&mut self, nodes: &'b [VNode<'b>]) {
// note that we iterate in reverse to unlink lists of nodes in their rough index order
nodes.iter().rev().for_each(|node| self.remove_node(node));
}
fn remove_node(&mut self, node: &'b VNode<'b>) {
for (idx, _) in node.template.roots.iter().enumerate() {
let id = match node.dynamic_root(idx) {
Some(Text(t)) => t.id.take(),
Some(Placeholder(t)) => t.id.take(),
Some(Fragment(t)) => return self.remove_nodes(t),
Some(Component(comp)) => {
comp.scope.set(None);
return self.remove_component(comp.scope.get().unwrap());
}
None => node.root_ids[idx].get(),
}
.unwrap();
self.mutations.push(Mutation::Remove { id })
}
self.clean_up_node(node);
for root in node.root_ids {
let id = root.get().unwrap();
if id.0 != 0 {
self.reclaim(id);
}
}
}
fn remove_component(&mut self, scope_id: ScopeId) {
let height = self.scopes[scope_id.0].height;
self.dirty_scopes.remove(&DirtyScope {
height,
id: scope_id,
});
// I promise, since we're descending down the tree, this is safe
match unsafe { self.scopes[scope_id.0].root_node().extend_lifetime_ref() } {
RenderReturn::Sync(Ok(t)) => self.remove_node(t),
_ => todo!("cannot handle nonstandard nodes"),
}
}
/// Push all the real nodes on the stack /// Push all the real nodes on the stack
fn push_all_real_nodes(&mut self, node: &'b VNode<'b>) -> usize { fn push_all_real_nodes(&mut self, node: &'b VNode<'b>) -> usize {
node.template node.template
@ -832,44 +643,50 @@ impl<'b> VirtualDom {
.iter() .iter()
.enumerate() .enumerate()
.map(|(idx, _)| { .map(|(idx, _)| {
match node.dynamic_root(idx) { let node = match node.dynamic_root(idx) {
Some(Text(t)) => { Some(node) => node,
None => {
self.mutations.push(Mutation::PushRoot {
id: node.root_ids[idx].get().unwrap(),
});
return 1;
}
};
match node {
Text(t) => {
self.mutations.push(Mutation::PushRoot { self.mutations.push(Mutation::PushRoot {
id: t.id.get().unwrap(), id: t.id.get().unwrap(),
}); });
1 1
} }
Some(Placeholder(t)) => { Placeholder(t) => {
self.mutations.push(Mutation::PushRoot { self.mutations.push(Mutation::PushRoot {
id: t.id.get().unwrap(), id: t.id.get().unwrap(),
}); });
1 1
} }
Some(Fragment(nodes)) => nodes Fragment(nodes) => nodes
.iter() .iter()
.map(|node| self.push_all_real_nodes(node)) .map(|node| self.push_all_real_nodes(node))
.count(), .count(),
Some(Component(comp)) => { Component(comp) => {
let scope = comp.scope.get().unwrap(); let scope = comp.scope.get().unwrap();
match unsafe { self.scopes[scope.0].root_node().extend_lifetime_ref() } { match unsafe { self.scopes[scope.0].root_node().extend_lifetime_ref() } {
RenderReturn::Sync(Ok(node)) => self.push_all_real_nodes(node), RenderReturn::Sync(Ok(node)) => self.push_all_real_nodes(node),
_ => todo!(), _ => todo!(),
} }
} }
None => {
self.mutations.push(Mutation::PushRoot {
id: node.root_ids[idx].get().unwrap(),
});
1
} }
};
}) })
.count() .count()
} }
fn create_children(&mut self, nodes: &'b [VNode<'b>]) -> usize { fn create_children(&mut self, nodes: impl IntoIterator<Item = &'b VNode<'b>>) -> usize {
nodes.iter().fold(0, |acc, child| acc + self.create(child)) nodes
.into_iter()
.fold(0, |acc, child| acc + self.create(child))
} }
fn create_and_insert_before(&mut self, new: &'b [VNode<'b>], before: &'b VNode<'b>) { fn create_and_insert_before(&mut self, new: &'b [VNode<'b>], before: &'b VNode<'b>) {
@ -884,6 +701,134 @@ impl<'b> VirtualDom {
self.mutations.push(Mutation::InsertAfter { id, m }) self.mutations.push(Mutation::InsertAfter { id, m })
} }
/// Simply replace a placeholder with a list of nodes
fn replace_placeholder(&mut self, l: &'b VPlaceholder, r: &'b [VNode<'b>]) {
let m = self.create_children(r);
let id = l.id.get().unwrap();
self.mutations.push(Mutation::ReplaceWith { id, m });
self.reclaim(id);
}
fn replace(&mut self, left: &'b VNode<'b>, right: impl IntoIterator<Item = &'b VNode<'b>>) {
let m = self.create_children(right);
let id = self.find_last_element(left);
self.mutations.push(Mutation::InsertAfter { id, m });
self.remove_node(left, true);
}
fn node_to_placeholder(&mut self, l: &'b [VNode<'b>], r: &'b VPlaceholder) {
// Create the placeholder first, ensuring we get a dedicated ID for the placeholder
let placeholder = self.next_element(&l[0], &[]);
r.id.set(Some(placeholder));
let id = self.find_last_element(&l[0]);
self.mutations
.push(Mutation::CreatePlaceholder { id: placeholder });
self.mutations.push(Mutation::InsertAfter { id, m: 1 });
self.remove_nodes(l);
}
/// Remove these nodes from the dom
/// Wont generate mutations for the inner nodes
fn remove_nodes(&mut self, nodes: &'b [VNode<'b>]) {
nodes.iter().for_each(|node| self.remove_node(node, true));
}
fn remove_node(&mut self, node: &'b VNode<'b>, gen_muts: bool) {
// Clean up the roots, assuming we need to generate mutations for these
for (idx, _) in node.template.roots.iter().enumerate() {
if let Some(dy) = node.dynamic_root(idx) {
self.remove_dynamic_node(dy, gen_muts);
} else {
let id = node.root_ids[idx].get().unwrap();
if gen_muts {
self.mutations.push(Mutation::Remove { id });
}
self.reclaim(id);
}
}
for (idx, dyn_node) in node.dynamic_nodes.iter().enumerate() {
// Roots are cleaned up automatically above
if node.template.node_paths[idx].len() == 1 {
continue;
}
self.remove_dynamic_node(dyn_node, false);
}
// we clean up nodes with dynamic attributes, provided the node is unique and not a root node
let mut id = None;
for (idx, attr) in node.dynamic_attrs.iter().enumerate() {
// We'll clean up the root nodes either way, so don't worry
if node.template.attr_paths[idx].len() == 1 {
continue;
}
let next_id = attr.mounted_element.get();
if id == Some(next_id) {
continue;
}
id = Some(next_id);
self.reclaim(next_id);
}
}
fn remove_dynamic_node(&mut self, node: &DynamicNode, gen_muts: bool) {
match node {
Component(comp) => self.remove_component_node(comp, gen_muts),
Text(t) => self.remove_text_node(t),
Placeholder(t) => self.remove_placeholder(t),
Fragment(nodes) => nodes
.iter()
.for_each(|node| self.remove_node(node, gen_muts)),
};
}
fn remove_placeholder(&mut self, t: &VPlaceholder) {
if let Some(id) = t.id.take() {
self.reclaim(id)
}
}
fn remove_text_node(&mut self, t: &VText) {
if let Some(id) = t.id.take() {
self.reclaim(id)
}
}
fn remove_component_node(&mut self, comp: &VComponent, gen_muts: bool) {
if let Some(scope) = comp.scope.take() {
match unsafe { self.scopes[scope.0].root_node().extend_lifetime_ref() } {
RenderReturn::Sync(Ok(t)) => self.remove_node(t, gen_muts),
_ => todo!("cannot handle nonstandard nodes"),
};
let props = self.scopes[scope.0].props.take();
self.dirty_scopes.remove(&DirtyScope {
height: self.scopes[scope.0].height,
id: scope,
});
*comp.props.borrow_mut() = unsafe { std::mem::transmute(props) };
// make sure to wipe any of its props and listeners
self.ensure_drop_safety(scope);
self.scopes.remove(scope.0);
}
}
fn find_first_element(&self, node: &'b VNode<'b>) -> ElementId { fn find_first_element(&self, node: &'b VNode<'b>) -> ElementId {
match node.dynamic_root(0) { match node.dynamic_root(0) {
None => node.root_ids[0].get().unwrap(), None => node.root_ids[0].get().unwrap(),
@ -915,28 +860,6 @@ impl<'b> VirtualDom {
} }
} }
} }
fn replace(&mut self, left: &'b VNode<'b>, right: &'b VNode<'b>) {
let first = self.find_first_element(left);
let id = self.replace_inner(left);
let created = self.create(right);
self.mutations.push(Mutation::ReplaceWith {
id: first,
m: created,
});
self.try_reclaim(id);
}
fn replace_many(&mut self, left: &'b VNode<'b>, right: &'b [VNode<'b>]) {
let first = self.find_first_element(left);
let id = self.replace_inner(left);
let created = self.create_children(right);
self.mutations.push(Mutation::ReplaceWith {
id: first,
m: created,
});
self.try_reclaim(id);
}
} }
/// Are the templates the same? /// Are the templates the same?

View file

@ -25,7 +25,11 @@ use crate::{innerlude::VNode, ScopeState};
/// LazyNodes::new(|f| f.element("div", [], [], [] None)) /// LazyNodes::new(|f| f.element("div", [], [], [] None))
/// ``` /// ```
pub struct LazyNodes<'a, 'b> { pub struct LazyNodes<'a, 'b> {
#[cfg(not(miri))]
inner: SmallBox<dyn FnMut(&'a ScopeState) -> VNode<'a> + 'b, S16>, inner: SmallBox<dyn FnMut(&'a ScopeState) -> VNode<'a> + 'b, S16>,
#[cfg(miri)]
inner: Box<dyn FnMut(&'a ScopeState) -> VNode<'a> + 'b>,
} }
impl<'a, 'b> LazyNodes<'a, 'b> { impl<'a, 'b> LazyNodes<'a, 'b> {
@ -39,10 +43,17 @@ impl<'a, 'b> LazyNodes<'a, 'b> {
let mut slot = Some(val); let mut slot = Some(val);
Self { Self {
#[cfg(not(miri))]
inner: smallbox!(move |f| { inner: smallbox!(move |f| {
let val = slot.take().expect("cannot call LazyNodes twice"); let val = slot.take().expect("cannot call LazyNodes twice");
val(f) val(f)
}), }),
#[cfg(miri)]
inner: Box::new(move |f| {
let val = slot.take().expect("cannot call LazyNodes twice");
val(f)
}),
} }
} }

View file

@ -19,7 +19,11 @@ impl VirtualDom {
/// queue /// queue
pub(crate) fn handle_task_wakeup(&mut self, id: TaskId) { pub(crate) fn handle_task_wakeup(&mut self, id: TaskId) {
let mut tasks = self.scheduler.tasks.borrow_mut(); let mut tasks = self.scheduler.tasks.borrow_mut();
let task = &tasks[id.0];
let task = match tasks.get(id.0) {
Some(task) => task,
None => return,
};
let waker = task.waker(); let waker = task.waker();
let mut cx = Context::from_waker(&waker); let mut cx = Context::from_waker(&waker);

View file

@ -32,8 +32,9 @@ impl VirtualDom {
parent, parent,
id, id,
height, height,
props: Some(props),
name, name,
props: Some(props),
tasks: self.scheduler.clone(),
placeholder: Default::default(), placeholder: Default::default(),
node_arena_1: BumpFrame::new(0), node_arena_1: BumpFrame::new(0),
node_arena_2: BumpFrame::new(0), node_arena_2: BumpFrame::new(0),
@ -43,7 +44,8 @@ impl VirtualDom {
hook_list: Default::default(), hook_list: Default::default(),
hook_idx: Default::default(), hook_idx: Default::default(),
shared_contexts: Default::default(), shared_contexts: Default::default(),
tasks: self.scheduler.clone(), borrowed_props: Default::default(),
listeners: Default::default(),
})) }))
} }
@ -75,7 +77,7 @@ impl VirtualDom {
scope.hook_idx.set(0); scope.hook_idx.set(0);
// safety: due to how we traverse the tree, we know that the scope is not currently aliased // safety: due to how we traverse the tree, we know that the scope is not currently aliased
let props = scope.props.as_ref().unwrap().as_ref(); let props: &dyn AnyProps = scope.props.as_ref().unwrap().as_ref();
let props: &dyn AnyProps = mem::transmute(props); let props: &dyn AnyProps = mem::transmute(props);
props.render(scope).extend_lifetime() props.render(scope).extend_lifetime()
}; };

View file

@ -87,6 +87,9 @@ pub struct ScopeState {
pub(crate) tasks: Rc<Scheduler>, pub(crate) tasks: Rc<Scheduler>,
pub(crate) spawned_tasks: FxHashSet<TaskId>, pub(crate) spawned_tasks: FxHashSet<TaskId>,
pub(crate) borrowed_props: RefCell<Vec<*const VComponent<'static>>>,
pub(crate) listeners: RefCell<Vec<*const Attribute<'static>>>,
pub(crate) props: Option<Box<dyn AnyProps<'static>>>, pub(crate) props: Option<Box<dyn AnyProps<'static>>>,
pub(crate) placeholder: Cell<Option<ElementId>>, pub(crate) placeholder: Cell<Option<ElementId>>,
} }
@ -369,7 +372,25 @@ impl<'src> ScopeState {
/// } /// }
///``` ///```
pub fn render(&'src self, rsx: LazyNodes<'src, '_>) -> Element<'src> { pub fn render(&'src self, rsx: LazyNodes<'src, '_>) -> Element<'src> {
Ok(rsx.call(self)) let element = rsx.call(self);
let mut listeners = self.listeners.borrow_mut();
for attr in element.dynamic_attrs {
if let AttributeValue::Listener(_) = attr.value {
let unbounded = unsafe { std::mem::transmute(attr as *const Attribute) };
listeners.push(unbounded);
}
}
let mut props = self.borrowed_props.borrow_mut();
for node in element.dynamic_nodes {
if let DynamicNode::Component(comp) = node {
let unbounded = unsafe { std::mem::transmute(comp as *const VComponent) };
props.push(unbounded);
}
}
Ok(element)
} }
/// Create a dynamic text node using [`Arguments`] and the [`ScopeState`]'s internal [`Bump`] allocator /// Create a dynamic text node using [`Arguments`] and the [`ScopeState`]'s internal [`Bump`] allocator

View file

@ -279,12 +279,11 @@ impl VirtualDom {
/// ///
/// Whenever the VirtualDom "works", it will re-render this scope /// Whenever the VirtualDom "works", it will re-render this scope
pub fn mark_dirty(&mut self, id: ScopeId) { pub fn mark_dirty(&mut self, id: ScopeId) {
let height = self.scopes[id.0].height; if let Some(scope) = self.scopes.get(id.0) {
let height = scope.height;
println!("marking scope {} dirty with height {}", id.0, height);
self.dirty_scopes.insert(DirtyScope { height, id }); self.dirty_scopes.insert(DirtyScope { height, id });
} }
}
/// Determine whether or not a scope is currently in a suspended state /// Determine whether or not a scope is currently in a suspended state
/// ///
@ -547,6 +546,11 @@ impl VirtualDom {
if let Some(dirty) = self.dirty_scopes.iter().next().cloned() { if let Some(dirty) = self.dirty_scopes.iter().next().cloned() {
self.dirty_scopes.remove(&dirty); self.dirty_scopes.remove(&dirty);
// If the scope doesn't exist for whatever reason, then we should skip it
if !self.scopes.contains(dirty.id.0) {
continue;
}
// if the scope is currently suspended, then we should skip it, ignoring any tasks calling for an update // if the scope is currently suspended, then we should skip it, ignoring any tasks calling for an update
if self.is_scope_suspended(dirty.id) { if self.is_scope_suspended(dirty.id) {
continue; continue;

View file

@ -62,11 +62,11 @@ fn contexts_drop() {
_ = dom.render_immediate(); _ = dom.render_immediate();
} }
#[tokio::test] #[test]
async fn tasks_drop() { fn tasks_drop() {
fn app(cx: Scope) -> Element { fn app(cx: Scope) -> Element {
cx.spawn(async { cx.spawn(async {
tokio::time::sleep(std::time::Duration::from_millis(100000)).await; // tokio::time::sleep(std::time::Duration::from_millis(100000)).await;
}); });
cx.render(rsx! { cx.render(rsx! {

View file

@ -20,7 +20,7 @@ serde = "1.0.136"
serde_json = "1.0.79" serde_json = "1.0.79"
thiserror = "1.0.30" thiserror = "1.0.30"
log = "0.4.14" log = "0.4.14"
wry = { version = "0.22.0" } wry = { version = "0.23.4" }
futures-channel = "0.3.21" futures-channel = "0.3.21"
tokio = { version = "1.16.1", features = [ tokio = { version = "1.16.1", features = [
"sync", "sync",