Skip to main content

Query

Struct Query 

Source
pub struct Query<T, E> { /* private fields */ }
Expand description

Async data: fetches on its first read, then keeps its value until it refetches.

Create one with query, from a function that returns the future to fetch the data: it is called for every fetch. Keep it where the views can read it (the app state, a PageState…) and read it every frame you show it. Clones share its state. See the module docs.

fn user(api: &Api, id: u32) -> Query<String, String> {
    let api = api.clone();
    remote::query(move || {
        let api = api.clone();
        async move { api.user(id).await }
    })
    .key(("user", id))
}

Implementations§

Source§

impl<T: MaybeSend + 'static, E: MaybeSend + 'static> Query<T, E>

Source

pub fn new<F, Fut>(fetch: F) -> Self
where F: Fn() -> Fut + 'static, Fut: Future<Output = Result<T, E>> + MaybeSend + 'static,

See query.

Source

pub fn key(self, key: impl AsId) -> Self

Shares the state of the queries with the same key (and value and error types): they fetch once for all of them, and the value is cached. The first query with a key fetches with its function, the others use its value.

The key identifies the data, so it must contain everything the fetch uses, like ("user", id). See Keys, dependencies and refreshes.

Source

pub fn depends(self, key: impl Into<String>) -> Self

Refetches when the dependency key is invalidated: with Nav::invalidate, invalidate, or by a command that refreshes it. Loads with the same dependency rerun along. Call it once per dependency.

A dependency names data on the backend ("users"), unlike the key that names one value (("user", 1)). See Keys, dependencies and refreshes.

Source

pub fn refetch_every(self, every: Duration) -> Self

Refetches this long after the last fetch finished, for as long as it is read: for data that changes elsewhere, like an inbox.

Source

pub fn stale_after(self, duration: Duration) -> Self

How old a cached value may be for this query to use it without refetching, when it first reads it. The cached value is shown while it refetches. Zero by default: a new query always refetches.

Source

pub fn cache_for(self, duration: Duration) -> Self

How long a keyed value is kept once no query uses it. 5 minutes by default.

Source

pub fn state(&self) -> QueryState<T, E>

What to show: loading, the value, or why the last fetch failed.

Source

pub fn data(&self) -> Option<Rc<T>>

The last value fetched: while it refetches, or if that failed, the previous one.

Source

pub fn error(&self) -> Option<Rc<E>>

Why the last fetch failed.

Source

pub fn is_loading(&self) -> bool

Whether the first fetch is in progress: there is no data yet.

Source

pub fn is_fetching(&self) -> bool

Whether a fetch is in progress, the first one or a refetch.

Source

pub fn version(&self) -> u64

Identifies the data: it changes whenever a fetch succeeded, so that a view can rebuild what it derives from the value only then. 0 while there is no value.

Source

pub fn refresh(&self)

Refetches, aborting the fetch in progress. The value stays until the new one arrives.

Source

pub fn set_data(&self, value: T)

Replaces the value, e.g. with what a command returned, or optimistically before a command confirms it. A fetch in progress still replaces it when done.

Trait Implementations§

Source§

impl<T, E> Clone for Query<T, E>

Source§

fn clone(&self) -> Self

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: Debug, E: Debug> Debug for Query<T, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, E> !Freeze for Query<T, E>

§

impl<T, E> !RefUnwindSafe for Query<T, E>

§

impl<T, E> !Send for Query<T, E>

§

impl<T, E> !Sync for Query<T, E>

§

impl<T, E> !UnwindSafe for Query<T, E>

§

impl<T, E> Unpin for Query<T, E>
where Rc<RefCell<State<T, E>>>: Unpin, Rc<dyn Fn() -> Pin<Box<dyn Future<Output = Result<T, E>> + Send>>>: Unpin,

§

impl<T, E> UnsafeUnpin for Query<T, E>
where Rc<RefCell<State<T, E>>>: UnsafeUnpin, Rc<dyn Fn() -> Pin<Box<dyn Future<Output = Result<T, E>> + Send>>>: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<S> FromRef<S> for S

Source§

fn from_ref(state: &S) -> &S

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.