add a way to add manual dependency tuple to memos and effects
This commit is contained in:
1 parent
2bbc609082
commit
0951a389f7
7 files changed
+106
-105
No files matched your search
@@ -1,14 +1,10 @@
|
||||
use crate::dependency::Dependency;
|
||||
use crate::{use_callback, use_signal};
|
||||
use crate::use_callback;
|
||||
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;
|
||||
use dioxus_signals::{Memo, Signal};
|
||||
|
||||
/// Creates a new unsync Selector. The selector will be run immediately and whenever any signal it reads changes.
|
||||
/// Creates a new Memo. The memo will be run immediately and whenever any signal it reads changes.
|
||||
///
|
||||
/// Selectors can be used to efficiently compute derived data from signals.
|
||||
/// Memos can be used to efficiently compute derived data from signals.
|
||||
///
|
||||
/// ```rust
|
||||
/// use dioxus::prelude::*;
|
||||
@@ -29,92 +25,3 @@ pub fn use_memo<R: PartialEq>(f: impl FnMut() -> R + 'static) -> Memo<R> {
|
||||
#[allow(clippy::redundant_closure)]
|
||||
use_hook(|| Signal::memo(move || callback()))
|
||||
}
|
||||
|
||||
/// 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
|
||||
///
|
||||
/// Selectors can be used to efficiently compute derived data from signals.
|
||||
///
|
||||
/// ```rust
|
||||
/// use dioxus::prelude::*;
|
||||
///
|
||||
/// fn App() -> Element {
|
||||
/// 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);
|
||||
///
|
||||
/// rsx! { "{double}" }
|
||||
/// }
|
||||
/// ```
|
||||
#[track_caller]
|
||||
pub fn use_memo_with_dependencies<R: PartialEq, D: Dependency>(
|
||||
dependencies: D,
|
||||
f: impl FnMut(D::Out) -> R + 'static,
|
||||
) -> ReadOnlySignal<R>
|
||||
where
|
||||
D::Out: 'static,
|
||||
{
|
||||
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
|
||||
///
|
||||
/// Selectors can be used to efficiently compute derived data from signals.
|
||||
///
|
||||
/// ```rust
|
||||
/// use dioxus::prelude::*;
|
||||
/// use dioxus_signals::*;
|
||||
///
|
||||
/// fn App() -> Element {
|
||||
/// 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);
|
||||
///
|
||||
/// rsx! { "{double}" }
|
||||
/// }
|
||||
/// ```
|
||||
#[track_caller]
|
||||
pub fn use_maybe_sync_memo_with_dependencies<
|
||||
R: PartialEq,
|
||||
D: Dependency,
|
||||
S: Storage<SignalData<R>>,
|
||||
>(
|
||||
dependencies: D,
|
||||
mut f: impl FnMut(D::Out) -> R + 'static,
|
||||
) -> ReadOnlySignal<R, S>
|
||||
where
|
||||
D::Out: 'static,
|
||||
{
|
||||
let mut dependencies_signal = use_signal(|| dependencies.out());
|
||||
|
||||
let selector = use_hook(|| {
|
||||
// Get the current reactive context
|
||||
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> =
|
||||
rc.run_in(|| Signal::new_maybe_sync(f(dependencies_signal.read().clone())));
|
||||
|
||||
spawn(async move {
|
||||
loop {
|
||||
// Wait for context to change
|
||||
let _ = changed.next().await;
|
||||
|
||||
let new = rc.run_in(|| f(dependencies_signal.read().clone()));
|
||||
if new != *state.peek() {
|
||||
*state.write() = new;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// And just return the readonly variant of that signal
|
||||
ReadOnlySignal::new_maybe_sync(state)
|
||||
});
|
||||
|
||||
// This will cause a re-run of the selector if the dependencies change
|
||||
let changed = { dependencies.changed(&*dependencies_signal.read()) };
|
||||
if changed {
|
||||
dependencies_signal.set(dependencies.out());
|
||||
}
|
||||
|
||||
selector
|
||||
}
|
||||
Reference in new issue
Block a user