From 287416cfa7b08c9c7ab7a43efcaeb514af54c4de Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 4 Mar 2024 19:52:17 -0600 Subject: [PATCH] rename flush_sync to wait_for_next_render --- packages/core/src/global_context.rs | 6 +++--- packages/core/src/lib.rs | 14 +++++++------- packages/core/tests/task.rs | 4 ++-- packages/hooks/src/use_effect.rs | 2 +- packages/hooks/src/use_future.rs | 10 +++------- packages/hooks/src/use_memo.rs | 2 -- packages/signals/src/signal.rs | 3 +-- 7 files changed, 17 insertions(+), 24 deletions(-) diff --git a/packages/core/src/global_context.rs b/packages/core/src/global_context.rs index d2d83b298..738bf64c3 100644 --- a/packages/core/src/global_context.rs +++ b/packages/core/src/global_context.rs @@ -250,14 +250,14 @@ pub fn after_render(f: impl FnMut() + 'static) { Runtime::with_current_scope(|cx| cx.push_after_render(f)); } -/// Wait for the virtualdom to finish its sync work before proceeding +/// Wait for the next render to complete /// /// This is useful if you've just triggered an update and want to wait for it to finish before proceeding with valid /// DOM nodes. /// -/// Effects rely on this to ensure that they only run effects after the DOM has been updated. Without flush_sync effects +/// Effects rely on this to ensure that they only run effects after the DOM has been updated. Without wait_for_next_render effects /// are run immediately before diffing the DOM, which causes all sorts of out-of-sync weirdness. -pub async fn flush_sync() { +pub async fn wait_for_next_render() { // Wait for the flush lock to be available // We release it immediately, so it's impossible for the lock to be held longer than this function Runtime::with(|rt| rt.render_signal.subscribe()) diff --git a/packages/core/src/lib.rs b/packages/core/src/lib.rs index 5b3b6216e..c2e1dcacd 100644 --- a/packages/core/src/lib.rs +++ b/packages/core/src/lib.rs @@ -88,14 +88,14 @@ pub use crate::innerlude::{ /// This includes types like [`Element`], and [`Component`]. pub mod prelude { pub use crate::innerlude::{ - consume_context, consume_context_from_scope, current_scope_id, fc_to_builder, flush_sync, - generation, has_context, needs_update, needs_update_any, parent_scope, provide_context, + consume_context, consume_context_from_scope, current_scope_id, fc_to_builder, generation, + has_context, needs_update, needs_update_any, parent_scope, provide_context, provide_root_context, remove_future, schedule_update, schedule_update_any, spawn, spawn_forever, suspend, try_consume_context, use_after_render, use_before_render, use_drop, - use_error_boundary, use_hook, use_hook_with_cleanup, AnyValue, Attribute, Component, - ComponentFunction, Element, ErrorBoundary, Event, EventHandler, Fragment, HasAttributes, - IntoAttributeValue, IntoDynNode, OptionStringFromMarker, Properties, Runtime, RuntimeGuard, - ScopeId, ScopeState, SuperFrom, SuperInto, Task, Template, TemplateAttribute, TemplateNode, - Throw, VNode, VNodeInner, VirtualDom, + use_error_boundary, use_hook, use_hook_with_cleanup, wait_for_next_render, AnyValue, + Attribute, Component, ComponentFunction, Element, ErrorBoundary, Event, EventHandler, + Fragment, HasAttributes, IntoAttributeValue, IntoDynNode, OptionStringFromMarker, + Properties, Runtime, RuntimeGuard, ScopeId, ScopeState, SuperFrom, SuperInto, Task, + Template, TemplateAttribute, TemplateNode, Throw, VNode, VNodeInner, VirtualDom, }; } diff --git a/packages/core/tests/task.rs b/packages/core/tests/task.rs index 9c3353827..f82b5473d 100644 --- a/packages/core/tests/task.rs +++ b/packages/core/tests/task.rs @@ -103,7 +103,7 @@ async fn flushing() { spawn(async move { let mut channel = BROADCAST.with(|b| b.1.resubscribe()); for _ in 0..10 { - flush_sync().await; + wait_for_next_render().await; println!("Task 1 recved"); channel.recv().await.unwrap(); println!("Task 1"); @@ -116,7 +116,7 @@ async fn flushing() { spawn(async move { let mut channel = BROADCAST.with(|b| b.1.resubscribe()); for _ in 0..10 { - flush_sync().await; + wait_for_next_render().await; println!("Task 2 recved"); channel.recv().await.unwrap(); println!("Task 2"); diff --git a/packages/hooks/src/use_effect.rs b/packages/hooks/src/use_effect.rs index 771a2478a..33e65309b 100644 --- a/packages/hooks/src/use_effect.rs +++ b/packages/hooks/src/use_effect.rs @@ -35,7 +35,7 @@ pub fn use_effect(mut callback: impl FnMut() + 'static) { rc.changed().await; // Wait for the dom the be finished with sync work - flush_sync().await; + wait_for_next_render().await; } }); }); diff --git a/packages/hooks/src/use_future.rs b/packages/hooks/src/use_future.rs index 53a67087c..ae35770bc 100644 --- a/packages/hooks/src/use_future.rs +++ b/packages/hooks/src/use_future.rs @@ -1,16 +1,13 @@ #![allow(missing_docs)] use crate::{use_callback, use_hook_did_run, use_signal, UseCallback}; -use dioxus_core::{ - prelude::{flush_sync, spawn, use_hook}, - Task, -}; +use dioxus_core::{prelude::*, Task}; use dioxus_signals::*; use dioxus_signals::{Readable, Writable}; use std::future::Future; /// A hook that allows you to spawn a future. /// This future will **not** run on the server -/// The future is spawned on the next call to `flush_sync` which means that it will not run on the server. +/// The future is spawned on the next call to `wait_for_next_render` which means that it will not run on the server. /// To run a future on the server, you should use `spawn` directly. /// `use_future` **won't return a value**. /// If you want to return a value from a future, use `use_resource` instead. @@ -45,14 +42,13 @@ where let mut callback = use_callback(move || { let fut = future(); spawn(async move { - flush_sync().await; state.set(UseFutureState::Pending); fut.await; state.set(UseFutureState::Ready); }) }); - // Create the task inside a copyvalue so we can reset it in-place later + // Create the task inside a CopyValue so we can reset it in-place later let task = use_hook(|| CopyValue::new(callback.call())); // Early returns in dioxus have consequences for use_memo, use_resource, and use_future, etc diff --git a/packages/hooks/src/use_memo.rs b/packages/hooks/src/use_memo.rs index d97ed6b20..adbea360f 100644 --- a/packages/hooks/src/use_memo.rs +++ b/packages/hooks/src/use_memo.rs @@ -135,8 +135,6 @@ where spawn(async move { loop { - // Wait for the dom the be finished with sync work - flush_sync().await; rc.changed().await; let new = rc.run_in(|| f(dependencies_signal.read().clone())); diff --git a/packages/signals/src/signal.rs b/packages/signals/src/signal.rs index faa39009b..5b601d8f1 100644 --- a/packages/signals/src/signal.rs +++ b/packages/signals/src/signal.rs @@ -3,7 +3,7 @@ use crate::{ ReadOnlySignal, ReadableRef, }; use dioxus_core::{ - prelude::{flush_sync, spawn, IntoAttributeValue}, + prelude::{spawn, IntoAttributeValue}, ScopeId, }; use generational_box::{AnyStorage, Storage, SyncStorage, UnsyncStorage}; @@ -109,7 +109,6 @@ impl Signal { spawn(async move { loop { - flush_sync().await; rc.changed().await; let new = f(); if new != *state.peek() {