only subscribe scopes to signals when rendering

This commit is contained in:
Evan Almloff committed 2024-02-14 09:33:22 -06:00
1 parent 49f3b8235d
commit 48751d2f98
14 files changed
+424 -48

No files matched your search

+4 -4
View File
@@ -11,17 +11,17 @@
/// ```
/// # use dioxus::prelude::*;
/// #
/// # #[derive(Props, PartialEq)]
/// # #[derive(Props, PartialEq, Clone)]
/// # struct Props {
/// # prop: String,
/// # }
/// # fn Component(cx: Scope<Props>) -> Element {
/// # fn Component(props: Props) -> Element {
///
/// let (data) = use_signal(|| {});
///
/// let handle_thing = move |_| {
/// to_owned![data, cx.props.prop];
/// cx.spawn(async move {
/// to_owned![data, props.prop];
/// spawn(async move {
/// // do stuff
/// });
/// };
+13 -13
View File
@@ -16,7 +16,7 @@ use dioxus_signals::{Storage, Writable};
/// let mut count = use_signal(|| 0);
/// let double = use_memo(move || count * 2);
/// count += 1;
/// assert_eq!(double.value(), count * 2);
/// assert_eq!(double(), count * 2);
///
/// rsx! { "{double}" }
/// }
@@ -35,12 +35,12 @@ pub fn use_memo<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<
/// use dioxus_signals::*;
///
/// fn App() -> Element {
/// let mut count = use_signal(cx, || 0);
/// let double = use_memo(cx, move || count * 2);
/// let mut count = use_signal(|| 0);
/// let double = use_memo(move || count * 2);
/// count += 1;
/// assert_eq!(double.value(), count * 2);
/// assert_eq!(double(), count * 2);
///
/// render! { "{double}" }
/// rsx! { "{double}" }
/// }
/// ```
#[track_caller]
@@ -79,11 +79,11 @@ pub fn use_maybe_sync_memo<R: PartialEq, S: Storage<SignalData<R>>>(
/// use dioxus::prelude::*;
///
/// fn App() -> Element {
/// let mut local_state = use_state(|| 0);
/// let double = use_memo_with_dependencies(cx, (local_state.get(),), move |(local_state,)| local_state * 2);
/// let mut local_state = use_signal(|| 0);
/// let double = use_memo_with_dependencies((&local_state(),), move |(local_state,)| local_state * 2);
/// local_state.set(1);
///
/// render! { "{double}" }
/// rsx! { "{double}" }
/// }
/// ```
#[track_caller]
@@ -94,7 +94,7 @@ pub fn use_memo_with_dependencies<R: PartialEq, D: Dependency>(
where
D::Out: 'static,
{
use_maybe_sync_selector_with_dependencies(dependencies, f)
use_maybe_sync_memo_with_dependencies(dependencies, f)
}
/// Creates a new Selector that may be sync with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
@@ -106,15 +106,15 @@ where
/// use dioxus_signals::*;
///
/// fn App() -> Element {
/// let mut local_state = use_state(|| 0);
/// let double = use_memo_with_dependencies(cx, (local_state.get(),), move |(local_state,)| local_state * 2);
/// let mut local_state = use_signal(|| 0i32);
/// let double: ReadOnlySignal<i32, SyncStorage> = use_maybe_sync_memo_with_dependencies((&local_state(),), move |(local_state,)| local_state * 2);
/// local_state.set(1);
///
/// render! { "{double}" }
/// rsx! { "{double}" }
/// }
/// ```
#[track_caller]
pub fn use_maybe_sync_selector_with_dependencies<
pub fn use_maybe_sync_memo_with_dependencies<
R: PartialEq,
D: Dependency,
S: Storage<SignalData<R>>,
+7 -11
View File
@@ -17,9 +17,7 @@ use dioxus_signals::{Signal, SignalData, Storage, SyncStorage, UnsyncStorage};
///
/// #[component]
/// fn Child(state: Signal<u32>) -> Element {
/// let state = *state;
///
/// use_future( |()| async move {
/// use_future(move || async move {
/// // Because the signal is a Copy type, we can use it in an async block without cloning it.
/// *state.write() += 1;
/// });
@@ -44,19 +42,17 @@ pub fn use_signal<T: 'static>(f: impl FnOnce() -> T) -> Signal<T, UnsyncStorage>
/// use dioxus::prelude::*;
/// use dioxus_signals::*;
///
/// fn App(cx: Scope) -> Element {
/// let mut count = use_signal_sync(cx, || 0);
/// fn App() -> Element {
/// let mut count = use_signal_sync(|| 0);
///
/// // Because signals have automatic dependency tracking, if you never read them in a component, that component will not be re-rended when the signal is updated.
/// // The app component will never be rerendered in this example.
/// render! { Child { state: count } }
/// rsx! { Child { state: count } }
/// }
///
/// #[component]
/// fn Child(cx: Scope, state: Signal<u32, SyncStorage>) -> Element {
/// let state = *state;
///
/// use_future!(cx, |()| async move {
/// fn Child(state: Signal<u32, SyncStorage>) -> Element {
/// use_future(move || async move {
/// // This signal is Send + Sync, so we can use it in an another thread
/// tokio::spawn(async move {
/// // Because the signal is a Copy type, we can use it in an async block without cloning it.
@@ -64,7 +60,7 @@ pub fn use_signal<T: 'static>(f: impl FnOnce() -> T) -> Signal<T, UnsyncStorage>
/// }).await;
/// });
///
/// render! {
/// rsx! {
/// button {
/// onclick: move |_| *state.write() += 1,
/// "{state}"