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.
A new query fetches on its first read, so don’t make one every frame: for a query made from
inputs that change, keep a Live.
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>
impl<T: MaybeSend + 'static, E: MaybeSend + 'static> Query<T, E>
Sourcepub fn key(self, key: impl Hash) -> Self
pub fn key(self, key: impl Hash) -> 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). It is only hashed, so it may contain secrets: pass them as they are, not
as an egui::Id, which keeps their Debug text in debug builds. See
Keys, dependencies and refreshes.
Sourcepub fn depends(self, key: impl Into<String>) -> Self
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.
Sourcepub fn refetch_every(self, every: Duration) -> Self
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.
Sourcepub fn stale_after(self, duration: Duration) -> Self
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.
Sourcepub fn cache_for(self, duration: Duration) -> Self
pub fn cache_for(self, duration: Duration) -> Self
How long a keyed value is kept once no query uses it. 5 minutes by default.
Sourcepub fn state(&self) -> QueryState<T, E>
pub fn state(&self) -> QueryState<T, E>
What to show: loading, the value, or why the last fetch failed.
Sourcepub fn data(&self) -> Option<Rc<T>>
pub fn data(&self) -> Option<Rc<T>>
The last value fetched: while it refetches, or if that failed, the previous one.
Sourcepub fn is_loading(&self) -> bool
pub fn is_loading(&self) -> bool
Whether the first fetch is in progress: there is no data yet.
Sourcepub fn is_fetching(&self) -> bool
pub fn is_fetching(&self) -> bool
Whether a fetch is in progress, the first one or a refetch.
Sourcepub fn version(&self) -> u64
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.
Trait Implementations§
Auto Trait Implementations§
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> Freeze for Query<T, E>
impl<T, E> Unpin for Query<T, E>
impl<T, E> UnsafeUnpin for Query<T, E>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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 more