Struct reactive_graph::computed::AsyncDerived
source · pub struct AsyncDerived<T, S = SyncStorage> { /* private fields */ }
Expand description
A reactive value that is derived by running an asynchronous computation in response to changes in its sources.
When one of its dependencies changes, this will re-run its async computation, then notify other values that depend on it that it has changed.
This is an arena-allocated type, which is Copy
and is disposed when its reactive
Owner
cleans up. For a reference-counted signal that livesas
as long as a reference to it is alive, see ArcAsyncDerived
.
§Examples
let signal1 = RwSignal::new(0);
let signal2 = RwSignal::new(0);
let derived = AsyncDerived::new(move || async move {
// reactive values can be tracked anywhere in the `async` block
let value1 = signal1.get();
tokio::time::sleep(std::time::Duration::from_millis(25)).await;
let value2 = signal2.get();
value1 + value2
});
// the value can be accessed synchronously as `Option<T>`
assert_eq!(derived.get(), None);
// we can also .await the value, i.e., convert it into a Future
assert_eq!(derived.await, 0);
assert_eq!(derived.get(), Some(0));
signal1.set(1);
// while the new value is still pending, the signal holds the old value
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
assert_eq!(derived.get(), Some(0));
// setting multiple dependencies will hold until the latest change is ready
signal2.set(1);
assert_eq!(derived.await, 2);
§Core Trait Implementations
.get()
clones the current value as anOption<T>
. If you call it within an effect, it will cause that effect to subscribe to the memo, and to re-run whenever the value of the memo changes..get_untracked()
clones the value of without reactively tracking it.
.read()
returns a guard that allows accessing the value by reference. If you call it within an effect, it will cause that effect to subscribe to the memo, and to re-run whenever the value changes..read_untracked()
gives access to the current value without reactively tracking it.
.with()
allows you to reactively access the value without cloning by applying a callback function..with_untracked()
allows you to access the value by applying a callback function without reactively tracking it.
IntoFuture
allows you to create aFuture
that resolves when this resource is done loading.
Implementations§
source§impl<T> AsyncDerived<T>where
T: 'static,
impl<T> AsyncDerived<T>where
T: 'static,
sourcepub fn new<Fut>(fun: impl Fn() -> Fut + Send + Sync + 'static) -> Self
pub fn new<Fut>(fun: impl Fn() -> Fut + Send + Sync + 'static) -> Self
Creates a new async derived computation.
This runs eagerly: i.e., calls fun
once when created and immediately spawns the Future
as a new task.
source§impl<T> AsyncDerived<T, LocalStorage>where
T: 'static,
impl<T> AsyncDerived<T, LocalStorage>where
T: 'static,
sourcepub fn new_unsync<Fut>(fun: impl Fn() -> Fut + 'static) -> Selfwhere
T: 'static,
Fut: Future<Output = T> + 'static,
pub fn new_unsync<Fut>(fun: impl Fn() -> Fut + 'static) -> Selfwhere
T: 'static,
Fut: Future<Output = T> + 'static,
Creates a new async derived computation that will be guaranteed to run on the current thread.
This runs eagerly: i.e., calls fun
once when created and immediately spawns the Future
as a new task.
sourcepub fn new_unsync_with_initial<Fut>(
initial_value: Option<T>,
fun: impl Fn() -> Fut + 'static,
) -> Selfwhere
T: 'static,
Fut: Future<Output = T> + 'static,
pub fn new_unsync_with_initial<Fut>(
initial_value: Option<T>,
fun: impl Fn() -> Fut + 'static,
) -> Selfwhere
T: 'static,
Fut: Future<Output = T> + 'static,
Creates a new async derived computation with an initial value. Async work will be guaranteed to run only on the current thread.
If the initial value is Some(_)
, the task will not be run initially.
source§impl<T, S> AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
sourcepub fn ready(&self) -> AsyncDerivedReadyFuture ⓘ
pub fn ready(&self) -> AsyncDerivedReadyFuture ⓘ
Returns a Future
that is ready when this resource has next finished loading.
source§impl<T, S> AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
sourcepub fn by_ref(&self) -> AsyncDerivedRefFuture<T> ⓘ
pub fn by_ref(&self) -> AsyncDerivedRefFuture<T> ⓘ
Returns a Future
that resolves when the computation is finished, and accesses the inner
value by reference rather than by cloning it.
Trait Implementations§
source§impl<T, S> Clone for AsyncDerived<T, S>
impl<T, S> Clone for AsyncDerived<T, S>
source§impl<T, S> Debug for AsyncDerived<T, S>where
S: Debug,
impl<T, S> Debug for AsyncDerived<T, S>where
S: Debug,
source§impl<T, S> DefinedAt for AsyncDerived<T, S>
impl<T, S> DefinedAt for AsyncDerived<T, S>
source§fn defined_at(&self) -> Option<&'static Location<'static>>
fn defined_at(&self) -> Option<&'static Location<'static>>
None
in
release mode.source§impl<T, S> Dispose for AsyncDerived<T, S>
impl<T, S> Dispose for AsyncDerived<T, S>
source§impl<T> From<ArcAsyncDerived<T>> for AsyncDerived<T>
impl<T> From<ArcAsyncDerived<T>> for AsyncDerived<T>
source§fn from(value: ArcAsyncDerived<T>) -> Self
fn from(value: ArcAsyncDerived<T>) -> Self
source§impl<T> FromLocal<ArcAsyncDerived<T>> for AsyncDerived<T, LocalStorage>where
T: 'static,
impl<T> FromLocal<ArcAsyncDerived<T>> for AsyncDerived<T, LocalStorage>where
T: 'static,
source§fn from_local(value: ArcAsyncDerived<T>) -> Self
fn from_local(value: ArcAsyncDerived<T>) -> Self
source§impl<T, S> IntoFuture for AsyncDerived<T, S>
impl<T, S> IntoFuture for AsyncDerived<T, S>
§type IntoFuture = AsyncDerivedFuture<T>
type IntoFuture = AsyncDerivedFuture<T>
source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
source§impl<T, S> ReactiveNode for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> ReactiveNode for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
source§fn mark_dirty(&self)
fn mark_dirty(&self)
source§fn mark_check(&self)
fn mark_check(&self)
source§fn mark_subscribers_check(&self)
fn mark_subscribers_check(&self)
source§fn update_if_necessary(&self) -> bool
fn update_if_necessary(&self) -> bool
source§impl<T, S> ReadUntracked for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> ReadUntracked for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
§type Value = ReadGuard<Option<T>, AsyncPlain<Option<T>>>
type Value = ReadGuard<Option<T>, AsyncPlain<Option<T>>>
source§fn try_read_untracked(&self) -> Option<Self::Value>
fn try_read_untracked(&self) -> Option<Self::Value>
None
if the signal has already been disposed.source§fn read_untracked(&self) -> Self::Value
fn read_untracked(&self) -> Self::Value
source§impl<T, S> Source for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> Source for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
source§fn add_subscriber(&self, subscriber: AnySubscriber)
fn add_subscriber(&self, subscriber: AnySubscriber)
source§fn remove_subscriber(&self, subscriber: &AnySubscriber)
fn remove_subscriber(&self, subscriber: &AnySubscriber)
source§fn clear_subscribers(&self)
fn clear_subscribers(&self)
source§impl<T, S> Subscriber for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> Subscriber for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
source§fn add_source(&self, source: AnySource)
fn add_source(&self, source: AnySource)
source§fn clear_sources(&self, subscriber: &AnySubscriber)
fn clear_sources(&self, subscriber: &AnySubscriber)
source§impl<T, S> ToAnySource for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> ToAnySource for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
source§fn to_any_source(&self) -> AnySource
fn to_any_source(&self) -> AnySource
source§impl<T, S> ToAnySubscriber for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> ToAnySubscriber for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
source§fn to_any_subscriber(&self) -> AnySubscriber
fn to_any_subscriber(&self) -> AnySubscriber
impl<T, S> Copy for AsyncDerived<T, S>
Auto Trait Implementations§
impl<T, S> Freeze for AsyncDerived<T, S>
impl<T, S = SyncStorage> !RefUnwindSafe for AsyncDerived<T, S>
impl<T, S> Send for AsyncDerived<T, S>where
S: Send,
impl<T, S> Sync for AsyncDerived<T, S>where
S: Sync,
impl<T, S> Unpin for AsyncDerived<T, S>where
S: Unpin,
impl<T, S = SyncStorage> !UnwindSafe for AsyncDerived<T, S>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Read for Twhere
T: Track + ReadUntracked,
impl<T> Read for Twhere
T: Track + ReadUntracked,
source§impl<T> With for Twhere
T: WithUntracked + Track,
impl<T> With for Twhere
T: WithUntracked + Track,
§type Value = <T as WithUntracked>::Value
type Value = <T as WithUntracked>::Value
source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
source§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
source§impl<T> WithUntracked for Twhere
T: DefinedAt + ReadUntracked,
impl<T> WithUntracked for Twhere
T: DefinedAt + ReadUntracked,
§type Value = <<T as ReadUntracked>::Value as Deref>::Target
type Value = <<T as ReadUntracked>::Value as Deref>::Target
source§fn try_with_untracked<U>(
&self,
fun: impl FnOnce(&<T as WithUntracked>::Value) -> U,
) -> Option<U>
fn try_with_untracked<U>( &self, fun: impl FnOnce(&<T as WithUntracked>::Value) -> U, ) -> Option<U>
None
if the signal has already been disposed.