remove flume, implement lazier memos

This commit is contained in:
Evan Almloff committed 2024-03-06 11:38:28 -06:00
1 parent 716eb11426
commit 492f0329bf
15 files changed
+267 -177

No files matched your search

+3 -2
View File
@@ -1,5 +1,6 @@
use dioxus_core::prelude::*;
use dioxus_signals::ReactiveContext;
use futures_util::StreamExt;
/// `use_effect` will subscribe to any changes in the signal values it captures
/// effects will always run after first mount and then whenever the signal values change
@@ -26,13 +27,13 @@ pub fn use_effect(mut callback: impl FnMut() + 'static) {
use_hook(|| {
spawn(async move {
let rc = ReactiveContext::new_with_origin(location);
let (rc, mut changed) = ReactiveContext::new_with_origin(location);
loop {
// Run the effect
rc.run_in(&mut callback);
// Wait for context to change
rc.changed().await;
let _ = changed.next().await;
// Wait for the dom the be finished with sync work
wait_for_next_render().await;
+9 -48
View File
@@ -1,8 +1,10 @@
use crate::dependency::Dependency;
use crate::use_signal;
use crate::{use_callback, use_signal};
use dioxus_core::prelude::*;
use dioxus_signals::Memo;
use dioxus_signals::{ReactiveContext, ReadOnlySignal, Readable, Signal, SignalData};
use dioxus_signals::{Storage, Writable};
use futures_util::StreamExt;
/// Creates a new unsync Selector. The selector will be run immediately and whenever any signal it reads changes.
///
@@ -22,51 +24,9 @@ use dioxus_signals::{Storage, Writable};
/// }
/// ```
#[track_caller]
pub fn use_memo<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<R> {
use_maybe_sync_memo(f)
}
/// Creates a new Selector that may be sync. The selector will be run immediately and whenever any signal it reads changes.
///
/// Selectors can be used to efficiently compute derived data from signals.
///
/// ```rust
/// use dioxus::prelude::*;
/// use dioxus_signals::*;
///
/// fn App() -> Element {
/// let mut count = use_signal(|| 0);
/// let double = use_memo(move || count * 2);
/// count += 1;
/// assert_eq!(double(), count * 2);
///
/// rsx! { "{double}" }
/// }
/// ```
#[track_caller]
pub fn use_maybe_sync_memo<R: PartialEq, S: Storage<SignalData<R>>>(
mut f: impl FnMut() -> R + 'static,
) -> ReadOnlySignal<R, S> {
use_hook(|| {
// Create a new reactive context for the memo
let rc = ReactiveContext::new();
// Create a new signal in that context, wiring up its dependencies and subscribers
let mut state: Signal<R, S> = rc.run_in(|| Signal::new_maybe_sync(f()));
spawn(async move {
loop {
rc.changed().await;
let new = rc.run_in(&mut f);
if new != *state.peek() {
*state.write() = new;
}
}
});
// And just return the readonly variant of that signal
ReadOnlySignal::new_maybe_sync(state)
})
pub fn use_memo<R: PartialEq>(f: impl FnMut() -> R + 'static) -> Memo<R> {
let mut callback = use_callback(f);
use_hook(|| Signal::memo(move || callback.call()))
}
/// Creates a new unsync Selector with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
@@ -127,7 +87,7 @@ where
let selector = use_hook(|| {
// Get the current reactive context
let rc = ReactiveContext::new();
let (rc, mut changed) = ReactiveContext::new();
// Create a new signal in that context, wiring up its dependencies and subscribers
let mut state: Signal<R, S> =
@@ -135,7 +95,8 @@ where
spawn(async move {
loop {
rc.changed().await;
// Wait for context to change
let _ = changed.next().await;
let new = rc.run_in(|| f(dependencies_signal.read().clone()));
if new != *state.peek() {
+8 -4
View File
@@ -6,8 +6,8 @@ use dioxus_core::{
Task,
};
use dioxus_signals::*;
use futures_util::{future, pin_mut, FutureExt};
use std::future::Future;
use futures_util::{future, pin_mut, FutureExt, StreamExt};
use std::{cell::Cell, future::Future, rc::Rc};
/// A memo that resolve to a value asynchronously.
/// Unlike `use_future`, `use_resource` runs on the **server**
@@ -44,7 +44,10 @@ where
{
let mut value = use_signal(|| None);
let mut state = use_signal(|| UseResourceState::Pending);
let rc = use_hook(ReactiveContext::new);
let (rc, changed) = use_hook(|| {
let (rc, changed) = ReactiveContext::new();
(rc, Rc::new(Cell::new(Some(changed))))
});
let mut cb = use_callback(move || {
// Create the user's task
@@ -70,10 +73,11 @@ where
let mut task = use_hook(|| Signal::new(cb.call()));
use_hook(|| {
let mut changed = changed.take().unwrap();
spawn(async move {
loop {
// Wait for the dependencies to change
rc.changed().await;
let _ = changed.next().await;
// Stop the old task
task.write().cancel();