pub struct Resource<T: 'static, E: 'static, K: 'static = ()> { /* private fields */ }Expand description
Executor-agnostic asynchronous reactive resource driven by a reactive source signal.
Tracks a monotonic u64 request generation so stale or out-of-order completions from
superseded requests are automatically ignored, and Self::drive drops in-flight futures
whenever source changes or Self::refetch is called.
Implementations§
Source§impl<T: PartialEq + 'static, E: PartialEq + 'static> Resource<T, E, ()>
impl<T: PartialEq + 'static, E: PartialEq + 'static> Resource<T, E, ()>
Sourcepub fn stateless(scope: &Scope) -> Self
pub fn stateless(scope: &Scope) -> Self
Creates a keyless Resource<T, E> triggered only on initial drive and Self::refetch.
Source§impl<T: PartialEq + 'static, E: PartialEq + 'static, K: 'static> Resource<T, E, K>
impl<T: PartialEq + 'static, E: PartialEq + 'static, K: 'static> Resource<T, E, K>
Sourcepub fn new(scope: &Scope, source: impl Into<Signal<K>>) -> Self
pub fn new(scope: &Scope, source: impl Into<Signal<K>>) -> Self
Creates a new Resource starting in ResourceState::Pending and keyed by source.
Sourcepub fn with_initial(
scope: &Scope,
source: impl Into<Signal<K>>,
initial: T,
) -> Self
pub fn with_initial( scope: &Scope, source: impl Into<Signal<K>>, initial: T, ) -> Self
Creates a new Resource pre-populated in ResourceState::Ready.
Sourcepub const fn state(&self) -> Atom<ResourceState<T, E>>
pub const fn state(&self) -> Atom<ResourceState<T, E>>
Returns the reactive Atom<ResourceState<T, E>> holding the current lifecycle state.
Sourcepub fn generation(&self) -> u64
pub fn generation(&self) -> u64
Returns the monotonic request generation counter.
Sourcepub fn is_pending(&self) -> bool
pub fn is_pending(&self) -> bool
Returns true when the resource is currently Pending or Refreshing.
Sourcepub fn refetch(&self)
pub fn refetch(&self)
Requests a reload from any active Self::drive loop without changing source.
Sourcepub fn mutate(&self, value: T)
pub fn mutate(&self, value: T)
Optimistically or imperatively sets the resource to ResourceState::Ready and advances
the generation counter so any currently in-flight request is ignored upon completion.
Sourcepub fn dispose(&self)
pub fn dispose(&self)
Transitions the resource to ResourceState::Disposed, cancels any active Self::drive
loop, and invalidates all in-flight requests. Safe to call repeatedly.
Sourcepub async fn load<F, Fut>(&self, loader: F)
pub async fn load<F, Fut>(&self, loader: F)
Runs a single generation-guarded load for the current source value.
If another Self::load, Self::mutate, Self::refetch, or Self::dispose
advances the generation before loader resolves, this completion is discarded.
Sourcepub async fn drive<F, Fut>(&self, loader: F)
pub async fn drive<F, Fut>(&self, loader: F)
Continuously drives this resource on any async executor using Scope::changes.
Loads immediately for the initial source value, and whenever source changes or
Self::refetch is called, drops any still-pending future (cancelling it) and starts a
fresh load. Ends cleanly when Self::dispose is called or source is disposed.