switch to use_reactive

This commit is contained in:
Evan Almloff committed 2024-03-15 11:54:09 -05:00
1 parent b8c41efb54
commit df41df75d0
8 files changed
+138 -112

No files matched your search

+3
View File
@@ -69,6 +69,9 @@ pub use use_coroutine::*;
mod use_future;
pub use use_future::*;
mod use_reactive;
pub use use_reactive::*;
// mod use_sorted;
// pub use use_sorted::*;
+23 -35
View File
@@ -2,8 +2,7 @@ use dioxus_core::prelude::*;
use dioxus_signals::ReactiveContext;
use futures_util::StreamExt;
use crate::use_signal;
use dioxus_signals::*;
use crate::use_callback;
/// `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
@@ -22,11 +21,31 @@ use dioxus_signals::*;
/// }
/// }
/// ```
///
/// ## With non-reactive dependencies
/// To add non-reactive dependencies, you can use the `use_reactive` hook.
///
/// Signals will automatically be added as dependencies, so you don't need to call this method for them.
///
/// ```rust
/// # use dioxus::prelude::*;
/// # async fn sleep(delay: u32) {}
///
/// #[component]
/// fn Comp(count: u32) -> Element {
/// // Because the memo subscribes to `count` by adding it as a dependency, the memo will rerun every time `count` changes.
/// use_effect(use_reactive((&count, |(count,)| println!("Manually manipulate the dom") )));
///
/// todo!()
/// }
/// ```
#[track_caller]
pub fn use_effect(mut callback: impl FnMut() + 'static) -> Effect {
pub fn use_effect(callback: impl FnMut() + 'static) -> Effect {
// let mut run_effect = use_hook(|| CopyValue::new(true));
// use_hook_did_run(move |did_run| run_effect.set(did_run));
let callback = use_callback(callback);
let location = std::panic::Location::caller();
use_hook(|| {
@@ -34,7 +53,7 @@ pub fn use_effect(mut callback: impl FnMut() + 'static) -> Effect {
spawn(async move {
loop {
// Run the effect
rc.run_in(&mut callback);
rc.run_in(&*callback);
// Wait for context to change
let _ = changed.next().await;
@@ -54,37 +73,6 @@ pub struct Effect {
}
impl Effect {
/// Adds an explicit dependency to the effect. If the dependency changes, the effect's closure will rerun.
///
/// Signals will automatically be added as dependencies, so you don't need to call this method for them.
///
/// NOTE: You must follow the rules of hooks when calling this method.
///
/// ```rust
/// # use dioxus::prelude::*;
/// # async fn sleep(delay: u32) {}
///
/// #[component]
/// fn Comp(delay: u32) -> Element {
/// // Because the effect subscribes to `delay` by adding it as a dependency, the effect's closure will rerun every time `delay` changes.
/// use_effect(move || {
/// println!("It is safe to manually manipulate the dom here");
/// })
/// .use_dependencies((&delay,));
///
/// todo!()
/// }
/// ```
pub fn use_dependencies(mut self, dependency: impl Dependency) -> Self {
let mut dependencies_signal = use_signal(|| dependency.out());
let changed = { dependency.changed(&*dependencies_signal.read()) };
if changed {
dependencies_signal.set(dependency.out());
self.mark_dirty();
}
self
}
/// Marks the effect as dirty, causing it to rerun on the next render.
pub fn mark_dirty(&mut self) {
self.rc.mark_dirty();
+18
View File
@@ -19,6 +19,24 @@ use dioxus_signals::{Memo, Signal};
/// rsx! { "{double}" }
/// }
/// ```
///
/// ## With non-reactive dependencies
/// To add non-reactive dependencies, you can use the `use_reactive` hook.
///
/// Signals will automatically be added as dependencies, so you don't need to call this method for them.
///
/// ```rust
/// # use dioxus::prelude::*;
/// # async fn sleep(delay: u32) {}
///
/// #[component]
/// fn Comp(count: u32) -> Element {
/// // Because the memo subscribes to `count` by adding it as a dependency, the memo will rerun every time `count` changes.
/// let new_count = use_memo(use_reactive((&count, |(count,)| count + 1)));
///
/// todo!()
/// }
/// ```
#[track_caller]
pub fn use_memo<R: PartialEq>(f: impl FnMut() -> R + 'static) -> Memo<R> {
let callback = use_callback(f);
+125
View File
@@ -0,0 +1,125 @@
use dioxus_signals::{Readable, Writable};
use crate::use_signal;
/// A dependency is a trait that can be used to determine if a effect or selector should be re-run.
pub trait Dependency: Sized + Clone {
/// The output of the dependency
type Out: Clone + PartialEq + 'static;
/// Returns the output of the dependency.
fn out(&self) -> Self::Out;
/// Returns true if the dependency has changed.
fn changed(&self, other: &Self::Out) -> bool {
self.out() != *other
}
}
impl Dependency for () {
type Out = ();
fn out(&self) -> Self::Out {}
}
/// A dependency is a trait that can be used to determine if a effect or selector should be re-run.
pub trait DependencyElement: 'static + PartialEq + Clone {}
impl<T> DependencyElement for T where T: 'static + PartialEq + Clone {}
impl<A: DependencyElement> Dependency for &A {
type Out = A;
fn out(&self) -> Self::Out {
(*self).clone()
}
}
macro_rules! impl_dep {
(
$($el:ident=$name:ident $other:ident,)*
) => {
impl< $($el),* > Dependency for ($(&$el,)*)
where
$(
$el: DependencyElement
),*
{
type Out = ($($el,)*);
fn out(&self) -> Self::Out {
let ($($name,)*) = self;
($((*$name).clone(),)*)
}
fn changed(&self, other: &Self::Out) -> bool {
let ($($name,)*) = self;
let ($($other,)*) = other;
$(
if *$name != $other {
return true;
}
)*
false
}
}
};
}
impl_dep!(A = a1 a2,);
impl_dep!(A = a1 a2, B = b1 b2,);
impl_dep!(A = a1 a2, B = b1 b2, C = c1 c2,);
impl_dep!(A = a1 a2, B = b1 b2, C = c1 c2, D = d1 d2,);
impl_dep!(A = a1 a2, B = b1 b2, C = c1 c2, D = d1 d2, E = e1 e2,);
impl_dep!(A = a1 a2, B = b1 b2, C = c1 c2, D = d1 d2, E = e1 e2, F = f1 f2,);
impl_dep!(A = a1 a2, B = b1 b2, C = c1 c2, D = d1 d2, E = e1 e2, F = f1 f2, G = g1 g2,);
impl_dep!(A = a1 a2, B = b1 b2, C = c1 c2, D = d1 d2, E = e1 e2, F = f1 f2, G = g1 g2, H = h1 h2,);
/// Takes some non-reactive data, and a closure and returns a closure that will subscribe to that non-reactive data as if it were reactive.
///
/// # Example
///
/// ```rust
/// use dioxus::prelude::*;
///
/// let data = 5;
///
/// use_effect(use_reactive((&data,), |(data,)| {
/// println!("Data changed: {}", data);
/// }));
/// ```
pub fn use_reactive<O, D: Dependency>(
non_reactive_data: D,
mut closure: impl FnMut(D::Out) -> O + 'static,
) -> impl FnMut() -> O + 'static {
let mut first_run = false;
let mut last_state = use_signal(|| {
first_run = true;
non_reactive_data.out()
});
if !first_run && non_reactive_data.changed(&*last_state.peek()) {
last_state.set(non_reactive_data.out());
}
move || closure(last_state())
}
/// A helper macro for `use_reactive` that merges uses the closure syntax to elaborate the dependency array
///
/// Takes some non-reactive data, and a closure and returns a closure that will subscribe to that non-reactive data as if it were reactive.
///
/// # Example
///
/// ```rust
/// use dioxus::prelude::*;
///
/// let data = 5;
///
/// use_effect(use_reactive!(|data| {
/// println!("Data changed: {}", data);
/// }));
/// ```
#[macro_export]
macro_rules! use_reactive {
(|| $($rest:tt)*) => { use_reactive( (), move |_| $($rest)* ) };
(| $($args:tt),* | $($rest:tt)*) => {
use_reactive(
($(&$args),*),
move |($($args),*)| $($rest)*
)
};
}
+21 -36
View File
@@ -51,8 +51,26 @@ use std::{cell::Cell, future::Future, rc::Rc};
/// }
///}
/// ```
///
/// ## With non-reactive dependencies
/// To add non-reactive dependencies, you can use the `use_reactive` hook.
///
/// Signals will automatically be added as dependencies, so you don't need to call this method for them.
///
/// ```rust
/// # use dioxus::prelude::*;
/// # async fn sleep(delay: u32) {}
///
/// #[component]
/// fn Comp(count: u32) -> Element {
/// // Because the memo subscribes to `count` by adding it as a dependency, the memo will rerun every time `count` changes.
/// let new_count = use_resource(use_reactive((&count, |(count,)| async move {count + 1} )));
///
/// todo!()
/// }
/// ```
#[must_use = "Consider using `cx.spawn` to run a future without reading its value"]
pub fn use_resource<T, F>(future: impl Fn() -> F + 'static) -> Resource<T>
pub fn use_resource<T, F>(mut future: impl FnMut() -> F + 'static) -> Resource<T>
where
T: 'static,
F: Future<Output = T> + 'static,
@@ -66,10 +84,9 @@ where
let cb = use_callback(move || {
// Create the user's task
#[allow(clippy::redundant_closure)]
let fut = rc.run_in(|| future());
let fut = rc.run_in(&mut future);
// Spawn a wrapper task that polls the innner future and watch its dependencies
// Spawn a wrapper task that polls the inner future and watch its dependencies
spawn(async move {
// move the future here and pin it so we can poll it
let fut = fut;
@@ -144,38 +161,6 @@ pub enum UseResourceState {
}
impl<T> Resource<T> {
/// Adds an explicit dependency to the resource. If the dependency changes, the resource's future will rerun.
///
/// Signals will automatically be added as dependencies, so you don't need to call this method for them.
///
/// NOTE: You must follow the rules of hooks when calling this method.
///
/// ```rust
/// # use dioxus::prelude::*;
/// # async fn sleep(delay: u32) {}
///
/// #[component]
/// fn Comp(delay: u32) -> Element {
/// // Because the resource subscribes to `delay` by adding it as a dependency, the resource's future will rerun every time `delay` changes.
/// let current_weather = use_resource(move || async move {
/// sleep(delay).await;
/// "Sunny"
/// })
/// .use_dependencies((&delay,));
///
/// todo!()
/// }
/// ```
pub fn use_dependencies(mut self, dependency: impl Dependency) -> Self {
let mut dependencies_signal = use_signal(|| dependency.out());
let changed = { dependency.changed(&*dependencies_signal.read()) };
if changed {
dependencies_signal.set(dependency.out());
self.restart();
}
self
}
/// Restart the resource's future.
///
/// Will not cancel the previous future, but will ignore any values that it