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,
) -> AsyncDerived<T>
pub fn new<Fut>( fun: impl Fn() -> Fut + Send + Sync + 'static, ) -> AsyncDerived<T>
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.
Sourcepub fn new_with_initial<Fut>(
initial_value: Option<T>,
fun: impl Fn() -> Fut + Send + Sync + 'static,
) -> AsyncDerived<T>
pub fn new_with_initial<Fut>( initial_value: Option<T>, fun: impl Fn() -> Fut + Send + Sync + 'static, ) -> AsyncDerived<T>
Creates a new async derived computation with an initial value.
If the initial value is Some(_)
, the task will not be run initially.
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,
) -> AsyncDerived<T, LocalStorage>where
T: 'static,
Fut: Future<Output = T> + 'static,
pub fn new_unsync<Fut>(
fun: impl Fn() -> Fut + 'static,
) -> AsyncDerived<T, LocalStorage>where
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,
) -> AsyncDerived<T, LocalStorage>where
T: 'static,
Fut: Future<Output = T> + 'static,
pub fn new_unsync_with_initial<Fut>(
initial_value: Option<T>,
fun: impl Fn() -> Fut + 'static,
) -> AsyncDerived<T, LocalStorage>where
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§fn clone(&self) -> AsyncDerived<T, S>
fn clone(&self) -> AsyncDerived<T, S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§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>) -> AsyncDerived<T>
fn from(value: ArcAsyncDerived<T>) -> AsyncDerived<T>
Source§impl<T> From<AsyncDerived<T>> for ArcAsyncDerived<T>
impl<T> From<AsyncDerived<T>> for ArcAsyncDerived<T>
Source§fn from(value: AsyncDerived<T>) -> ArcAsyncDerived<T>
fn from(value: AsyncDerived<T>) -> ArcAsyncDerived<T>
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>) -> AsyncDerived<T, LocalStorage>
fn from_local(value: ArcAsyncDerived<T>) -> AsyncDerived<T, LocalStorage>
Source§impl<T, S> IntoFuture for AsyncDerived<T, S>
impl<T, S> IntoFuture for AsyncDerived<T, S>
Source§type IntoFuture = AsyncDerivedFuture<T>
type IntoFuture = AsyncDerivedFuture<T>
Source§fn into_future(self) -> <AsyncDerived<T, S> as IntoFuture>::IntoFuture
fn into_future(self) -> <AsyncDerived<T, S> as IntoFuture>::IntoFuture
Source§impl<T, S> IsDisposed for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> IsDisposed for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
Source§fn is_disposed(&self) -> bool
fn is_disposed(&self) -> bool
true
, the signal cannot be accessed without a panic.Source§impl<T, S> Notify for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> Notify for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
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>>,
Source§type Value = ReadGuard<Option<T>, AsyncPlain<Option<T>>>
type Value = ReadGuard<Option<T>, AsyncPlain<Option<T>>>
Source§fn try_read_untracked(
&self,
) -> Option<<AsyncDerived<T, S> as ReadUntracked>::Value>
fn try_read_untracked( &self, ) -> Option<<AsyncDerived<T, S> as ReadUntracked>::Value>
None
if the signal has already been disposed.Source§fn read_untracked(&self) -> Self::Value
fn read_untracked(&self) -> Self::Value
Source§fn custom_try_read(&self) -> Option<Option<Self::Value>>
fn custom_try_read(&self) -> Option<Option<Self::Value>>
Read::try_read
implementation despite it being auto implemented. Read moreSource§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
Source§impl<T, S> Write for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
impl<T, S> Write for AsyncDerived<T, S>where
T: 'static,
S: Storage<ArcAsyncDerived<T>>,
Source§fn try_write(&self) -> Option<impl UntrackableGuard>
fn try_write(&self) -> Option<impl UntrackableGuard>
None
if the signal has already been disposed.Source§fn try_write_untracked(&self) -> Option<impl DerefMut>
fn try_write_untracked(&self) -> Option<impl DerefMut>
None
if the signal has already been disposed.Source§fn write(&self) -> impl UntrackableGuard
fn write(&self) -> impl UntrackableGuard
Source§fn write_untracked(&self) -> impl DerefMut
fn write_untracked(&self) -> impl DerefMut
impl<T, S> Copy for AsyncDerived<T, S>
Auto Trait Implementations§
impl<T, S> Freeze for AsyncDerived<T, S>
impl<T, S> RefUnwindSafe for AsyncDerived<T, S>
impl<T, S> Send for AsyncDerived<T, S>
impl<T, S> Sync for AsyncDerived<T, S>
impl<T, S> Unpin for AsyncDerived<T, S>
impl<T, S> 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§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Read for Twhere
T: Track + ReadUntracked,
impl<T> Read for Twhere
T: Track + ReadUntracked,
Source§impl<T> Set for Twhere
T: Update + IsDisposed,
impl<T> Set for Twhere
T: Update + IsDisposed,
Source§impl<T> StorageAccess<T> for T
impl<T> StorageAccess<T> for T
Source§fn as_borrowed(&self) -> &T
fn as_borrowed(&self) -> &T
Source§fn into_taken(self) -> T
fn into_taken(self) -> T
Source§impl<T> Update for Twhere
T: Write,
impl<T> Update for Twhere
T: Write,
Source§fn try_maybe_update<U>(
&self,
fun: impl FnOnce(&mut <T as Update>::Value) -> (bool, U),
) -> Option<U>
fn try_maybe_update<U>( &self, fun: impl FnOnce(&mut <T as Update>::Value) -> (bool, U), ) -> Option<U>
(true, _)
, and returns the value returned by the update function,
or None
if the signal has already been disposed.Source§fn update(&self, fun: impl FnOnce(&mut Self::Value))
fn update(&self, fun: impl FnOnce(&mut Self::Value))
Source§impl<T> UpdateUntracked for Twhere
T: Write,
impl<T> UpdateUntracked for Twhere
T: Write,
Source§fn try_update_untracked<U>(
&self,
fun: impl FnOnce(&mut <T as UpdateUntracked>::Value) -> U,
) -> Option<U>
fn try_update_untracked<U>( &self, fun: impl FnOnce(&mut <T as UpdateUntracked>::Value) -> U, ) -> Option<U>
None
if the signal has already been disposed.
Does not notify subscribers that the signal has changed.Source§impl<T> With for Twhere
T: Read,
impl<T> With for Twhere
T: Read,
Source§type Value = <<T as Read>::Value as Deref>::Target
type Value = <<T as Read>::Value as Deref>::Target
Source§impl<T> WithUntracked for Twhere
T: DefinedAt + ReadUntracked,
impl<T> WithUntracked for Twhere
T: DefinedAt + ReadUntracked,
Source§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.