wip: add diffing tests
This commit is contained in:
1 parent
4bfd936e12
commit
662f58c8bc
5 files changed
+76
-3
No files matched your search
@@ -195,10 +195,11 @@ impl<'b: 'static> 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>) {
|
||||||
if let Some(components) = matching_components(left, right) {
|
match matching_components(left, right) {
|
||||||
components
|
None => self.replace_template(left, right),
|
||||||
|
Some(components) => components
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.for_each(|(l, r)| self.diff_vcomponent(l, r))
|
.for_each(|(l, r)| self.diff_vcomponent(l, r)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -654,6 +655,10 @@ impl<'b: 'static> VirtualDom {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn replace_template(&mut self, left: &'b VNode<'b>, right: &'b VNode<'b>) {
|
||||||
|
todo!("replacing should work!")
|
||||||
|
}
|
||||||
|
|
||||||
fn replace(
|
fn replace(
|
||||||
&mut self,
|
&mut self,
|
||||||
left_template: &'b VNode<'b>,
|
left_template: &'b VNode<'b>,
|
||||||
|
|||||||
@@ -103,6 +103,10 @@ impl ScopeState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn generation(&self) -> usize {
|
||||||
|
self.render_cnt.get()
|
||||||
|
}
|
||||||
|
|
||||||
/// Get a handle to the currently active bump arena for this Scope
|
/// Get a handle to the currently active bump arena for this Scope
|
||||||
///
|
///
|
||||||
/// This is a bump memory allocator. Be careful using this directly since the contents will be wiped on the next render.
|
/// This is a bump memory allocator. Be careful using this directly since the contents will be wiped on the next render.
|
||||||
|
|||||||
@@ -553,6 +553,8 @@ impl VirtualDom {
|
|||||||
// Save the current mutations length so we can split them into boundary
|
// Save the current mutations length so we can split them into boundary
|
||||||
let mutations_to_this_point = self.mutations.len();
|
let mutations_to_this_point = self.mutations.len();
|
||||||
|
|
||||||
|
// Run the scope and get the mutations
|
||||||
|
self.run_scope(dirty.id);
|
||||||
self.diff_scope(dirty.id);
|
self.diff_scope(dirty.id);
|
||||||
|
|
||||||
// If suspended leaves are present, then we should find the boundary for this scope and attach things
|
// If suspended leaves are present, then we should find the boundary for this scope and attach things
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
struct_variant_width = 100
|
||||||
|
struct_lit_width = 80
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
use dioxus::core::Mutation::*;
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
use dioxus_core::ElementId;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn text_diff() {
|
||||||
|
fn app(cx: Scope) -> Element {
|
||||||
|
let gen = cx.generation();
|
||||||
|
cx.render(rsx!( h1 { "hello {gen}" } ))
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut vdom = VirtualDom::new(app);
|
||||||
|
vdom.rebuild();
|
||||||
|
|
||||||
|
vdom.mark_dirty_scope(ScopeId(0));
|
||||||
|
assert_eq!(
|
||||||
|
vdom.render_immediate().edits,
|
||||||
|
[SetText { value: "hello 1", id: ElementId(2) }]
|
||||||
|
);
|
||||||
|
|
||||||
|
vdom.mark_dirty_scope(ScopeId(0));
|
||||||
|
assert_eq!(
|
||||||
|
vdom.render_immediate().edits,
|
||||||
|
[SetText { value: "hello 2", id: ElementId(2) }]
|
||||||
|
);
|
||||||
|
|
||||||
|
vdom.mark_dirty_scope(ScopeId(0));
|
||||||
|
assert_eq!(
|
||||||
|
vdom.render_immediate().edits,
|
||||||
|
[SetText { value: "hello 3", id: ElementId(2) }]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn element_swap() {
|
||||||
|
fn app(cx: Scope) -> Element {
|
||||||
|
let gen = cx.generation();
|
||||||
|
|
||||||
|
match gen % 2 {
|
||||||
|
0 => cx.render(rsx!( h1 { "hello 1" } )),
|
||||||
|
1 => cx.render(rsx!( h2 { "hello 2" } )),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut vdom = VirtualDom::new(app);
|
||||||
|
vdom.rebuild();
|
||||||
|
|
||||||
|
vdom.mark_dirty_scope(ScopeId(0));
|
||||||
|
dbg!(vdom.render_immediate());
|
||||||
|
|
||||||
|
vdom.mark_dirty_scope(ScopeId(0));
|
||||||
|
dbg!(vdom.render_immediate());
|
||||||
|
|
||||||
|
vdom.mark_dirty_scope(ScopeId(0));
|
||||||
|
dbg!(vdom.render_immediate());
|
||||||
|
|
||||||
|
vdom.mark_dirty_scope(ScopeId(0));
|
||||||
|
dbg!(vdom.render_immediate());
|
||||||
|
}
|
||||||
Reference in new issue
Block a user