WIP: Todomvc in progress
This commit is contained in:
1 parent
0d44f009b0
commit
b843dbd367
10 files changed
+416
-219
No files matched your search
@@ -887,6 +887,7 @@ impl<'a> DiffMachine<'a> {
|
||||
}
|
||||
|
||||
match old.len().cmp(&new.len()) {
|
||||
// old.len > new.len -> removing some nodes
|
||||
Ordering::Greater => {
|
||||
// [... parent prev_child]
|
||||
self.change_list.go_to_sibling(new.len());
|
||||
@@ -896,6 +897,7 @@ impl<'a> DiffMachine<'a> {
|
||||
self.remove_self_and_next_siblings(&old[new.len()..]);
|
||||
// [... parent]
|
||||
}
|
||||
// old.len < new.len -> adding some nodes
|
||||
Ordering::Less => {
|
||||
// [... parent last_child]
|
||||
self.change_list.go_up();
|
||||
@@ -903,6 +905,7 @@ impl<'a> DiffMachine<'a> {
|
||||
self.change_list.commit_traversal();
|
||||
self.create_and_append_children(&new[old.len()..]);
|
||||
}
|
||||
// old.len == new.len -> no nodes added/removed, but πerhaps changed
|
||||
Ordering::Equal => {
|
||||
// [... parent child]
|
||||
self.change_list.go_up();
|
||||
@@ -924,6 +927,7 @@ impl<'a> DiffMachine<'a> {
|
||||
// When this function returns, the change list stack is in the same state.
|
||||
pub fn remove_all_children(&mut self, old: &[VNode<'a>]) {
|
||||
debug_assert!(self.change_list.traversal_is_committed());
|
||||
log::debug!("REMOVING CHILDREN");
|
||||
for _child in old {
|
||||
// registry.remove_subtree(child);
|
||||
}
|
||||
@@ -958,7 +962,27 @@ impl<'a> DiffMachine<'a> {
|
||||
// [... parent]
|
||||
pub fn remove_self_and_next_siblings(&mut self, old: &[VNode<'a>]) {
|
||||
debug_assert!(self.change_list.traversal_is_committed());
|
||||
for _child in old {
|
||||
for child in old {
|
||||
if let VNode::Component(vcomp) = child {
|
||||
// self.change_list
|
||||
// .create_text_node("placeholder for vcomponent");
|
||||
|
||||
let root_id = vcomp.stable_addr.as_ref().borrow().unwrap();
|
||||
self.lifecycle_events.push_back(LifeCycleEvent::Remove {
|
||||
root_id,
|
||||
stable_scope_addr: Rc::downgrade(&vcomp.ass_scope),
|
||||
})
|
||||
// let id = get_id();
|
||||
// *component.stable_addr.as_ref().borrow_mut() = Some(id);
|
||||
// self.change_list.save_known_root(id);
|
||||
// let scope = Rc::downgrade(&component.ass_scope);
|
||||
// self.lifecycle_events.push_back(LifeCycleEvent::Mount {
|
||||
// caller: Rc::downgrade(&component.caller),
|
||||
// root_id: id,
|
||||
// stable_scope_addr: scope,
|
||||
// });
|
||||
}
|
||||
|
||||
// registry.remove_subtree(child);
|
||||
}
|
||||
self.change_list.remove_self_and_next_siblings();
|
||||
|
||||
@@ -26,7 +26,7 @@ use std::{
|
||||
any::{Any, TypeId},
|
||||
borrow::{Borrow, BorrowMut},
|
||||
cell::RefCell,
|
||||
collections::{HashMap, HashSet},
|
||||
collections::{HashMap, HashSet, VecDeque},
|
||||
fmt::Debug,
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
@@ -277,7 +277,7 @@ impl VirtualDom {
|
||||
// Start a new mutable borrow to components
|
||||
// We are guaranteeed that this scope is unique because we are tracking which nodes have modified
|
||||
|
||||
let mut cur_component = self.components.try_get_mut(update.idx).unwrap();
|
||||
let cur_component = self.components.try_get_mut(update.idx).unwrap();
|
||||
|
||||
cur_component.run_scope()?;
|
||||
|
||||
@@ -323,7 +323,12 @@ impl VirtualDom {
|
||||
})
|
||||
})?;
|
||||
|
||||
cur_component.children.borrow_mut().insert(idx);
|
||||
{
|
||||
let cur_component = self.components.try_get_mut(update.idx).unwrap();
|
||||
let mut ch = cur_component.children.borrow_mut();
|
||||
ch.insert(idx);
|
||||
std::mem::drop(ch);
|
||||
}
|
||||
|
||||
// Grab out that component
|
||||
let new_component = self.components.try_get_mut(idx).unwrap();
|
||||
@@ -415,8 +420,33 @@ impl VirtualDom {
|
||||
root_id,
|
||||
stable_scope_addr,
|
||||
} => {
|
||||
unimplemented!("This feature (Remove) is unimplemented")
|
||||
let id = stable_scope_addr
|
||||
.upgrade()
|
||||
.unwrap()
|
||||
.as_ref()
|
||||
.borrow()
|
||||
.unwrap();
|
||||
|
||||
log::warn!("Removing node {:#?}", id);
|
||||
|
||||
// This would normally be recursive but makes sense to do linear to
|
||||
let mut children_to_remove = VecDeque::new();
|
||||
children_to_remove.push_back(id);
|
||||
|
||||
// Accumulate all the child components that need to be removed
|
||||
while let Some(child_id) = children_to_remove.pop_back() {
|
||||
let comp = self.components.try_get(child_id).unwrap();
|
||||
let children = comp.children.borrow();
|
||||
for child in children.iter() {
|
||||
children_to_remove.push_front(*child);
|
||||
}
|
||||
log::debug!("Removing component: {:#?}", child_id);
|
||||
self.components
|
||||
.with(|components| components.remove(child_id).unwrap())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
LifeCycleEvent::Replace {
|
||||
caller,
|
||||
root_id: id,
|
||||
@@ -442,6 +472,8 @@ pub struct Scope {
|
||||
// The parent's scope ID
|
||||
pub parent: Option<ScopeIdx>,
|
||||
|
||||
// IDs of children that this scope has created
|
||||
// This enables us to drop the children and their children when this scope is destroyed
|
||||
pub children: RefCell<HashSet<ScopeIdx>>,
|
||||
|
||||
// A reference to the list of components.
|
||||
|
||||
Reference in new issue
Block a user