Skip to content

Commit

Permalink
rename flush_sync to wait_for_next_render
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmloff committed Mar 5, 2024
1 parent 2e3a730 commit 287416c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 24 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/global_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
4 changes: 2 additions & 2 deletions packages/core/tests/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks/src/use_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
});
Expand Down
10 changes: 3 additions & 7 deletions packages/hooks/src/use_future.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions packages/hooks/src/use_memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
3 changes: 1 addition & 2 deletions packages/signals/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -109,7 +109,6 @@ impl<T: PartialEq + 'static> Signal<T> {

spawn(async move {
loop {
flush_sync().await;
rc.changed().await;
let new = f();
if new != *state.peek() {
Expand Down

0 comments on commit 287416c

Please sign in to comment.