Merge branch 'upstream' into make-widgets-cross-framework
This commit is contained in:
commit
8b5f2cb02f
22 files changed
+380
-122
No files matched your search
@@ -27,7 +27,7 @@ pub struct DioxusState {
|
||||
|
||||
impl DioxusState {
|
||||
/// Initialize the DioxusState in the RealDom
|
||||
pub fn create(rdom: &mut RealDom) -> Self {
|
||||
pub fn create<V: FromAnyValue + Send + Sync>(rdom: &mut RealDom<V>) -> Self {
|
||||
let root_id = rdom.root_id();
|
||||
let mut root = rdom.get_mut(root_id).unwrap();
|
||||
root.insert(ElementIdComponent(ElementId(0)));
|
||||
@@ -48,7 +48,11 @@ impl DioxusState {
|
||||
self.node_id_mapping.get(element_id.0).copied().flatten()
|
||||
}
|
||||
|
||||
fn set_element_id(&mut self, mut node: NodeMut, element_id: ElementId) {
|
||||
fn set_element_id<V: FromAnyValue + Send + Sync>(
|
||||
&mut self,
|
||||
mut node: NodeMut<V>,
|
||||
element_id: ElementId,
|
||||
) {
|
||||
let node_id = node.id();
|
||||
node.insert(ElementIdComponent(element_id));
|
||||
if self.node_id_mapping.len() <= element_id.0 {
|
||||
@@ -57,7 +61,7 @@ impl DioxusState {
|
||||
self.node_id_mapping[element_id.0] = Some(node_id);
|
||||
}
|
||||
|
||||
fn load_child(&self, rdom: &RealDom, path: &[u8]) -> NodeId {
|
||||
fn load_child<V: FromAnyValue + Send + Sync>(&self, rdom: &RealDom<V>, path: &[u8]) -> NodeId {
|
||||
let mut current = rdom.get(*self.stack.last().unwrap()).unwrap();
|
||||
for i in path {
|
||||
let new_id = current.child_ids()[*i as usize];
|
||||
@@ -67,7 +71,11 @@ impl DioxusState {
|
||||
}
|
||||
|
||||
/// Updates the dom with some mutations and return a set of nodes that were updated. Pass the dirty nodes to update_state.
|
||||
pub fn apply_mutations(&mut self, rdom: &mut RealDom, mutations: Mutations) {
|
||||
pub fn apply_mutations<V: FromAnyValue + Send + Sync>(
|
||||
&mut self,
|
||||
rdom: &mut RealDom<V>,
|
||||
mutations: Mutations,
|
||||
) {
|
||||
for template in mutations.templates {
|
||||
let mut template_root_ids = Vec::new();
|
||||
for root in template.roots {
|
||||
@@ -222,7 +230,10 @@ impl DioxusState {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_template_node(rdom: &mut RealDom, node: &TemplateNode) -> NodeId {
|
||||
fn create_template_node<V: FromAnyValue + Send + Sync>(
|
||||
rdom: &mut RealDom<V>,
|
||||
node: &TemplateNode,
|
||||
) -> NodeId {
|
||||
match node {
|
||||
TemplateNode::Element {
|
||||
tag,
|
||||
|
||||
@@ -16,37 +16,27 @@ use crate::{NodeId, NodeMask};
|
||||
|
||||
#[derive(Default)]
|
||||
struct DirtyNodes {
|
||||
passes_dirty: Vec<u64>,
|
||||
nodes_dirty: FxHashSet<NodeId>,
|
||||
}
|
||||
|
||||
impl DirtyNodes {
|
||||
pub fn add_node(&mut self, node_id: NodeId) {
|
||||
let node_id = node_id.uindex();
|
||||
let index = node_id / 64;
|
||||
let bit = node_id % 64;
|
||||
let encoded = 1 << bit;
|
||||
if let Some(passes) = self.passes_dirty.get_mut(index) {
|
||||
*passes |= encoded;
|
||||
} else {
|
||||
self.passes_dirty.resize(index + 1, 0);
|
||||
self.passes_dirty[index] |= encoded;
|
||||
}
|
||||
self.nodes_dirty.insert(node_id);
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.passes_dirty.iter().all(|dirty| *dirty == 0)
|
||||
self.nodes_dirty.is_empty()
|
||||
}
|
||||
|
||||
pub fn pop(&mut self) -> Option<usize> {
|
||||
let index = self.passes_dirty.iter().position(|dirty| *dirty != 0)?;
|
||||
let passes = self.passes_dirty[index];
|
||||
let node_id = passes.trailing_zeros();
|
||||
let encoded = 1 << node_id;
|
||||
self.passes_dirty[index] &= !encoded;
|
||||
Some((index * 64) + node_id as usize)
|
||||
pub fn pop(&mut self) -> Option<NodeId> {
|
||||
self.nodes_dirty.iter().next().copied().map(|id| {
|
||||
self.nodes_dirty.remove(&id);
|
||||
id
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Tracks the dirty nodes sorted by height for each pass. We resolve passes based on the height of the node in order to avoid resolving any node twice in a pass.
|
||||
#[derive(Clone, Unique)]
|
||||
pub struct DirtyNodeStates {
|
||||
dirty: Arc<FxHashMap<TypeId, RwLock<BTreeMap<u16, DirtyNodes>>>>,
|
||||
@@ -76,7 +66,7 @@ impl DirtyNodeStates {
|
||||
}
|
||||
}
|
||||
|
||||
fn pop_front(&self, pass_id: TypeId) -> Option<(u16, usize)> {
|
||||
fn pop_front(&self, pass_id: TypeId) -> Option<(u16, NodeId)> {
|
||||
let mut values = self.dirty.get(&pass_id)?.write();
|
||||
let mut value = values.first_entry()?;
|
||||
let height = *value.key();
|
||||
@@ -89,7 +79,7 @@ impl DirtyNodeStates {
|
||||
Some((height, id))
|
||||
}
|
||||
|
||||
fn pop_back(&self, pass_id: TypeId) -> Option<(u16, usize)> {
|
||||
fn pop_back(&self, pass_id: TypeId) -> Option<(u16, NodeId)> {
|
||||
let mut values = self.dirty.get(&pass_id)?.write();
|
||||
let mut value = values.last_entry()?;
|
||||
let height = *value.key();
|
||||
@@ -214,7 +204,6 @@ pub fn run_pass<V: FromAnyValue + Send + Sync>(
|
||||
match pass_direction {
|
||||
PassDirection::ParentToChild => {
|
||||
while let Some((height, id)) = dirty.pop_front(type_id) {
|
||||
let id = tree.id_at(id).unwrap();
|
||||
if (update_node)(id, ctx) {
|
||||
nodes_updated.insert(id);
|
||||
for id in tree.children_ids(id) {
|
||||
@@ -227,7 +216,6 @@ pub fn run_pass<V: FromAnyValue + Send + Sync>(
|
||||
}
|
||||
PassDirection::ChildToParent => {
|
||||
while let Some((height, id)) = dirty.pop_back(type_id) {
|
||||
let id = tree.id_at(id).unwrap();
|
||||
if (update_node)(id, ctx) {
|
||||
nodes_updated.insert(id);
|
||||
if let Some(id) = tree.parent_id(id) {
|
||||
@@ -240,7 +228,6 @@ pub fn run_pass<V: FromAnyValue + Send + Sync>(
|
||||
}
|
||||
PassDirection::AnyOrder => {
|
||||
while let Some((height, id)) = dirty.pop_back(type_id) {
|
||||
let id = tree.id_at(id).unwrap();
|
||||
if (update_node)(id, ctx) {
|
||||
nodes_updated.insert(id);
|
||||
for dependant in &dependants {
|
||||
|
||||
@@ -49,7 +49,7 @@ pub trait TreeMut: TreeRef {
|
||||
|
||||
impl<'a> TreeRef for TreeRefView<'a> {
|
||||
fn parent_id(&self, id: NodeId) -> Option<NodeId> {
|
||||
self.get(id).unwrap().parent
|
||||
self.get(id).ok()?.parent
|
||||
}
|
||||
|
||||
fn children_ids(&self, id: NodeId) -> Vec<NodeId> {
|
||||
@@ -59,7 +59,7 @@ impl<'a> TreeRef for TreeRefView<'a> {
|
||||
}
|
||||
|
||||
fn height(&self, id: NodeId) -> Option<u16> {
|
||||
Some(self.get(id).unwrap().height)
|
||||
Some(self.get(id).ok()?.height)
|
||||
}
|
||||
|
||||
fn contains(&self, id: NodeId) -> bool {
|
||||
|
||||
Reference in new issue
Block a user