Skip to main content

Resource

Struct Resource 

Source
pub struct Resource<T>
where 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> Resource<T>
where T: 'static,

Source

pub fn new<S, Fut, E>( source: impl Fn() -> S + 'static, fetcher: impl Fn(S) -> Fut + 'static, ) -> Resource<T>
where S: Clone + PartialEq + 'static, Fut: Future<Output = Result<T, E>> + 'static, E: Error + Send + Sync + 'static,

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.

Source

pub fn signal(self) -> Signal<Load<T>>

The underlying state signal (tracked reads, when(move || …) guards, each sources).

Source

pub fn get(self) -> Load<T>
where T: Clone,

The current load state (tracked).

Source

pub fn with<R>(self, f: impl FnOnce(&Load<T>) -> R) -> R

Read the current load state by reference (tracked).

Source

pub fn loading(self) -> bool

Whether a fetch is in flight (tracked).

Source

pub fn ready(self) -> bool

Whether a value is ready (tracked) — when(move || r.ready(), …).

Source

pub fn refetch(self)

Force a refetch with the current source value (even if unchanged).

Trait Implementations§

Source§

impl<T> Clone for Resource<T>

Source§

fn clone(&self) -> Resource<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Copy for Resource<T>

Auto Trait Implementations§

§

impl<T> Freeze for Resource<T>

§

impl<T> !RefUnwindSafe for Resource<T>

§

impl<T> !Send for Resource<T>

§

impl<T> !Sync for Resource<T>

§

impl<T> Unpin for Resource<T>

§

impl<T> UnsafeUnpin for Resource<T>

§

impl<T> !UnwindSafe for Resource<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoReactive<T, StaticMark> for T
where T: Clone + 'static,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> ErasedDestructor for T
where T: 'static,

Day API ↩ Guides· daybrite.dev