update persistant_iterator utility
This commit is contained in:
1 parent
d2047ed744
commit
72184e65ef
3 files changed
+213
-213
No files matched your search
File renamed without changes.
@@ -62,7 +62,7 @@ impl<S: State> RealDom<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn element_to_node_id(&self, element_id: ElementId) -> RealNodeId {
|
pub fn element_to_node_id(&self, element_id: ElementId) -> RealNodeId {
|
||||||
self.node_id_mapping.get(element_id.0).unwrap().unwrap()
|
self.node_id_mapping.get(element_id.0).unwrap().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+212
-212
@@ -1,225 +1,225 @@
|
|||||||
// use crate::{real_dom::RealDom, state::State, RealNodeId};
|
use crate::{node::NodeType, real_dom::RealDom, state::State, tree::TreeView, NodeId, RealNodeId};
|
||||||
// use dioxus_core::{ElementId, Mutations};
|
use dioxus_core::{Mutation, Mutations};
|
||||||
|
|
||||||
// pub enum ElementProduced {
|
pub enum ElementProduced {
|
||||||
// /// The iterator produced an element by progressing to the next node in a depth first order.
|
/// The iterator produced an element by progressing to the next node in a depth first order.
|
||||||
// Progressed(RealNodeId),
|
Progressed(RealNodeId),
|
||||||
// /// The iterator reached the end of the tree and looped back to the root
|
/// The iterator reached the end of the tree and looped back to the root
|
||||||
// Looped(RealNodeId),
|
Looped(RealNodeId),
|
||||||
// }
|
}
|
||||||
// impl ElementProduced {
|
impl ElementProduced {
|
||||||
// pub fn id(&self) -> RealNodeId {
|
pub fn id(&self) -> RealNodeId {
|
||||||
// match self {
|
match self {
|
||||||
// ElementProduced::Progressed(id) => *id,
|
ElementProduced::Progressed(id) => *id,
|
||||||
// ElementProduced::Looped(id) => *id,
|
ElementProduced::Looped(id) => *id,
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// #[derive(Debug)]
|
#[derive(Debug)]
|
||||||
// enum NodePosition {
|
enum NodePosition {
|
||||||
// AtNode,
|
AtNode,
|
||||||
// InChild(usize),
|
InChild(usize),
|
||||||
// }
|
}
|
||||||
|
|
||||||
// impl NodePosition {
|
impl NodePosition {
|
||||||
// fn map(&self, mut f: impl FnMut(usize) -> usize) -> Self {
|
fn map(&self, mut f: impl FnMut(usize) -> usize) -> Self {
|
||||||
// match self {
|
match self {
|
||||||
// Self::AtNode => Self::AtNode,
|
Self::AtNode => Self::AtNode,
|
||||||
// Self::InChild(i) => Self::InChild(f(*i)),
|
Self::InChild(i) => Self::InChild(f(*i)),
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// fn get_or_insert(&mut self, child_idx: usize) -> usize {
|
fn get_or_insert(&mut self, child_idx: usize) -> usize {
|
||||||
// match self {
|
match self {
|
||||||
// Self::AtNode => {
|
Self::AtNode => {
|
||||||
// *self = Self::InChild(child_idx);
|
*self = Self::InChild(child_idx);
|
||||||
// child_idx
|
child_idx
|
||||||
// }
|
}
|
||||||
// Self::InChild(i) => *i,
|
Self::InChild(i) => *i,
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// /// Focus systems need a iterator that can persist through changes in the [dioxus_core::VirtualDom].
|
/// Focus systems need a iterator that can persist through changes in the [dioxus_core::VirtualDom].
|
||||||
// /// This iterator traverses the tree depth first.
|
/// This iterator traverses the tree depth first.
|
||||||
// /// Iterate through it with [PersistantElementIter::next] [PersistantElementIter::prev], and update it with [PersistantElementIter::prune] (with data from [`dioxus_core::prelude::VirtualDom::work_with_deadline`]).
|
/// Iterate through it with [PersistantElementIter::next] [PersistantElementIter::prev], and update it with [PersistantElementIter::prune] (with data from [`dioxus_core::prelude::VirtualDom::work_with_deadline`]).
|
||||||
// /// The iterator loops around when it reaches the end or the beginning.
|
/// The iterator loops around when it reaches the end or the beginning.
|
||||||
// pub struct PersistantElementIter {
|
pub struct PersistantElementIter {
|
||||||
// // stack of elements and fragments
|
// stack of elements and fragments
|
||||||
// stack: smallvec::SmallVec<[(RealNodeId, NodePosition); 5]>,
|
stack: smallvec::SmallVec<[(RealNodeId, NodePosition); 5]>,
|
||||||
// }
|
}
|
||||||
|
|
||||||
// impl Default for PersistantElementIter {
|
impl Default for PersistantElementIter {
|
||||||
// fn default() -> Self {
|
fn default() -> Self {
|
||||||
// PersistantElementIter {
|
PersistantElementIter {
|
||||||
// stack: smallvec::smallvec![(RealNodeId::ElementId(ElementId(0)), NodePosition::AtNode)],
|
stack: smallvec::smallvec![(NodeId(0), NodePosition::AtNode)],
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// impl PersistantElementIter {
|
impl PersistantElementIter {
|
||||||
// pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
// Self::default()
|
Self::default()
|
||||||
// }
|
}
|
||||||
|
|
||||||
// /// remove stale element refreneces
|
/// remove stale element refreneces
|
||||||
// /// returns true if the focused element is removed
|
/// returns true if the focused element is removed
|
||||||
// pub fn prune<S: State>(&mut self, mutations: &Mutations, rdom: &RealDom<S>) -> bool {
|
pub fn prune<S: State>(&mut self, mutations: &Mutations, rdom: &RealDom<S>) -> bool {
|
||||||
// let mut changed = false;
|
let mut changed = false;
|
||||||
// let ids_removed: Vec<_> = mutations
|
let ids_removed: Vec<_> = mutations
|
||||||
// .edits
|
.edits
|
||||||
// .iter()
|
.iter()
|
||||||
// .filter_map(|e| {
|
.filter_map(|m| {
|
||||||
// // nodes within templates will never be removed
|
// nodes within templates will never be removedns
|
||||||
// if let DomEdit::Remove { root } = e {
|
if let Mutation::Remove { id } = m {
|
||||||
// let id = rdom.resolve_maybe_id(*root);
|
Some(rdom.element_to_node_id(*id))
|
||||||
// Some(id)
|
} else {
|
||||||
// } else {
|
None
|
||||||
// None
|
}
|
||||||
// }
|
})
|
||||||
// })
|
.collect();
|
||||||
// .collect();
|
// if any element is removed in the chain, remove it and its children from the stack
|
||||||
// // if any element is removed in the chain, remove it and its children from the stack
|
if let Some(r) = self
|
||||||
// if let Some(r) = self
|
.stack
|
||||||
// .stack
|
.iter()
|
||||||
// .iter()
|
.position(|(el_id, _)| ids_removed.iter().any(|id| el_id == id))
|
||||||
// .position(|(el_id, _)| ids_removed.iter().any(|id| el_id == id))
|
{
|
||||||
// {
|
self.stack.truncate(r);
|
||||||
// self.stack.truncate(r);
|
changed = true;
|
||||||
// changed = true;
|
}
|
||||||
// }
|
// if a child is removed or inserted before or at the current element, update the child index
|
||||||
// // if a child is removed or inserted before or at the current element, update the child index
|
for (el_id, child_idx) in self.stack.iter_mut() {
|
||||||
// for (el_id, child_idx) in self.stack.iter_mut() {
|
if let NodePosition::InChild(child_idx) = child_idx {
|
||||||
// if let NodePosition::InChild(child_idx) = child_idx {
|
if let Some(children) = &rdom.tree.children_ids(*el_id) {
|
||||||
// if let NodeType::Element { children, .. } = &rdom[*el_id].node_data.node_type {
|
for m in &mutations.edits {
|
||||||
// for m in &mutations.edits {
|
match m {
|
||||||
// match m {
|
Mutation::Remove { id } => {
|
||||||
// DomEdit::Remove { root } => {
|
let id = rdom.element_to_node_id(*id);
|
||||||
// let id = rdom.resolve_maybe_id(*root);
|
if children.iter().take(*child_idx + 1).any(|c| *c == id) {
|
||||||
// if children.iter().take(*child_idx + 1).any(|c| *c == id) {
|
*child_idx -= 1;
|
||||||
// *child_idx -= 1;
|
}
|
||||||
// }
|
}
|
||||||
// }
|
Mutation::InsertBefore { id, m } => {
|
||||||
// DomEdit::InsertBefore { root, nodes } => {
|
let id = rdom.element_to_node_id(*id);
|
||||||
// let id = rdom.resolve_maybe_id(*root);
|
if children.iter().take(*child_idx + 1).any(|c| *c == id) {
|
||||||
// let n = nodes.len();
|
*child_idx += *m;
|
||||||
// if children.iter().take(*child_idx + 1).any(|c| *c == id) {
|
}
|
||||||
// *child_idx += n as usize;
|
}
|
||||||
// }
|
Mutation::InsertAfter { id, m } => {
|
||||||
// }
|
let id = rdom.element_to_node_id(*id);
|
||||||
// DomEdit::InsertAfter { root, nodes } => {
|
if children.iter().take(*child_idx).any(|c| *c == id) {
|
||||||
// let id = rdom.resolve_maybe_id(*root);
|
*child_idx += *m;
|
||||||
// let n = nodes.len();
|
}
|
||||||
// if children.iter().take(*child_idx).any(|c| *c == id) {
|
}
|
||||||
// *child_idx += n as usize;
|
_ => (),
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// _ => (),
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
changed
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
// changed
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /// get the next element
|
/// get the next element
|
||||||
// pub fn next<S: State>(&mut self, rdom: &RealDom<S>) -> ElementProduced {
|
pub fn next<S: State>(&mut self, rdom: &RealDom<S>) -> ElementProduced {
|
||||||
// if self.stack.is_empty() {
|
if self.stack.is_empty() {
|
||||||
// let id = RealNodeId::ElementId(ElementId(0));
|
let id = NodeId(0);
|
||||||
// let new = (id, NodePosition::AtNode);
|
let new = (id, NodePosition::AtNode);
|
||||||
// self.stack.push(new);
|
self.stack.push(new);
|
||||||
// ElementProduced::Looped(id)
|
ElementProduced::Looped(id)
|
||||||
// } else {
|
} else {
|
||||||
// let (last, o_child_idx) = self.stack.last_mut().unwrap();
|
let (last, o_child_idx) = self.stack.last_mut().unwrap();
|
||||||
// let node = &rdom[*last];
|
let node = &rdom[*last];
|
||||||
// match &node.node_data.node_type {
|
match &node.node_data.node_type {
|
||||||
// NodeType::Element { children, .. } => {
|
NodeType::Element { .. } => {
|
||||||
// *o_child_idx = o_child_idx.map(|i| i + 1);
|
let children = rdom.tree.children_ids(*last).unwrap();
|
||||||
// // if we have children, go to the next child
|
*o_child_idx = o_child_idx.map(|i| i + 1);
|
||||||
// let child_idx = o_child_idx.get_or_insert(0);
|
// if we have children, go to the next child
|
||||||
// if child_idx >= children.len() {
|
let child_idx = o_child_idx.get_or_insert(0);
|
||||||
// self.pop();
|
if child_idx >= children.len() {
|
||||||
// self.next(rdom)
|
self.pop();
|
||||||
// } else {
|
self.next(rdom)
|
||||||
// let id = children[child_idx];
|
} else {
|
||||||
// if let NodeType::Element { .. } = &rdom[id].node_data.node_type {
|
let id = children[child_idx];
|
||||||
// self.stack.push((id, NodePosition::AtNode));
|
if let NodeType::Element { .. } = &rdom[id].node_data.node_type {
|
||||||
// }
|
self.stack.push((id, NodePosition::AtNode));
|
||||||
// ElementProduced::Progressed(id)
|
}
|
||||||
// }
|
ElementProduced::Progressed(id)
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NodeType::Text { .. } | NodeType::Placeholder { .. } => {
|
NodeType::Text { .. } | NodeType::Placeholder { .. } => {
|
||||||
// // we are at a leaf, so we are done
|
// we are at a leaf, so we are done
|
||||||
// ElementProduced::Progressed(self.pop())
|
ElementProduced::Progressed(self.pop())
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// /// get the previous element
|
/// get the previous element
|
||||||
// pub fn prev<S: State>(&mut self, rdom: &RealDom<S>) -> ElementProduced {
|
pub fn prev<S: State>(&mut self, rdom: &RealDom<S>) -> ElementProduced {
|
||||||
// // recursively add the last child element to the stack
|
// recursively add the last child element to the stack
|
||||||
// fn push_back<S: State>(
|
fn push_back<S: State>(
|
||||||
// stack: &mut smallvec::SmallVec<[(RealNodeId, NodePosition); 5]>,
|
stack: &mut smallvec::SmallVec<[(RealNodeId, NodePosition); 5]>,
|
||||||
// new_node: RealNodeId,
|
new_node: RealNodeId,
|
||||||
// rdom: &RealDom<S>,
|
rdom: &RealDom<S>,
|
||||||
// ) -> RealNodeId {
|
) -> RealNodeId {
|
||||||
// match &rdom[new_node].node_data.node_type {
|
match &rdom[new_node].node_data.node_type {
|
||||||
// NodeType::Element { children, .. } => {
|
NodeType::Element { .. } => {
|
||||||
// if children.is_empty() {
|
let children = rdom.tree.children_ids(new_node).unwrap();
|
||||||
// new_node
|
if children.is_empty() {
|
||||||
// } else {
|
new_node
|
||||||
// stack.push((new_node, NodePosition::InChild(children.len() - 1)));
|
} else {
|
||||||
// push_back(stack, *children.last().unwrap(), rdom)
|
stack.push((new_node, NodePosition::InChild(children.len() - 1)));
|
||||||
// }
|
push_back(stack, *children.last().unwrap(), rdom)
|
||||||
// }
|
}
|
||||||
// _ => new_node,
|
}
|
||||||
// }
|
_ => new_node,
|
||||||
// }
|
}
|
||||||
// if self.stack.is_empty() {
|
}
|
||||||
// let new_node = RealNodeId::ElementId(ElementId(0));
|
if self.stack.is_empty() {
|
||||||
// ElementProduced::Looped(push_back(&mut self.stack, new_node, rdom))
|
let new_node = NodeId(0);
|
||||||
// } else {
|
ElementProduced::Looped(push_back(&mut self.stack, new_node, rdom))
|
||||||
// let (last, o_child_idx) = self.stack.last_mut().unwrap();
|
} else {
|
||||||
// let node = &rdom[*last];
|
let (last, o_child_idx) = self.stack.last_mut().unwrap();
|
||||||
// match &node.node_data.node_type {
|
let node = &rdom[*last];
|
||||||
// NodeType::Element { children, .. } => {
|
match &node.node_data.node_type {
|
||||||
// // if we have children, go to the next child
|
NodeType::Element { .. } => {
|
||||||
// if let NodePosition::InChild(0) = o_child_idx {
|
let children = rdom.tree.children_ids(*last).unwrap();
|
||||||
// ElementProduced::Progressed(self.pop())
|
// if we have children, go to the next child
|
||||||
// } else {
|
if let NodePosition::InChild(0) = o_child_idx {
|
||||||
// *o_child_idx = o_child_idx.map(|i| i - 1);
|
ElementProduced::Progressed(self.pop())
|
||||||
// if let NodePosition::InChild(child_idx) = o_child_idx {
|
} else {
|
||||||
// if *child_idx >= children.len() || children.is_empty() {
|
*o_child_idx = o_child_idx.map(|i| i - 1);
|
||||||
// self.pop();
|
if let NodePosition::InChild(child_idx) = o_child_idx {
|
||||||
// self.prev(rdom)
|
if *child_idx >= children.len() || children.is_empty() {
|
||||||
// } else {
|
self.pop();
|
||||||
// let new_node = children[*child_idx];
|
self.prev(rdom)
|
||||||
// ElementProduced::Progressed(push_back(
|
} else {
|
||||||
// &mut self.stack,
|
let new_node = children[*child_idx];
|
||||||
// new_node,
|
ElementProduced::Progressed(push_back(
|
||||||
// rdom,
|
&mut self.stack,
|
||||||
// ))
|
new_node,
|
||||||
// }
|
rdom,
|
||||||
// } else {
|
))
|
||||||
// self.pop();
|
}
|
||||||
// self.prev(rdom)
|
} else {
|
||||||
// }
|
self.pop();
|
||||||
// }
|
self.prev(rdom)
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NodeType::Text { .. } | NodeType::Placeholder { .. } => {
|
NodeType::Text { .. } | NodeType::Placeholder { .. } => {
|
||||||
// // we are at a leaf, so we are done
|
// we are at a leaf, so we are done
|
||||||
// ElementProduced::Progressed(self.pop())
|
ElementProduced::Progressed(self.pop())
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// fn pop(&mut self) -> RealNodeId {
|
fn pop(&mut self) -> RealNodeId {
|
||||||
// self.stack.pop().unwrap().0
|
self.stack.pop().unwrap().0
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
Reference in new issue
Block a user