From 797feb565719a6e32ab9aa1e45c8d987d8172072 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 4 Mar 2024 15:09:31 -0600 Subject: [PATCH] remove logging, clean up some code, and fix effect test --- packages/core/src/diff/component.rs | 2 +- packages/core/src/dirty_scope.rs | 59 +++++++++++++++-------------- packages/core/src/render_signal.rs | 1 - packages/core/src/virtual_dom.rs | 18 ++++----- packages/core/tests/task.rs | 15 ++++---- 5 files changed, 47 insertions(+), 48 deletions(-) diff --git a/packages/core/src/diff/component.rs b/packages/core/src/diff/component.rs index 495f70549..afdfe4add 100644 --- a/packages/core/src/diff/component.rs +++ b/packages/core/src/diff/component.rs @@ -2,7 +2,7 @@ use std::ops::{Deref, DerefMut}; use crate::{ any_props::AnyProps, - innerlude::{DirtyScopes,ScopeOrder, ElementRef, MountId, VComponent, WriteMutations}, + innerlude::{DirtyScopes, ElementRef, MountId, ScopeOrder, VComponent, WriteMutations}, nodes::RenderReturn, nodes::VNode, scopes::ScopeId, diff --git a/packages/core/src/dirty_scope.rs b/packages/core/src/dirty_scope.rs index a9b0cb245..a740f0c5c 100644 --- a/packages/core/src/dirty_scope.rs +++ b/packages/core/src/dirty_scope.rs @@ -3,8 +3,8 @@ use crate::Task; use std::borrow::Borrow; use std::cell::Cell; use std::cell::RefCell; -use std::hash::Hash; use std::collections::BTreeSet; +use std::hash::Hash; #[derive(Debug, Clone, Copy, Eq)] pub struct ScopeOrder { @@ -82,34 +82,37 @@ impl DirtyScopes { match (dirty_scope, dirty_task) { (Some(scope), Some(task)) => { let tasks_order = task.borrow(); - if scope > tasks_order{ - let scope = self.scopes.pop_first().unwrap(); - Some(Work{ - scope: scope, - rerun_scope: true, - tasks: Vec::new(), - }) - } else if tasks_order> scope { - let task = self.tasks.pop_first().unwrap(); - Some(Work{ - scope: task.order, - rerun_scope: false, - tasks: task.tasks_queued.into_inner(), - }) - } - else { - let scope = self.scopes.pop_first().unwrap(); - let task = self.tasks.pop_first().unwrap(); - Some(Work{ - scope: scope, - rerun_scope: true, - tasks: task.tasks_queued.into_inner(), - }) + match scope.cmp(tasks_order) { + std::cmp::Ordering::Less => { + let scope = self.scopes.pop_first().unwrap(); + Some(Work { + scope: scope, + rerun_scope: true, + tasks: Vec::new(), + }) + } + std::cmp::Ordering::Greater => { + let task = self.tasks.pop_first().unwrap(); + Some(Work { + scope: task.order, + rerun_scope: false, + tasks: task.tasks_queued.into_inner(), + }) + } + std::cmp::Ordering::Equal => { + let scope = self.scopes.pop_first().unwrap(); + let task = self.tasks.pop_first().unwrap(); + Some(Work { + scope: scope, + rerun_scope: true, + tasks: task.tasks_queued.into_inner(), + }) + } } } (Some(scope), None) => { let scope = self.scopes.pop_first().unwrap(); - Some(Work{ + Some(Work { scope: scope, rerun_scope: true, tasks: Vec::new(), @@ -117,13 +120,13 @@ impl DirtyScopes { } (None, Some(task)) => { let task = self.tasks.pop_first().unwrap(); - Some(Work{ + Some(Work { scope: task.order, rerun_scope: false, tasks: task.tasks_queued.into_inner(), }) } - (None, None) => None + (None, None) => None, } } @@ -132,6 +135,7 @@ impl DirtyScopes { } } +#[derive(Debug)] pub struct Work { pub scope: ScopeOrder, pub rerun_scope: bool, @@ -154,7 +158,6 @@ impl From for DirtyTasks { } impl DirtyTasks { - pub fn queue_task(&self, task: Task) { self.tasks_queued.borrow_mut().push(task); } diff --git a/packages/core/src/render_signal.rs b/packages/core/src/render_signal.rs index dac72dd7f..860885de1 100644 --- a/packages/core/src/render_signal.rs +++ b/packages/core/src/render_signal.rs @@ -32,7 +32,6 @@ impl RenderSignal { waker: None, })); self.wakers.borrow_mut().push(inner.clone()); - println!("RenderSignalFutureInner"); let waker = RenderSignalFuture { inner }; waker } diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index e1657c071..0d594c43b 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -8,8 +8,8 @@ use crate::{ any_props::AnyProps, arena::ElementId, innerlude::{ - DirtyScopes, ElementRef, ErrorBoundary, NoOpMutations, SchedulerMsg, ScopeState, VNodeMount, - VProps, WriteMutations, + DirtyScopes, ElementRef, ErrorBoundary, NoOpMutations, SchedulerMsg, ScopeState, + VNodeMount, VProps, WriteMutations, }, nodes::RenderReturn, nodes::{Template, TemplateId}, @@ -185,7 +185,6 @@ pub struct VirtualDom { pub(crate) scopes: Slab, pub(crate) dirty_scopes: DirtyScopes, - pub(crate) scopes_need_rerun: bool, // Maps a template path to a map of byte indexes to templates pub(crate) templates: FxHashMap>, @@ -313,7 +312,6 @@ impl VirtualDom { rx, runtime: Runtime::new(tx), scopes: Default::default(), - scopes_need_rerun: false, dirty_scopes: Default::default(), templates: Default::default(), queued_templates: Default::default(), @@ -378,7 +376,6 @@ impl VirtualDom { }; tracing::trace!("Marking scope {:?} as dirty", id); - self.scopes_need_rerun = true; let order = ScopeOrder::new(scope.height(), id); self.dirty_scopes.queue_scope(order); } @@ -450,7 +447,7 @@ impl VirtualDom { self.process_events(); // Now that we have collected all queued work, we should check if we have any dirty scopes. If there are not, then we can poll any queued futures - if self.scopes_need_rerun { + if self.dirty_scopes.has_dirty_scopes() { return; } @@ -485,7 +482,7 @@ impl VirtualDom { self.queue_events(); // Now that we have collected all queued work, we should check if we have any dirty scopes. If there are not, then we can poll any queued futures - if self.scopes_need_rerun { + if self.dirty_scopes.has_dirty_scopes() { return; } @@ -498,12 +495,12 @@ impl VirtualDom { } // Then poll any tasks that might be pending - let tasks = std::mem::take(&mut *task.tasks_queued.borrow_mut()); + let tasks = task.tasks_queued.into_inner(); for task in tasks { let _ = self.runtime.handle_task_wakeup(task); // Running that task, may mark a scope higher up as dirty. If it does, return from the function early self.queue_events(); - if self.scopes_need_rerun { + if self.dirty_scopes.has_dirty_scopes() { return; } } @@ -602,7 +599,7 @@ impl VirtualDom { let _runtime = RuntimeGuard::new(self.runtime.clone()); // Then, poll any tasks that might be pending in the scope // This will run effects, so this **must** be done after the scope is diffed - for task in work.tasks{ + for task in work.tasks { let _ = self.runtime.handle_task_wakeup(task); } // If the scope is dirty, run the scope and get the mutations @@ -615,7 +612,6 @@ impl VirtualDom { } self.runtime.render_signal.send(); - self.scopes_need_rerun = false; } /// [`Self::render_immediate`] to a vector of mutations for testing purposes diff --git a/packages/core/tests/task.rs b/packages/core/tests/task.rs index 8828606f6..8c73c0d28 100644 --- a/packages/core/tests/task.rs +++ b/packages/core/tests/task.rs @@ -104,10 +104,11 @@ async fn flushing() { }); use_hook(|| { spawn(async move { + let mut channel = BROADCAST.with(|b| b.1.resubscribe()); for _ in 0..10 { flush_sync().await; - BROADCAST.with(|b| b.1.resubscribe()).recv().await.unwrap(); println!("Task 1 recved"); + channel.recv().await.unwrap(); println!("Task 1"); SEQUENCE.with(|s| s.borrow_mut().push(1)); } @@ -116,10 +117,11 @@ async fn flushing() { use_hook(|| { spawn(async move { + let mut channel = BROADCAST.with(|b| b.1.resubscribe()); for _ in 0..10 { flush_sync().await; - BROADCAST.with(|b| b.1.resubscribe()).recv().await.unwrap(); println!("Task 2 recved"); + channel.recv().await.unwrap(); println!("Task 2"); SEQUENCE.with(|s| s.borrow_mut().push(2)); } @@ -135,16 +137,15 @@ async fn flushing() { let fut = async { // Trigger the flush by waiting for work - for i in 0..30 { + for i in 0..10 { BROADCAST.with(|b| b.0.send(()).unwrap()); - tokio::select! { - _ = dom.wait_for_work() => {} - _ = tokio::time::sleep(Duration::from_millis(10)) => {} - } dom.mark_dirty(ScopeId(0)); + dom.wait_for_work().await; dom.render_immediate(&mut dioxus_core::NoOpMutations); println!("Flushed {}", i); } + BROADCAST.with(|b| b.0.send(()).unwrap()); + dom.wait_for_work().await; }; tokio::select! {