Use futures_util mutex instead of flume for sending flush lock wakeups
This commit is contained in:
14 files changed
+100
-43
No files matched your search
Generated
-1
@@ -2476,7 +2476,6 @@ version = "0.4.3"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"dioxus",
|
"dioxus",
|
||||||
"dioxus-ssr",
|
"dioxus-ssr",
|
||||||
"flume",
|
|
||||||
"futures-channel",
|
"futures-channel",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"longest-increasing-subsequence",
|
"longest-increasing-subsequence",
|
||||||
|
|||||||
+2
-2
@@ -7,7 +7,7 @@ fn main() {
|
|||||||
|
|
||||||
fn app() -> Element {
|
fn app() -> Element {
|
||||||
let mut breed = use_signal(|| "deerhound".to_string());
|
let mut breed = use_signal(|| "deerhound".to_string());
|
||||||
let breed_list = use_async_memo(move || async move {
|
let breed_list = use_resource(move || async move {
|
||||||
let list = reqwest::get("https://dog.ceo/api/breeds/list/all")
|
let list = reqwest::get("https://dog.ceo/api/breeds/list/all")
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
@@ -44,7 +44,7 @@ fn app() -> Element {
|
|||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
fn BreedPic(breed: Signal<String>) -> Element {
|
fn BreedPic(breed: Signal<String>) -> Element {
|
||||||
let fut = use_async_memo(move || async move {
|
let fut = use_resource(move || async move {
|
||||||
reqwest::get(format!("https://dog.ceo/api/breed/{breed}/images/random"))
|
reqwest::get(format!("https://dog.ceo/api/breed/{breed}/images/random"))
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn app() -> Element {
|
fn app() -> Element {
|
||||||
let future = use_async_memo(move || async move {
|
let future = use_resource(move || async move {
|
||||||
let mut eval = eval(
|
let mut eval = eval(
|
||||||
r#"
|
r#"
|
||||||
dioxus.send("Hi from JS!");
|
dioxus.send("Hi from JS!");
|
||||||
|
|||||||
+3
-6
@@ -21,10 +21,7 @@ fn app() -> Element {
|
|||||||
if count() > 30 {
|
if count() > 30 {
|
||||||
return rsx! {
|
return rsx! {
|
||||||
h1 { "Count is too high!" }
|
h1 { "Count is too high!" }
|
||||||
button {
|
button { onclick: move |_| count.set(0), "Press to reset" }
|
||||||
onclick: move |_| count.set(0),
|
|
||||||
"Press to reset"
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,8 +35,8 @@ fn app() -> Element {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// use_resource will spawn a future that resolves to a value - essentially an async memo
|
// use_resource will spawn a future that resolves to a value
|
||||||
let _slow_count = use_async_memo(move || async move {
|
let _slow_count = use_resource(move || async move {
|
||||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||||
count() * 2
|
count() * 2
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -20,8 +20,6 @@ slab = { workspace = true }
|
|||||||
futures-channel = { workspace = true }
|
futures-channel = { workspace = true }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
serde = { version = "1", features = ["derive"], optional = true }
|
serde = { version = "1", features = ["derive"], optional = true }
|
||||||
flume = { version = "0.11.0", default-features = false, features = ["async"]}
|
|
||||||
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { workspace = true, features = ["full"] }
|
tokio = { workspace = true, features = ["full"] }
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::{runtime::Runtime, Element, ScopeId, Task};
|
use crate::{runtime::Runtime, Element, ScopeId, Task};
|
||||||
use futures_util::{future::poll_fn, Future};
|
use futures_util::Future;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
/// Get the current scope id
|
/// Get the current scope id
|
||||||
@@ -257,13 +257,11 @@ pub fn after_render(f: impl FnMut() + 'static) {
|
|||||||
/// 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 flush_sync effects
|
||||||
/// are run immediately before diffing the DOM, which causes all sorts of out-of-sync weirdness.
|
/// are run immediately before diffing the DOM, which causes all sorts of out-of-sync weirdness.
|
||||||
pub async fn flush_sync() {
|
pub async fn flush_sync() {
|
||||||
// if flushing() {
|
// Wait for the flush lock to be available
|
||||||
// return;
|
// We release it immediately, so it's impossible for the lock to be held longer than this function
|
||||||
// }
|
Runtime::with(|rt| rt.flush_mutex.clone())
|
||||||
|
|
||||||
_ = Runtime::with(|rt| rt.flush.clone())
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.recv_async()
|
.lock()
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
use rustc_hash::FxHashSet;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
innerlude::{LocalTask, SchedulerMsg},
|
innerlude::{LocalTask, SchedulerMsg},
|
||||||
scope_context::Scope,
|
scope_context::Scope,
|
||||||
@@ -9,6 +7,7 @@ use crate::{
|
|||||||
use std::{
|
use std::{
|
||||||
cell::{Cell, Ref, RefCell},
|
cell::{Cell, Ref, RefCell},
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
@@ -32,18 +31,18 @@ pub struct Runtime {
|
|||||||
|
|
||||||
pub(crate) sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
|
pub(crate) sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
|
||||||
|
|
||||||
// Tasks waiting to be manually resumed when we call wait_for_work
|
// the virtualdom will hold this lock while it's doing syncronous work
|
||||||
pub(crate) flush: flume::Receiver<()>,
|
// when the lock is lifted, tasks waiting for the lock will be able to run
|
||||||
|
pub(crate) flush_mutex: Arc<futures_util::lock::Mutex<()>>,
|
||||||
|
pub(crate) flush_lock: Cell<Option<futures_util::lock::OwnedMutexGuard<()>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Runtime {
|
impl Runtime {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>) -> Rc<Self> {
|
||||||
sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
|
|
||||||
flush: flume::Receiver<()>,
|
|
||||||
) -> Rc<Self> {
|
|
||||||
Rc::new(Self {
|
Rc::new(Self {
|
||||||
sender,
|
sender,
|
||||||
flush,
|
flush_mutex: Default::default(),
|
||||||
|
flush_lock: Default::default(),
|
||||||
rendering: Cell::new(true),
|
rendering: Cell::new(true),
|
||||||
scope_states: Default::default(),
|
scope_states: Default::default(),
|
||||||
scope_stack: Default::default(),
|
scope_stack: Default::default(),
|
||||||
@@ -150,6 +149,23 @@ impl Runtime {
|
|||||||
pub(crate) fn with_scope<R>(scope: ScopeId, f: impl FnOnce(&Scope) -> R) -> Option<R> {
|
pub(crate) fn with_scope<R>(scope: ScopeId, f: impl FnOnce(&Scope) -> R) -> Option<R> {
|
||||||
Self::with(|rt| rt.get_state(scope).map(|sc| f(&sc))).flatten()
|
Self::with(|rt| rt.get_state(scope).map(|sc| f(&sc))).flatten()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Acquire the flush lock and store it interally
|
||||||
|
///
|
||||||
|
/// This means the virtual dom is currently doing syncronous work
|
||||||
|
/// The lock will be held until `release_flush_lock` is called - and then the OwnedLock will be dropped
|
||||||
|
pub(crate) fn acquire_flush_lock(&self) {
|
||||||
|
self.flush_lock
|
||||||
|
.set(Some(self.flush_mutex.try_lock_owned().unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Release the flush lock
|
||||||
|
///
|
||||||
|
/// On the drop of the flush lock, all tasks waiting on `flush_sync` will spring to life via their wakers.
|
||||||
|
/// You can now freely poll those tasks and they can progress
|
||||||
|
pub(crate) fn release_flush_lock(&self) {
|
||||||
|
self.flush_lock.take();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A guard for a new runtime. This must be used to override the current runtime when importing components from a dynamic library that has it's own runtime.
|
/// A guard for a new runtime. This must be used to override the current runtime when importing components from a dynamic library that has it's own runtime.
|
||||||
|
|||||||
@@ -202,7 +202,6 @@ pub struct VirtualDom {
|
|||||||
pub(crate) suspended_scopes: FxHashSet<ScopeId>,
|
pub(crate) suspended_scopes: FxHashSet<ScopeId>,
|
||||||
|
|
||||||
rx: futures_channel::mpsc::UnboundedReceiver<SchedulerMsg>,
|
rx: futures_channel::mpsc::UnboundedReceiver<SchedulerMsg>,
|
||||||
flush_tx: flume::Sender<()>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VirtualDom {
|
impl VirtualDom {
|
||||||
@@ -306,12 +305,10 @@ impl VirtualDom {
|
|||||||
/// ```
|
/// ```
|
||||||
pub(crate) fn new_with_component(root: impl AnyProps + 'static) -> Self {
|
pub(crate) fn new_with_component(root: impl AnyProps + 'static) -> Self {
|
||||||
let (tx, rx) = futures_channel::mpsc::unbounded();
|
let (tx, rx) = futures_channel::mpsc::unbounded();
|
||||||
let (flush_tx, flush_rx) = flume::unbounded(); // I don't think this needs to be unbounded
|
|
||||||
|
|
||||||
let mut dom = Self {
|
let mut dom = Self {
|
||||||
rx,
|
rx,
|
||||||
flush_tx,
|
runtime: Runtime::new(tx),
|
||||||
runtime: Runtime::new(tx, flush_rx),
|
|
||||||
scopes: Default::default(),
|
scopes: Default::default(),
|
||||||
dirty_scopes: Default::default(),
|
dirty_scopes: Default::default(),
|
||||||
templates: Default::default(),
|
templates: Default::default(),
|
||||||
@@ -430,12 +427,13 @@ impl VirtualDom {
|
|||||||
self.poll_tasks().await;
|
self.poll_tasks().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Poll futures without progressing any futures from the flush table
|
///
|
||||||
async fn poll_tasks(&mut self) {
|
async fn poll_tasks(&mut self) {
|
||||||
loop {
|
// Release the flush lock
|
||||||
// Send the flush signal to the runtime, waking up tasks
|
// This will cause all the flush wakers to immediately spring to life, which we will off with process_events
|
||||||
_ = self.flush_tx.try_send(());
|
self.runtime.release_flush_lock();
|
||||||
|
|
||||||
|
loop {
|
||||||
// Process all events - Scopes are marked dirty, etc
|
// Process all events - Scopes are marked dirty, etc
|
||||||
// Sometimes when wakers fire we get a slew of updates at once, so its important that we drain this completely
|
// Sometimes when wakers fire we get a slew of updates at once, so its important that we drain this completely
|
||||||
self.process_events();
|
self.process_events();
|
||||||
@@ -448,6 +446,10 @@ impl VirtualDom {
|
|||||||
// Make sure we set the runtime since we're running user code
|
// Make sure we set the runtime since we're running user code
|
||||||
let _runtime = RuntimeGuard::new(self.runtime.clone());
|
let _runtime = RuntimeGuard::new(self.runtime.clone());
|
||||||
|
|
||||||
|
// Hold a lock to the flush sync to prevent tasks from running in the event we get an immediate
|
||||||
|
// When we're doing awaiting the rx, the lock will be dropped and tasks waiting on the lock will get waked
|
||||||
|
self.runtime.acquire_flush_lock();
|
||||||
|
|
||||||
match self.rx.next().await.expect("channel should never close") {
|
match self.rx.next().await.expect("channel should never close") {
|
||||||
SchedulerMsg::Immediate(id) => self.mark_dirty(id),
|
SchedulerMsg::Immediate(id) => self.mark_dirty(id),
|
||||||
SchedulerMsg::TaskNotified(id) => self.runtime.handle_task_wakeup(id),
|
SchedulerMsg::TaskNotified(id) => self.runtime.handle_task_wakeup(id),
|
||||||
@@ -546,6 +548,7 @@ impl VirtualDom {
|
|||||||
|
|
||||||
// Process any events that might be pending in the queue
|
// Process any events that might be pending in the queue
|
||||||
// Signals marked with .write() need a chance to be handled by the effect driver
|
// Signals marked with .write() need a chance to be handled by the effect driver
|
||||||
|
// This also processes futures which might progress into immediates
|
||||||
self.process_events();
|
self.process_events();
|
||||||
|
|
||||||
// Next, diff any dirty scopes
|
// Next, diff any dirty scopes
|
||||||
|
|||||||
@@ -21,6 +21,26 @@ use element::DioxusTUIMutationWriter;
|
|||||||
pub use plasmo::{query::Query, Config, RenderingMode, Size, TuiContext};
|
pub use plasmo::{query::Query, Config, RenderingMode, Size, TuiContext};
|
||||||
use plasmo::{render, Driver};
|
use plasmo::{render, Driver};
|
||||||
|
|
||||||
|
pub mod launch {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
pub type Config = super::Config;
|
||||||
|
/// Launches the WebView and runs the event loop, with configuration and root props.
|
||||||
|
pub fn launch(
|
||||||
|
root: fn() -> Element,
|
||||||
|
contexts: Vec<Box<dyn Fn() -> Box<dyn Any>>>,
|
||||||
|
platform_config: Config,
|
||||||
|
) {
|
||||||
|
let mut virtual_dom = VirtualDom::new(root);
|
||||||
|
|
||||||
|
for context in contexts {
|
||||||
|
virtual_dom.insert_any_root_context(context());
|
||||||
|
}
|
||||||
|
|
||||||
|
launch_vdom_cfg(virtual_dom, platform_config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn launch(app: fn() -> Element) {
|
pub fn launch(app: fn() -> Element) {
|
||||||
launch_cfg(app, Config::default())
|
launch_cfg(app, Config::default())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,16 @@ impl LaunchBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "tui")]
|
||||||
|
/// Launch your tui application
|
||||||
|
pub fn tui() -> LaunchBuilder<dioxus_tui::Config, UnsendContext> {
|
||||||
|
LaunchBuilder {
|
||||||
|
launch_fn: dioxus_tui::launch::launch,
|
||||||
|
contexts: Vec::new(),
|
||||||
|
platform_config: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Provide a custom launch function for your application.
|
/// Provide a custom launch function for your application.
|
||||||
///
|
///
|
||||||
/// Useful for third party renderers to tap into the launch builder API without having to reimplement it.
|
/// Useful for third party renderers to tap into the launch builder API without having to reimplement it.
|
||||||
@@ -215,3 +225,9 @@ pub fn launch_desktop(app: fn() -> Element) {
|
|||||||
pub fn launch_fullstack(app: fn() -> Element) {
|
pub fn launch_fullstack(app: fn() -> Element) {
|
||||||
LaunchBuilder::fullstack().launch(app)
|
LaunchBuilder::fullstack().launch(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "tui")]
|
||||||
|
/// Launch your tui application without any additional configuration. See [`LaunchBuilder`] for more options.
|
||||||
|
pub fn launch_tui(app: fn() -> Element) {
|
||||||
|
LaunchBuilder::tui().launch(app)
|
||||||
|
}
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
use crate::use_hook_did_run;
|
|
||||||
use dioxus_core::prelude::*;
|
use dioxus_core::prelude::*;
|
||||||
use dioxus_signals::{CopyValue, Effect, ReactiveContext, Writable};
|
use dioxus_signals::ReactiveContext;
|
||||||
// use futures_util::select;
|
|
||||||
|
|
||||||
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
|
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
|
||||||
/// The signal will be owned by the current component and will be dropped when the component is dropped.
|
/// The signal will be owned by the current component and will be dropped when the component is dropped.
|
||||||
@@ -16,6 +14,9 @@ pub fn use_effect(mut callback: impl FnMut() + 'static) {
|
|||||||
let rc = ReactiveContext::new(None);
|
let rc = ReactiveContext::new(None);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
// Wait for the dom the be finished with sync work
|
||||||
|
flush_sync().await;
|
||||||
|
|
||||||
// Run the effect
|
// Run the effect
|
||||||
rc.run_in(|| callback());
|
rc.run_in(|| callback());
|
||||||
|
|
||||||
|
|||||||
@@ -56,6 +56,9 @@ pub fn use_maybe_sync_memo<R: PartialEq, S: Storage<SignalData<R>>>(
|
|||||||
|
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
|
// Wait for the dom the be finished with sync work
|
||||||
|
flush_sync().await;
|
||||||
|
|
||||||
rc.changed().await;
|
rc.changed().await;
|
||||||
let new = rc.run_in(|| f());
|
let new = rc.run_in(|| f());
|
||||||
if new != *state.peek() {
|
if new != *state.peek() {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use crate::{use_callback, use_signal};
|
use crate::{use_callback, use_signal};
|
||||||
use dioxus_core::{
|
use dioxus_core::{
|
||||||
prelude::{spawn, suspend, use_hook},
|
prelude::{flush_sync, spawn, suspend, use_hook},
|
||||||
Task,
|
Task,
|
||||||
};
|
};
|
||||||
use dioxus_signals::*;
|
use dioxus_signals::*;
|
||||||
@@ -13,7 +13,7 @@ use std::future::Future;
|
|||||||
///
|
///
|
||||||
/// Regular memos are synchronous and resolve immediately. However, you might want to resolve a memo
|
/// Regular memos are synchronous and resolve immediately. However, you might want to resolve a memo
|
||||||
#[must_use = "Consider using `cx.spawn` to run a future without reading its value"]
|
#[must_use = "Consider using `cx.spawn` to run a future without reading its value"]
|
||||||
pub fn use_async_memo<T, F>(future: impl Fn() -> F + 'static) -> AsyncMemo<T>
|
pub fn use_resource<T, F>(future: impl Fn() -> F + 'static) -> AsyncMemo<T>
|
||||||
where
|
where
|
||||||
T: 'static,
|
T: 'static,
|
||||||
F: Future<Output = T> + 'static,
|
F: Future<Output = T> + 'static,
|
||||||
@@ -28,6 +28,9 @@ where
|
|||||||
|
|
||||||
// Spawn a wrapper task that polls the innner future and watch its dependencies
|
// Spawn a wrapper task that polls the innner future and watch its dependencies
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
|
// Wait for the dom the be finished with sync work
|
||||||
|
flush_sync().await;
|
||||||
|
|
||||||
// move the future here and pin it so we can poll it
|
// move the future here and pin it so we can poll it
|
||||||
let fut = fut;
|
let fut = fut;
|
||||||
pin_mut!(fut);
|
pin_mut!(fut);
|
||||||
@@ -88,7 +91,9 @@ impl<T> AsyncMemo<T> {
|
|||||||
|
|
||||||
// Manually set the value in the future slot without starting the future over
|
// Manually set the value in the future slot without starting the future over
|
||||||
pub fn set(&mut self, new_value: T) {
|
pub fn set(&mut self, new_value: T) {
|
||||||
todo!()
|
// if let Some(task) = self.task.take() {
|
||||||
|
// cx.remove_future(task);
|
||||||
|
// }
|
||||||
// self.value.set(Some(new_value));
|
// self.value.set(Some(new_value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ impl TuiContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn quit(&self) {
|
pub fn quit(&self) {
|
||||||
|
// panic!("ack")
|
||||||
self.tx.unbounded_send(InputEvent::Close).unwrap();
|
self.tx.unbounded_send(InputEvent::Close).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,7 +259,7 @@ pub fn render<R: Driver>(
|
|||||||
TermEvent::Resize(_, _) => updated = true,
|
TermEvent::Resize(_, _) => updated = true,
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
InputEvent::Close => break,
|
InputEvent::Close => panic!("ackkkk"),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let InputEvent::UserInput(evt) = evt.unwrap() {
|
if let InputEvent::UserInput(evt) = evt.unwrap() {
|
||||||
|
|||||||
Reference in new issue
Block a user