pub struct Resource<T: 'static> { /* private fields */ }Expand description
Declarative async data loading (§4.5): a TRACKED source whose value feeds an async
fetcher; the result lands in a Signal<Load<T>>. The source re-runs when its tracked
reads change; an unchanged source value refetches nothing, Resource::refetch always
does. Latest wins: a source change supersedes the in-flight fetch (aborting its task —
dropping any FetchFuture inside cancels the platform request) and a stale completion
writes nothing. Scope disposal aborts the in-flight fetch the same way.
let stations = Resource::new(
move || region.get(), // tracked: refetch on change
|region| async move { fetch_stations(region).await },
);
when(move || stations.ready(), move || station_list(stations));Implementations§
Source§impl<T: 'static> Resource<T>
impl<T: 'static> Resource<T>
Sourcepub fn new<S, Fut, E>(
source: impl Fn() -> S + 'static,
fetcher: impl Fn(S) -> Fut + 'static,
) -> Resource<T>
pub fn new<S, Fut, E>( source: impl Fn() -> S + 'static, fetcher: impl Fn(S) -> Fut + 'static, ) -> Resource<T>
Start loading: source runs tracked (its signal reads become dependencies) and its
value moves into fetcher’s future, which runs on the main-loop executor via the
installed spawner — so no Send bound anywhere, and the future may read/write signals
after its awaits. Infallible fetchers use E = std::convert::Infallible.
Sourcepub fn signal(self) -> Signal<Load<T>>
pub fn signal(self) -> Signal<Load<T>>
The underlying state signal (tracked reads, when(move || …) guards, each sources).