wip: stack-based "real child iterator"
This commit is contained in:
1 parent
0826fdfee1
commit
895cc0142b
6 files changed
+356
-121
No files matched your search
+172
-1
@@ -188,7 +188,18 @@ impl<'a, Dom: RealDom> DiffMachine<'a, Dom> {
|
||||
}
|
||||
|
||||
// TODO on handling these types
|
||||
VNode::Fragment(_) => todo!(),
|
||||
VNode::Fragment(frag) => {
|
||||
if frag.children.len() == 0 {
|
||||
// do nothing
|
||||
} else {
|
||||
self.create(&frag.children[0]);
|
||||
self.dom.replace_with();
|
||||
for child in frag.children.iter().skip(1) {
|
||||
self.create(child);
|
||||
self.dom.append_child();
|
||||
}
|
||||
}
|
||||
}
|
||||
VNode::Suspended => todo!(),
|
||||
},
|
||||
|
||||
@@ -437,6 +448,10 @@ impl<'a, Dom: RealDom> DiffMachine<'a, Dom> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn iter_children(&self, node: &VNode<'a>) -> ChildIterator<'a> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Dom: RealDom> DiffMachine<'a, Dom> {
|
||||
@@ -1206,3 +1221,159 @@ enum KeyedPrefixResult {
|
||||
// the beginning of `new` and `old` we already processed.
|
||||
MoreWorkToDo(usize),
|
||||
}
|
||||
|
||||
struct ChildIterator<'a> {
|
||||
scopes: &'a ScopeArena,
|
||||
|
||||
// Heuristcally we should never bleed into 5 completely nested fragments/components
|
||||
// Smallvec lets us stack allocate our little stack machine so the vast majority of cases are sane
|
||||
stack: smallvec::SmallVec<[(u16, &'a VNode<'a>); 5]>,
|
||||
}
|
||||
|
||||
impl<'a> ChildIterator<'a> {
|
||||
fn new(starter: &'a VNode<'a>, scopes: &'a ScopeArena) -> Self {
|
||||
Self {
|
||||
scopes,
|
||||
stack: smallvec::smallvec![(0, starter)],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for ChildIterator<'a> {
|
||||
type Item = &'a VNode<'a>;
|
||||
|
||||
fn next(&mut self) -> Option<&'a VNode<'a>> {
|
||||
let mut should_pop = false;
|
||||
let mut returned_node = None;
|
||||
let mut should_push = None;
|
||||
|
||||
while returned_node.is_none() {
|
||||
if let Some((count, node)) = self.stack.last_mut() {
|
||||
match node {
|
||||
// We can only exit our looping when we get "real" nodes
|
||||
VNode::Element(_) | VNode::Text(_) => {
|
||||
// We've recursed INTO an element/text
|
||||
// We need to recurse *out* of it and move forward to the next
|
||||
// println!("Found element! Returning it!");
|
||||
should_pop = true;
|
||||
returned_node = Some(&**node);
|
||||
}
|
||||
|
||||
// If we get a fragment we push the next child
|
||||
VNode::Fragment(frag) => {
|
||||
let _count = *count as usize;
|
||||
if _count >= frag.children.len() {
|
||||
should_pop = true;
|
||||
} else {
|
||||
should_push = Some(&frag.children[_count]);
|
||||
}
|
||||
}
|
||||
|
||||
// Immediately abort suspended nodes - can't do anything with them yet
|
||||
// VNode::Suspended => should_pop = true,
|
||||
VNode::Suspended => todo!(),
|
||||
|
||||
// For components, we load their root and push them onto the stack
|
||||
VNode::Component(sc) => {
|
||||
let scope = self.scopes.try_get(sc.ass_scope.borrow().unwrap()).unwrap();
|
||||
|
||||
// Simply swap the current node on the stack with the root of the component
|
||||
*node = scope.root();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// If there's no more items on the stack, we're done!
|
||||
return None;
|
||||
}
|
||||
|
||||
if should_pop {
|
||||
self.stack.pop();
|
||||
if let Some((id, _)) = self.stack.last_mut() {
|
||||
*id += 1;
|
||||
}
|
||||
should_pop = false;
|
||||
}
|
||||
|
||||
if let Some(push) = should_push {
|
||||
self.stack.push((0, push));
|
||||
should_push = None;
|
||||
}
|
||||
}
|
||||
|
||||
returned_node
|
||||
}
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate as dioxus;
|
||||
use crate::innerlude::*;
|
||||
use crate::util::DebugDom;
|
||||
use dioxus_core_macro::*;
|
||||
|
||||
#[test]
|
||||
fn test_child_iterator() {
|
||||
static App: FC<()> = |cx| {
|
||||
cx.render(rsx! {
|
||||
Fragment {
|
||||
div {}
|
||||
h1 {}
|
||||
h2 {}
|
||||
h3 {}
|
||||
Fragment {
|
||||
"internal node"
|
||||
div {
|
||||
"baller text shouldn't show up"
|
||||
}
|
||||
p {
|
||||
|
||||
}
|
||||
Fragment {
|
||||
Fragment {
|
||||
"wow you really like framgents"
|
||||
Fragment {
|
||||
"why are you like this"
|
||||
Fragment {
|
||||
"just stop now please"
|
||||
Fragment {
|
||||
"this hurts"
|
||||
Fragment {
|
||||
"who needs this many fragments?????"
|
||||
Fragment {
|
||||
"just... fine..."
|
||||
Fragment {
|
||||
"no"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"my text node 1"
|
||||
"my text node 2"
|
||||
"my text node 3"
|
||||
"my text node 4"
|
||||
}
|
||||
})
|
||||
};
|
||||
let mut dom = VirtualDom::new(App);
|
||||
let mut renderer = DebugDom::new();
|
||||
dom.rebuild(&mut renderer).unwrap();
|
||||
let starter = dom.base_scope().root();
|
||||
let ite = ChildIterator::new(starter, &dom.components);
|
||||
for child in ite {
|
||||
match child {
|
||||
VNode::Element(el) => println!("Found: Element {}", el.tag_name),
|
||||
VNode::Text(t) => println!("Found: Text {:?}", t.text),
|
||||
|
||||
// These would represent failing cases.
|
||||
VNode::Fragment(_) => panic!("Found: Fragment"),
|
||||
VNode::Suspended => panic!("Found: Suspended"),
|
||||
VNode::Component(_) => panic!("Found: Component"),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user