Merge pull request #182 from DioxusLabs/jk/debugging-diff
fix: nodes being unmounted when used in highly nested contexts
This commit is contained in:
17 files changed
+939
-895
No files matched your search
+461
-665
File diff suppressed because it is too large.
Load diff
@@ -12,7 +12,6 @@ pub(crate) mod util;
|
||||
pub(crate) mod virtual_dom;
|
||||
|
||||
pub(crate) mod innerlude {
|
||||
pub(crate) use crate::diff::*;
|
||||
pub use crate::events::*;
|
||||
pub use crate::lazynodes::*;
|
||||
pub use crate::mutations::*;
|
||||
|
||||
@@ -143,6 +143,10 @@ impl<'a> Mutations<'a> {
|
||||
self.edits.push(InsertBefore { n, root });
|
||||
}
|
||||
|
||||
pub(crate) fn append_children(&mut self, n: u32) {
|
||||
self.edits.push(AppendChildren { many: n });
|
||||
}
|
||||
|
||||
// Remove Nodes from the dom
|
||||
pub(crate) fn remove(&mut self, id: u64) {
|
||||
self.edits.push(Remove { root: id });
|
||||
@@ -217,6 +221,10 @@ impl<'a> Mutations<'a> {
|
||||
let name = attribute.name;
|
||||
self.edits.push(RemoveAttribute { name, root });
|
||||
}
|
||||
|
||||
pub(crate) fn mark_dirty_scope(&mut self, scope: ScopeId) {
|
||||
self.dirty_scopes.insert(scope);
|
||||
}
|
||||
}
|
||||
|
||||
// refs are only assigned once
|
||||
|
||||
@@ -147,13 +147,6 @@ impl<'src> VNode<'src> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn children(&self) -> &[VNode<'src>] {
|
||||
match &self {
|
||||
VNode::Fragment(f) => f.children,
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
|
||||
// Create an "owned" version of the vnode.
|
||||
pub fn decouple(&self) -> VNode<'src> {
|
||||
match *self {
|
||||
@@ -175,6 +168,7 @@ impl Debug for VNode<'_> {
|
||||
.field("key", &el.key)
|
||||
.field("attrs", &el.attributes)
|
||||
.field("children", &el.children)
|
||||
.field("id", &el.id)
|
||||
.finish(),
|
||||
VNode::Text(t) => write!(s, "VNode::VText {{ text: {} }}", t.text),
|
||||
VNode::Placeholder(t) => write!(s, "VNode::VPlaceholder {{ id: {:?} }}", t.id),
|
||||
|
||||
+54
-63
@@ -13,6 +13,8 @@ use std::{
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
/// for traceability, we use the raw fn pointer to identify the function
|
||||
/// we also get the component name, but that's not necessarily unique in the app
|
||||
pub(crate) type ComponentPtr = *mut std::os::raw::c_void;
|
||||
|
||||
pub(crate) struct Heuristic {
|
||||
@@ -94,7 +96,7 @@ impl ScopeArena {
|
||||
|
||||
// Get the height of the scope
|
||||
let height = parent_scope
|
||||
.map(|id| self.get_scope(id).map(|scope| scope.height))
|
||||
.map(|id| self.get_scope(id).map(|scope| scope.height + 1))
|
||||
.flatten()
|
||||
.unwrap_or_default();
|
||||
|
||||
@@ -110,31 +112,62 @@ impl ScopeArena {
|
||||
if let Some(old_scope) = self.free_scopes.borrow_mut().pop() {
|
||||
// reuse the old scope
|
||||
let scope = unsafe { &mut *old_scope };
|
||||
scope.props.get_mut().replace(vcomp);
|
||||
|
||||
scope.container = container;
|
||||
scope.our_arena_idx = new_scope_id;
|
||||
scope.parent_scope = parent_scope;
|
||||
scope.height = height;
|
||||
scope.fnptr = fc_ptr;
|
||||
scope.props.get_mut().replace(vcomp);
|
||||
scope.subtree.set(subtree);
|
||||
scope.our_arena_idx = new_scope_id;
|
||||
scope.container = container;
|
||||
scope.frames[0].reset();
|
||||
scope.frames[1].reset();
|
||||
scope.shared_contexts.get_mut().clear();
|
||||
scope.items.get_mut().listeners.clear();
|
||||
scope.items.get_mut().borrowed_props.clear();
|
||||
scope.hook_idx.set(0);
|
||||
scope.hook_vals.get_mut().clear();
|
||||
|
||||
let any_item = self.scopes.borrow_mut().insert(new_scope_id, scope);
|
||||
debug_assert!(any_item.is_none());
|
||||
} else {
|
||||
// else create a new scope
|
||||
let (node_capacity, hook_capacity) = self
|
||||
.heuristics
|
||||
.borrow()
|
||||
.get(&fc_ptr)
|
||||
.map(|h| (h.node_arena_size, h.hook_arena_size))
|
||||
.unwrap_or_default();
|
||||
|
||||
self.scopes.borrow_mut().insert(
|
||||
new_scope_id,
|
||||
self.bump.alloc(ScopeState::new(
|
||||
height,
|
||||
self.bump.alloc(ScopeState {
|
||||
container,
|
||||
new_scope_id,
|
||||
our_arena_idx: new_scope_id,
|
||||
parent_scope,
|
||||
vcomp,
|
||||
self.tasks.clone(),
|
||||
self.heuristics
|
||||
.borrow()
|
||||
.get(&fc_ptr)
|
||||
.map(|h| (h.node_arena_size, h.hook_arena_size))
|
||||
.unwrap_or_default(),
|
||||
)),
|
||||
height,
|
||||
fnptr: fc_ptr,
|
||||
props: RefCell::new(Some(vcomp)),
|
||||
frames: [BumpFrame::new(node_capacity), BumpFrame::new(node_capacity)],
|
||||
|
||||
// todo: subtrees
|
||||
subtree: Cell::new(0),
|
||||
is_subtree_root: Cell::new(false),
|
||||
|
||||
generation: 0.into(),
|
||||
|
||||
tasks: self.tasks.clone(),
|
||||
shared_contexts: Default::default(),
|
||||
|
||||
items: RefCell::new(SelfReferentialItems {
|
||||
listeners: Default::default(),
|
||||
borrowed_props: Default::default(),
|
||||
}),
|
||||
|
||||
hook_arena: Bump::new(),
|
||||
hook_vals: RefCell::new(Vec::with_capacity(hook_capacity)),
|
||||
hook_idx: Default::default(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -189,8 +222,6 @@ 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) {
|
||||
log::trace!("Ensuring drop safety for scope {:?}", scope_id);
|
||||
|
||||
if let Some(scope) = self.get_scope(scope_id) {
|
||||
let mut items = scope.items.borrow_mut();
|
||||
|
||||
@@ -201,7 +232,6 @@ impl ScopeArena {
|
||||
if let Some(scope_id) = comp.scope.get() {
|
||||
self.ensure_drop_safety(scope_id);
|
||||
}
|
||||
|
||||
drop(comp.props.take());
|
||||
});
|
||||
|
||||
@@ -217,18 +247,18 @@ impl ScopeArena {
|
||||
// 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
|
||||
log::trace!("Running scope {:?}", id);
|
||||
self.ensure_drop_safety(id);
|
||||
|
||||
// todo: we *know* that this is aliased by the contents of the scope itself
|
||||
let scope = unsafe { &mut *self.get_scope_raw(id).expect("could not find scope") };
|
||||
|
||||
log::trace!("running scope {:?} symbol: {:?}", id, scope.fnptr);
|
||||
|
||||
// Safety:
|
||||
// - We dropped the listeners, so no more &mut T can be used while these are held
|
||||
// - All children nodes that rely on &mut T are replaced with a new reference
|
||||
scope.hook_idx.set(0);
|
||||
|
||||
// book keeping to ensure safety around the borrowed data
|
||||
{
|
||||
// Safety:
|
||||
// - We've dropped all references to the wip bump frame with "ensure_drop_safety"
|
||||
@@ -241,8 +271,6 @@ impl ScopeArena {
|
||||
debug_assert!(items.borrowed_props.is_empty());
|
||||
}
|
||||
|
||||
// safety: this is definitely not dropped
|
||||
|
||||
/*
|
||||
If the component returns None, then we fill in a placeholder node. This will wipe what was there.
|
||||
An alternate approach is to leave the Real Dom the same, but that can lead to safety issues and a lot more checks.
|
||||
@@ -284,14 +312,13 @@ impl ScopeArena {
|
||||
|
||||
while let Some(id) = cur_el.take() {
|
||||
if let Some(el) = nodes.get(id.0) {
|
||||
log::trace!("Found valid receiver element");
|
||||
|
||||
let real_el = unsafe { &**el };
|
||||
log::debug!("looking for listener on {:?}", real_el);
|
||||
|
||||
if let VNode::Element(real_el) = real_el {
|
||||
for listener in real_el.listeners.borrow().iter() {
|
||||
if listener.event == event.name {
|
||||
log::trace!("Found valid receiver event");
|
||||
|
||||
log::debug!("calling listener {:?}", listener.event);
|
||||
if state.canceled.get() {
|
||||
// stop bubbling if canceled
|
||||
break;
|
||||
@@ -421,6 +448,7 @@ pub struct ScopeState {
|
||||
pub(crate) container: ElementId,
|
||||
pub(crate) our_arena_idx: ScopeId,
|
||||
pub(crate) height: u32,
|
||||
pub(crate) fnptr: ComponentPtr,
|
||||
|
||||
// todo: subtrees
|
||||
pub(crate) is_subtree_root: Cell<bool>,
|
||||
@@ -449,43 +477,6 @@ pub struct SelfReferentialItems<'a> {
|
||||
|
||||
// Public methods exposed to libraries and components
|
||||
impl ScopeState {
|
||||
fn new(
|
||||
height: u32,
|
||||
container: ElementId,
|
||||
our_arena_idx: ScopeId,
|
||||
parent_scope: Option<*mut ScopeState>,
|
||||
vcomp: Box<dyn AnyProps>,
|
||||
tasks: Rc<TaskQueue>,
|
||||
(node_capacity, hook_capacity): (usize, usize),
|
||||
) -> Self {
|
||||
ScopeState {
|
||||
container,
|
||||
our_arena_idx,
|
||||
parent_scope,
|
||||
height,
|
||||
props: RefCell::new(Some(vcomp)),
|
||||
frames: [BumpFrame::new(node_capacity), BumpFrame::new(node_capacity)],
|
||||
|
||||
// todo: subtrees
|
||||
subtree: Cell::new(0),
|
||||
is_subtree_root: Cell::new(false),
|
||||
|
||||
generation: 0.into(),
|
||||
|
||||
tasks,
|
||||
shared_contexts: Default::default(),
|
||||
|
||||
items: RefCell::new(SelfReferentialItems {
|
||||
listeners: Default::default(),
|
||||
borrowed_props: Default::default(),
|
||||
}),
|
||||
|
||||
hook_arena: Bump::new(),
|
||||
hook_vals: RefCell::new(Vec::with_capacity(hook_capacity)),
|
||||
hook_idx: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the subtree ID that this scope belongs to.
|
||||
///
|
||||
/// Each component has its own subtree ID - the root subtree has an ID of 0. This ID is used by the renderer to route
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//!
|
||||
//! This module provides the primary mechanics to create a hook-based, concurrent VDOM for Rust.
|
||||
|
||||
use crate::diff::DiffState;
|
||||
use crate::innerlude::*;
|
||||
use futures_channel::mpsc::{UnboundedReceiver, UnboundedSender};
|
||||
use futures_util::{future::poll_fn, StreamExt};
|
||||
@@ -448,6 +449,7 @@ impl VirtualDom {
|
||||
/// apply_mutations(mutations);
|
||||
/// }
|
||||
/// ```
|
||||
#[allow(unused)]
|
||||
pub fn work_with_deadline(&mut self, mut deadline: impl FnMut() -> bool) -> Vec<Mutations> {
|
||||
let mut committed_mutations = vec![];
|
||||
|
||||
@@ -467,36 +469,41 @@ impl VirtualDom {
|
||||
h1.cmp(&h2).reverse()
|
||||
});
|
||||
|
||||
log::debug!("dirty_scopes: {:?}", self.dirty_scopes);
|
||||
|
||||
if let Some(scopeid) = self.dirty_scopes.pop() {
|
||||
if !ran_scopes.contains(&scopeid) {
|
||||
ran_scopes.insert(scopeid);
|
||||
|
||||
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);
|
||||
diff_state.diff_scope(scopeid);
|
||||
|
||||
let scope = scopes.get_scope(scopeid).unwrap();
|
||||
diff_state.stack.element_stack.push(scope.container);
|
||||
let DiffState { mutations, .. } = diff_state;
|
||||
|
||||
log::debug!("succesffuly resolved scopes {:?}", mutations.dirty_scopes);
|
||||
for scope in &mutations.dirty_scopes {
|
||||
self.dirty_scopes.remove(scope);
|
||||
}
|
||||
|
||||
committed_mutations.push(mutations);
|
||||
|
||||
// todo: pause the diff machine
|
||||
// if diff_state.work(&mut deadline) {
|
||||
// let DiffState { mutations, .. } = diff_state;
|
||||
// for scope in &mutations.dirty_scopes {
|
||||
// self.dirty_scopes.remove(scope);
|
||||
// }
|
||||
// committed_mutations.push(mutations);
|
||||
// } else {
|
||||
// // leave the work in an incomplete state
|
||||
// //
|
||||
// // todo: we should store the edits and re-apply them later
|
||||
// // for now, we just dump the work completely (threadsafe)
|
||||
// return committed_mutations;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
if diff_state.work(&mut deadline) {
|
||||
let DiffState { mutations, .. } = diff_state;
|
||||
|
||||
for scope in &mutations.dirty_scopes {
|
||||
self.dirty_scopes.remove(scope);
|
||||
}
|
||||
|
||||
committed_mutations.push(mutations);
|
||||
} else {
|
||||
// leave the work in an incomplete state
|
||||
//
|
||||
// todo: we should store the edits and re-apply them later
|
||||
// for now, we just dump the work completely (threadsafe)
|
||||
return committed_mutations;
|
||||
}
|
||||
}
|
||||
|
||||
committed_mutations
|
||||
@@ -524,13 +531,15 @@ impl VirtualDom {
|
||||
let mut diff_state = DiffState::new(&self.scopes);
|
||||
|
||||
self.scopes.run_scope(scope_id);
|
||||
diff_state
|
||||
.stack
|
||||
.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);
|
||||
diff_state.work(|| false);
|
||||
diff_state.element_stack.push(ElementId(0));
|
||||
diff_state.scope_stack.push(scope_id);
|
||||
|
||||
let node = self.scopes.fin_head(scope_id);
|
||||
let created = diff_state.create_node(node);
|
||||
|
||||
diff_state.mutations.append_children(created as u32);
|
||||
|
||||
self.dirty_scopes.clear();
|
||||
assert!(self.dirty_scopes.is_empty());
|
||||
|
||||
@@ -577,12 +586,11 @@ impl VirtualDom {
|
||||
);
|
||||
|
||||
diff_machine.force_diff = true;
|
||||
diff_machine.stack.push(DiffInstruction::Diff { old, new });
|
||||
diff_machine.stack.scope_stack.push(scope_id);
|
||||
|
||||
diff_machine.scope_stack.push(scope_id);
|
||||
let scope = diff_machine.scopes.get_scope(scope_id).unwrap();
|
||||
diff_machine.stack.element_stack.push(scope.container);
|
||||
diff_machine.work(|| false);
|
||||
diff_machine.element_stack.push(scope.container);
|
||||
|
||||
diff_machine.diff_node(old, new);
|
||||
|
||||
diff_machine.mutations
|
||||
}
|
||||
@@ -621,10 +629,10 @@ impl VirtualDom {
|
||||
/// ```
|
||||
pub fn diff_vnodes<'a>(&'a self, old: &'a VNode<'a>, new: &'a VNode<'a>) -> Mutations<'a> {
|
||||
let mut machine = DiffState::new(&self.scopes);
|
||||
machine.stack.push(DiffInstruction::Diff { new, old });
|
||||
machine.stack.element_stack.push(ElementId(0));
|
||||
machine.stack.scope_stack.push(ScopeId(0));
|
||||
machine.work(|| false);
|
||||
machine.element_stack.push(ElementId(0));
|
||||
machine.scope_stack.push(ScopeId(0));
|
||||
machine.diff_node(old, new);
|
||||
|
||||
machine.mutations
|
||||
}
|
||||
|
||||
@@ -643,11 +651,11 @@ impl VirtualDom {
|
||||
/// ```
|
||||
pub fn create_vnodes<'a>(&'a self, nodes: LazyNodes<'a, '_>) -> Mutations<'a> {
|
||||
let mut machine = DiffState::new(&self.scopes);
|
||||
machine.stack.element_stack.push(ElementId(0));
|
||||
machine
|
||||
.stack
|
||||
.create_node(self.render_vnodes(nodes), MountType::Append);
|
||||
machine.work(|| false);
|
||||
machine.scope_stack.push(ScopeId(0));
|
||||
machine.element_stack.push(ElementId(0));
|
||||
let node = self.render_vnodes(nodes);
|
||||
let created = machine.create_node(node);
|
||||
machine.mutations.append_children(created as u32);
|
||||
machine.mutations
|
||||
}
|
||||
|
||||
@@ -672,16 +680,15 @@ impl VirtualDom {
|
||||
let (old, new) = (self.render_vnodes(left), self.render_vnodes(right));
|
||||
|
||||
let mut create = DiffState::new(&self.scopes);
|
||||
create.stack.scope_stack.push(ScopeId(0));
|
||||
create.stack.element_stack.push(ElementId(0));
|
||||
create.stack.create_node(old, MountType::Append);
|
||||
create.work(|| false);
|
||||
create.scope_stack.push(ScopeId(0));
|
||||
create.element_stack.push(ElementId(0));
|
||||
let created = create.create_node(old);
|
||||
create.mutations.append_children(created as u32);
|
||||
|
||||
let mut edit = DiffState::new(&self.scopes);
|
||||
edit.stack.scope_stack.push(ScopeId(0));
|
||||
edit.stack.element_stack.push(ElementId(0));
|
||||
edit.stack.push(DiffInstruction::Diff { old, new });
|
||||
edit.work(|| false);
|
||||
edit.scope_stack.push(ScopeId(0));
|
||||
edit.element_stack.push(ElementId(0));
|
||||
edit.diff_node(old, new);
|
||||
|
||||
(create.mutations, edit.mutations)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#![allow(unused, non_upper_case_globals)]
|
||||
|
||||
//! Diffing Tests
|
||||
//!
|
||||
//! These tests only verify that the diffing algorithm works properly for single components.
|
||||
@@ -105,6 +106,7 @@ fn empty_fragments_create_many_anchors() {
|
||||
create.edits,
|
||||
[CreatePlaceholder { root: 1 }, AppendChildren { many: 1 }]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
change.edits,
|
||||
[
|
||||
@@ -237,7 +239,7 @@ fn two_fragments_with_differrent_elements_are_differet() {
|
||||
// replace the divs with new h1s
|
||||
CreateElement { tag: "h1", root: 7 },
|
||||
ReplaceWith { root: 1, m: 1 },
|
||||
CreateElement { tag: "h1", root: 8 },
|
||||
CreateElement { tag: "h1", root: 1 }, // notice how 1 gets re-used
|
||||
ReplaceWith { root: 2, m: 1 },
|
||||
]
|
||||
);
|
||||
@@ -270,15 +272,18 @@ fn two_fragments_with_differrent_elements_are_differet_shorter() {
|
||||
AppendChildren { many: 6 },
|
||||
]
|
||||
);
|
||||
|
||||
// note: key reuse is always the last node that got used
|
||||
// slab maintains a linked list, essentially
|
||||
assert_eq!(
|
||||
change.edits,
|
||||
[
|
||||
Remove { root: 3 },
|
||||
Remove { root: 4 },
|
||||
Remove { root: 5 },
|
||||
CreateElement { root: 7, tag: "h1" },
|
||||
ReplaceWith { root: 1, m: 1 },
|
||||
CreateElement { root: 8, tag: "h1" },
|
||||
CreateElement { root: 5, tag: "h1" }, // 3 gets reused
|
||||
ReplaceWith { root: 1, m: 1 }, // 1 gets deleted
|
||||
CreateElement { root: 1, tag: "h1" }, // 1 gets reused
|
||||
ReplaceWith { root: 2, m: 1 },
|
||||
]
|
||||
);
|
||||
@@ -564,13 +569,13 @@ fn keyed_diffing_additions_and_moves_in_middle() {
|
||||
let dom = new_dom();
|
||||
|
||||
let left = rsx!({
|
||||
[/**/ 4, 5, 6, 7 /**/].iter().map(|f| {
|
||||
[/**/ 1, 2, 3, 4 /**/].iter().map(|f| {
|
||||
rsx! { div { key: "{f}" }}
|
||||
})
|
||||
});
|
||||
|
||||
let right = rsx!({
|
||||
[/**/ 7, 4, 13, 17, 5, 11, 12, 6 /**/].iter().map(|f| {
|
||||
[/**/ 4, 1, 7, 8, 2, 5, 6, 3 /**/].iter().map(|f| {
|
||||
rsx! { div { key: "{f}" }}
|
||||
})
|
||||
});
|
||||
@@ -581,14 +586,14 @@ fn keyed_diffing_additions_and_moves_in_middle() {
|
||||
assert_eq!(
|
||||
change.edits,
|
||||
[
|
||||
// create 13, 17
|
||||
// create 5, 6
|
||||
CreateElement { tag: "div", root: 5 },
|
||||
CreateElement { tag: "div", root: 6 },
|
||||
InsertBefore { root: 2, n: 2 },
|
||||
// create 11, 12
|
||||
InsertBefore { root: 3, n: 2 },
|
||||
// create 7, 8
|
||||
CreateElement { tag: "div", root: 7 },
|
||||
CreateElement { tag: "div", root: 8 },
|
||||
InsertBefore { root: 3, n: 2 },
|
||||
InsertBefore { root: 2, n: 2 },
|
||||
// move 7
|
||||
PushRoot { root: 4 },
|
||||
InsertBefore { root: 1, n: 1 }
|
||||
|
||||
@@ -59,8 +59,8 @@ fn test_early_abort() {
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
[
|
||||
CreateElement { tag: "div", root: 2 },
|
||||
CreateTextNode { text: "Hello, world!", root: 4 },
|
||||
CreateElement { tag: "div", root: 1 }, // keys get reused
|
||||
CreateTextNode { text: "Hello, world!", root: 2 }, // keys get reused
|
||||
AppendChildren { many: 1 },
|
||||
ReplaceWith { root: 3, m: 1 },
|
||||
]
|
||||
|
||||
@@ -40,7 +40,7 @@ fn manual_diffing() {
|
||||
|
||||
#[test]
|
||||
fn events_generate() {
|
||||
static App: Component = |cx| {
|
||||
fn app(cx: Scope) -> Element {
|
||||
let count = cx.use_hook(|_| 0);
|
||||
|
||||
let inner = match *count {
|
||||
@@ -61,7 +61,7 @@ fn events_generate() {
|
||||
cx.render(inner)
|
||||
};
|
||||
|
||||
let mut dom = VirtualDom::new(App);
|
||||
let mut dom = VirtualDom::new(app);
|
||||
let mut channel = dom.get_scheduler_channel();
|
||||
assert!(dom.has_work());
|
||||
|
||||
@@ -83,7 +83,7 @@ fn events_generate() {
|
||||
|
||||
#[test]
|
||||
fn components_generate() {
|
||||
static App: Component = |cx| {
|
||||
fn app(cx: Scope) -> Element {
|
||||
let render_phase = cx.use_hook(|_| 0);
|
||||
*render_phase += 1;
|
||||
|
||||
@@ -100,13 +100,14 @@ fn components_generate() {
|
||||
})
|
||||
};
|
||||
|
||||
static Child: Component = |cx| {
|
||||
fn Child(cx: Scope) -> Element {
|
||||
log::debug!("Running child");
|
||||
cx.render(rsx! {
|
||||
h1 {}
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
let mut dom = VirtualDom::new(App);
|
||||
let mut dom = VirtualDom::new(app);
|
||||
let edits = dom.rebuild();
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
@@ -116,72 +117,67 @@ fn components_generate() {
|
||||
]
|
||||
);
|
||||
|
||||
let edits = dom.hard_diff(ScopeId(0));
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateElement { tag: "div", root: 2 },
|
||||
ReplaceWith { root: 1, m: 1 },
|
||||
]
|
||||
);
|
||||
|
||||
let edits = dom.hard_diff(ScopeId(0));
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateTextNode { text: "Text2", root: 3 },
|
||||
CreateTextNode { text: "Text2", root: 1 },
|
||||
ReplaceWith { root: 2, m: 1 },
|
||||
]
|
||||
);
|
||||
|
||||
let edits = dom.hard_diff(ScopeId(0));
|
||||
// child {}
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateElement { tag: "h1", root: 4 },
|
||||
ReplaceWith { root: 3, m: 1 },
|
||||
CreateElement { tag: "h1", root: 2 },
|
||||
ReplaceWith { root: 1, m: 1 },
|
||||
]
|
||||
);
|
||||
|
||||
let edits = dom.hard_diff(ScopeId(0));
|
||||
// placeholder
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
[CreatePlaceholder { root: 5 }, ReplaceWith { root: 4, m: 1 },]
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[CreatePlaceholder { root: 1 }, ReplaceWith { root: 2, m: 1 },]
|
||||
);
|
||||
|
||||
let edits = dom.hard_diff(ScopeId(0));
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateTextNode { text: "text 3", root: 6 },
|
||||
ReplaceWith { root: 5, m: 1 },
|
||||
CreateTextNode { text: "text 3", root: 2 },
|
||||
ReplaceWith { root: 1, m: 1 },
|
||||
]
|
||||
);
|
||||
|
||||
let edits = dom.hard_diff(ScopeId(0));
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateTextNode { text: "text 0", root: 7 },
|
||||
CreateTextNode { text: "text 1", root: 8 },
|
||||
ReplaceWith { root: 6, m: 2 },
|
||||
CreateTextNode { text: "text 0", root: 1 },
|
||||
CreateTextNode { text: "text 1", root: 3 },
|
||||
ReplaceWith { root: 2, m: 2 },
|
||||
]
|
||||
);
|
||||
|
||||
let edits = dom.hard_diff(ScopeId(0));
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
dom.hard_diff(ScopeId(0)).edits,
|
||||
[
|
||||
CreateElement { tag: "h1", root: 9 },
|
||||
ReplaceWith { root: 7, m: 1 },
|
||||
Remove { root: 8 },
|
||||
CreateElement { tag: "h1", root: 2 },
|
||||
ReplaceWith { root: 1, m: 1 },
|
||||
Remove { root: 3 },
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn component_swap() {
|
||||
static App: Component = |cx| {
|
||||
fn app(cx: Scope) -> Element {
|
||||
let render_phase = cx.use_hook(|_| 0);
|
||||
*render_phase += 1;
|
||||
|
||||
@@ -257,7 +253,7 @@ fn component_swap() {
|
||||
})
|
||||
};
|
||||
|
||||
let mut dom = VirtualDom::new(App);
|
||||
let mut dom = VirtualDom::new(app);
|
||||
let edits = dom.rebuild();
|
||||
dbg!(&edits);
|
||||
|
||||
|
||||
@@ -314,36 +314,117 @@ fn test_pass_thru() {
|
||||
#[inline_props]
|
||||
fn Router<'a>(cx: Scope, children: Element<'a>) -> Element {
|
||||
cx.render(rsx! {
|
||||
&cx.props.children
|
||||
div {
|
||||
&cx.props.children
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn MemoizedThing(cx: Scope) -> Element {
|
||||
rsx!(cx, div { "memoized" })
|
||||
#[inline_props]
|
||||
fn NavContainer<'a>(cx: Scope, children: Element<'a>) -> Element {
|
||||
cx.render(rsx! {
|
||||
header {
|
||||
nav {
|
||||
&cx.props.children
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn NavMenu(cx: Scope) -> Element {
|
||||
rsx!(cx,
|
||||
NavBrand {}
|
||||
div {
|
||||
NavStart {}
|
||||
NavEnd {}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
fn NavBrand(cx: Scope) -> Element {
|
||||
rsx!(cx, div {})
|
||||
}
|
||||
|
||||
fn NavStart(cx: Scope) -> Element {
|
||||
rsx!(cx, div {})
|
||||
}
|
||||
|
||||
fn NavEnd(cx: Scope) -> Element {
|
||||
rsx!(cx, div {})
|
||||
}
|
||||
|
||||
#[inline_props]
|
||||
fn MainContainer<'a>(
|
||||
cx: Scope,
|
||||
nav: Element<'a>,
|
||||
body: Element<'a>,
|
||||
footer: Element<'a>,
|
||||
) -> Element {
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
class: "columns is-mobile",
|
||||
div {
|
||||
class: "column is-full",
|
||||
&cx.props.nav,
|
||||
&cx.props.body,
|
||||
&cx.props.footer,
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn app(cx: Scope) -> Element {
|
||||
let thing = cx.use_hook(|_| "asd");
|
||||
rsx!(cx,
|
||||
Router {
|
||||
MemoizedThing {
|
||||
}
|
||||
let nav = cx.render(rsx! {
|
||||
NavContainer {
|
||||
NavMenu {}
|
||||
}
|
||||
)
|
||||
});
|
||||
let body = cx.render(rsx! {
|
||||
div {}
|
||||
});
|
||||
let footer = cx.render(rsx! {
|
||||
div {}
|
||||
});
|
||||
|
||||
cx.render(rsx! {
|
||||
MainContainer {
|
||||
nav: nav,
|
||||
body: body,
|
||||
footer: footer,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let mut dom = new_dom(app, ());
|
||||
let _ = dom.rebuild();
|
||||
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
for x in 0..40 {
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
|
||||
dom.work_with_deadline(|| false);
|
||||
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
|
||||
dom.work_with_deadline(|| false);
|
||||
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
|
||||
dom.work_with_deadline(|| false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
#![allow(unused, non_upper_case_globals)]
|
||||
|
||||
//! Diffing Tests
|
||||
//!
|
||||
//! These tests only verify that the diffing algorithm works properly for single components.
|
||||
//!
|
||||
//! It does not validated that component lifecycles work properly. This is done in another test file.
|
||||
|
||||
use dioxus::{prelude::*, DomEdit, ScopeId};
|
||||
use dioxus_core as dioxus;
|
||||
use dioxus_core_macro::*;
|
||||
use dioxus_html as dioxus_elements;
|
||||
|
||||
mod test_logging;
|
||||
|
||||
fn new_dom() -> VirtualDom {
|
||||
const IS_LOGGING_ENABLED: bool = false;
|
||||
test_logging::set_up_logging(IS_LOGGING_ENABLED);
|
||||
VirtualDom::new(|cx| rsx!(cx, "hi"))
|
||||
}
|
||||
|
||||
use DomEdit::*;
|
||||
|
||||
/// Should push the text node onto the stack and modify it
|
||||
#[test]
|
||||
fn nested_passthru_creates() {
|
||||
fn app(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
Child {
|
||||
Child {
|
||||
Child {
|
||||
div {
|
||||
"hi"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
#[inline_props]
|
||||
fn Child<'a>(cx: Scope, children: Element<'a>) -> Element {
|
||||
cx.render(rsx! {
|
||||
children
|
||||
})
|
||||
};
|
||||
|
||||
let mut dom = VirtualDom::new(app);
|
||||
let mut channel = dom.get_scheduler_channel();
|
||||
assert!(dom.has_work());
|
||||
|
||||
let edits = dom.rebuild();
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
[
|
||||
CreateElement { tag: "div", root: 1 },
|
||||
CreateTextNode { text: "hi", root: 2 },
|
||||
AppendChildren { many: 1 },
|
||||
AppendChildren { many: 1 },
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
/// Should push the text node onto the stack and modify it
|
||||
#[test]
|
||||
fn nested_passthru_creates_add() {
|
||||
fn app(cx: Scope) -> Element {
|
||||
cx.render(rsx! {
|
||||
Child {
|
||||
"1"
|
||||
Child {
|
||||
"2"
|
||||
Child {
|
||||
"3"
|
||||
div {
|
||||
"hi"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
#[inline_props]
|
||||
fn Child<'a>(cx: Scope, children: Element<'a>) -> Element {
|
||||
cx.render(rsx! {
|
||||
children
|
||||
})
|
||||
};
|
||||
|
||||
let mut dom = VirtualDom::new(app);
|
||||
let mut channel = dom.get_scheduler_channel();
|
||||
assert!(dom.has_work());
|
||||
|
||||
let edits = dom.rebuild();
|
||||
assert_eq!(
|
||||
edits.edits,
|
||||
[
|
||||
CreateTextNode { text: "1", root: 1 },
|
||||
CreateTextNode { text: "2", root: 2 },
|
||||
CreateTextNode { text: "3", root: 3 },
|
||||
CreateElement { tag: "div", root: 4 },
|
||||
CreateTextNode { text: "hi", root: 5 },
|
||||
AppendChildren { many: 1 },
|
||||
AppendChildren { many: 4 },
|
||||
]
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#![allow(unused, non_upper_case_globals)]
|
||||
|
||||
use dioxus::{prelude::*, DomEdit, Mutations};
|
||||
use dioxus::{prelude::*, DomEdit, Mutations, SchedulerMsg, ScopeId};
|
||||
use dioxus_core as dioxus;
|
||||
use dioxus_core_macro::*;
|
||||
use dioxus_html as dioxus_elements;
|
||||
@@ -34,3 +34,97 @@ fn shared_state_test() {
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn swap_test() {
|
||||
struct MySharedState(&'static str);
|
||||
|
||||
fn app(cx: Scope) -> Element {
|
||||
let val = cx.use_hook(|_| 0);
|
||||
*val += 1;
|
||||
|
||||
cx.provide_context(MySharedState("world!"));
|
||||
|
||||
let child = match *val % 2 {
|
||||
0 => rsx!(
|
||||
Child1 {
|
||||
Child1 { }
|
||||
Child2 { }
|
||||
}
|
||||
),
|
||||
_ => rsx!(
|
||||
Child2 {
|
||||
Child2 { }
|
||||
Child2 { }
|
||||
}
|
||||
),
|
||||
};
|
||||
|
||||
cx.render(rsx!(
|
||||
Router {
|
||||
div { child }
|
||||
}
|
||||
))
|
||||
}
|
||||
|
||||
#[inline_props]
|
||||
fn Router<'a>(cx: Scope, children: Element<'a>) -> Element<'a> {
|
||||
cx.render(rsx!(div { children }))
|
||||
}
|
||||
|
||||
#[inline_props]
|
||||
fn Child1<'a>(cx: Scope, children: Element<'a>) -> Element {
|
||||
let shared = cx.consume_context::<MySharedState>().unwrap();
|
||||
println!("Child1: {}", shared.0);
|
||||
cx.render(rsx! {
|
||||
div {
|
||||
"{shared.0}",
|
||||
children
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[inline_props]
|
||||
fn Child2<'a>(cx: Scope, children: Element<'a>) -> Element {
|
||||
let shared = cx.consume_context::<MySharedState>().unwrap();
|
||||
println!("Child2: {}", shared.0);
|
||||
cx.render(rsx! {
|
||||
h1 {
|
||||
"{shared.0}",
|
||||
children
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let mut dom = VirtualDom::new(app);
|
||||
let Mutations { edits, .. } = dom.rebuild();
|
||||
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
dom.work_with_deadline(|| false);
|
||||
|
||||
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(1)));
|
||||
// dom.work_with_deadline(|| false);
|
||||
|
||||
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(2)));
|
||||
// dom.work_with_deadline(|| false);
|
||||
|
||||
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(3)));
|
||||
// dom.work_with_deadline(|| false);
|
||||
|
||||
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
// dom.work_with_deadline(|| false);
|
||||
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
// dom.work_with_deadline(|| false);
|
||||
// dom.handle_message(SchedulerMsg::Immediate(ScopeId(0)));
|
||||
// dom.work_with_deadline(|| false);
|
||||
}
|
||||
@@ -41,7 +41,7 @@ pub struct LinkProps<'a> {
|
||||
}
|
||||
|
||||
pub fn Link<'a>(cx: Scope<'a, LinkProps<'a>>) -> Element {
|
||||
log::trace!("render Link to {}", cx.props.to);
|
||||
// log::trace!("render Link to {}", cx.props.to);
|
||||
if let Some(service) = cx.consume_context::<RouterService>() {
|
||||
return cx.render(rsx! {
|
||||
a {
|
||||
|
||||
@@ -30,7 +30,7 @@ pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
|
||||
Some(ctx) => ctx.total_route.to_string(),
|
||||
None => cx.props.to.to_string(),
|
||||
};
|
||||
log::trace!("total route for {} is {}", cx.props.to, total_route);
|
||||
// log::trace!("total route for {} is {}", cx.props.to, total_route);
|
||||
|
||||
// provide our route context
|
||||
let route_context = cx.provide_context(RouteContext {
|
||||
@@ -48,7 +48,7 @@ pub fn Route<'a>(cx: Scope<'a, RouteProps<'a>>) -> Element {
|
||||
Some(RouteInner {})
|
||||
});
|
||||
|
||||
log::trace!("Checking route {}", cx.props.to);
|
||||
// log::trace!("Checking route {}", cx.props.to);
|
||||
|
||||
if router_root.should_render(cx.scope_id()) {
|
||||
cx.render(rsx!(&cx.props.children))
|
||||
|
||||
@@ -17,6 +17,7 @@ pub struct RouterProps<'a> {
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Router<'a>(cx: Scope<'a, RouterProps<'a>>) -> Element {
|
||||
log::debug!("running router {:?}", cx.scope_id());
|
||||
let svc = cx.use_hook(|_| {
|
||||
let update = cx.schedule_update_any();
|
||||
cx.provide_context(RouterService::new(update, cx.scope_id()))
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
pub trait RouterProvider {
|
||||
fn get_current_route(&self) -> String;
|
||||
fn subscribe_to_route_changes(&self, callback: Box<dyn Fn(String)>);
|
||||
}
|
||||
|
||||
enum RouteChange {
|
||||
LinkTo(String),
|
||||
Back(String),
|
||||
fn listen(&self, callback: Box<dyn Fn()>);
|
||||
}
|
||||
@@ -7,14 +7,18 @@ use std::{
|
||||
|
||||
use dioxus_core::ScopeId;
|
||||
|
||||
use crate::platform::RouterProvider;
|
||||
|
||||
pub struct RouterService {
|
||||
pub(crate) regen_route: Rc<dyn Fn(ScopeId)>,
|
||||
pub(crate) pending_events: Rc<RefCell<Vec<RouteEvent>>>,
|
||||
history: Rc<RefCell<BrowserHistory>>,
|
||||
slots: Rc<RefCell<Vec<(ScopeId, String)>>>,
|
||||
onchange_listeners: Rc<RefCell<HashSet<ScopeId>>>,
|
||||
root_found: Rc<Cell<Option<ScopeId>>>,
|
||||
cur_path_params: Rc<RefCell<HashMap<String, String>>>,
|
||||
|
||||
// history: Rc<dyn RouterProvider>,
|
||||
history: Rc<RefCell<BrowserHistory>>,
|
||||
listener: HistoryListener,
|
||||
}
|
||||
|
||||
@@ -58,12 +62,10 @@ impl RouterService {
|
||||
root_found.set(None);
|
||||
// checking if the route is valid is cheap, so we do it
|
||||
for (slot, root) in slots.borrow_mut().iter().rev() {
|
||||
log::trace!("regenerating slot {:?} for root '{}'", slot, root);
|
||||
regen_route(*slot);
|
||||
}
|
||||
|
||||
for listener in onchange_listeners.borrow_mut().iter() {
|
||||
log::trace!("regenerating listener {:?}", listener);
|
||||
regen_route(*listener);
|
||||
}
|
||||
|
||||
@@ -87,31 +89,24 @@ impl RouterService {
|
||||
}
|
||||
|
||||
pub fn push_route(&self, route: &str) {
|
||||
log::trace!("Pushing route: {}", route);
|
||||
self.history.borrow_mut().push(route);
|
||||
}
|
||||
|
||||
pub fn register_total_route(&self, route: String, scope: ScopeId, fallback: bool) {
|
||||
let clean = clean_route(route);
|
||||
log::trace!("Registered route '{}' with scope id {:?}", clean, scope);
|
||||
self.slots.borrow_mut().push((scope, clean));
|
||||
}
|
||||
|
||||
pub fn should_render(&self, scope: ScopeId) -> bool {
|
||||
log::trace!("Should render scope id {:?}?", scope);
|
||||
if let Some(root_id) = self.root_found.get() {
|
||||
log::trace!(" we already found a root with scope id {:?}", root_id);
|
||||
if root_id == scope {
|
||||
log::trace!(" yes - it's a match");
|
||||
return true;
|
||||
}
|
||||
log::trace!(" no - it's not a match");
|
||||
return false;
|
||||
}
|
||||
|
||||
let location = self.history.borrow().location();
|
||||
let path = location.path();
|
||||
log::trace!(" current path is '{}'", path);
|
||||
|
||||
let roots = self.slots.borrow();
|
||||
|
||||
@@ -120,23 +115,15 @@ impl RouterService {
|
||||
// fallback logic
|
||||
match root {
|
||||
Some((id, route)) => {
|
||||
log::trace!(
|
||||
" matched given scope id {:?} with route root '{}'",
|
||||
scope,
|
||||
route,
|
||||
);
|
||||
if let Some(params) = route_matches_path(route, path) {
|
||||
log::trace!(" and it matches the current path '{}'", path);
|
||||
self.root_found.set(Some(*id));
|
||||
*self.cur_path_params.borrow_mut() = params;
|
||||
true
|
||||
} else {
|
||||
if route == "" {
|
||||
log::trace!(" and the route is the root, so we will use that without a better match");
|
||||
self.root_found.set(Some(*id));
|
||||
true
|
||||
} else {
|
||||
log::trace!(" and the route '{}' is not the root nor does it match the current path", route);
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -154,12 +141,10 @@ impl RouterService {
|
||||
}
|
||||
|
||||
pub fn subscribe_onchange(&self, id: ScopeId) {
|
||||
log::trace!("Subscribing onchange for scope id {:?}", id);
|
||||
self.onchange_listeners.borrow_mut().insert(id);
|
||||
}
|
||||
|
||||
pub fn unsubscribe_onchange(&self, id: ScopeId) {
|
||||
log::trace!("Subscribing onchange for scope id {:?}", id);
|
||||
self.onchange_listeners.borrow_mut().remove(&id);
|
||||
}
|
||||
}
|
||||
@@ -182,36 +167,20 @@ fn route_matches_path(route: &str, path: &str) -> Option<HashMap<String, String>
|
||||
let route_pieces = route.split('/').collect::<Vec<_>>();
|
||||
let path_pieces = clean_path(path).split('/').collect::<Vec<_>>();
|
||||
|
||||
log::trace!(
|
||||
" checking route pieces {:?} vs path pieces {:?}",
|
||||
route_pieces,
|
||||
path_pieces,
|
||||
);
|
||||
|
||||
if route_pieces.len() != path_pieces.len() {
|
||||
log::trace!(" the routes are different lengths");
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut matches = HashMap::new();
|
||||
for (i, r) in route_pieces.iter().enumerate() {
|
||||
log::trace!(" checking route piece '{}' vs path", r);
|
||||
// If this is a parameter then it matches as long as there's
|
||||
// _any_thing in that spot in the path.
|
||||
if r.starts_with(':') {
|
||||
log::trace!(
|
||||
" route piece '{}' starts with a colon so it matches anything",
|
||||
r,
|
||||
);
|
||||
let param = &r[1..];
|
||||
matches.insert(param.to_string(), path_pieces[i].to_string());
|
||||
continue;
|
||||
}
|
||||
log::trace!(
|
||||
" route piece '{}' must be an exact match for path piece '{}'",
|
||||
r,
|
||||
path_pieces[i],
|
||||
);
|
||||
|
||||
if path_pieces[i] != *r {
|
||||
return None;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user