pub struct Live<T, E> { /* private fields */ }Expand description
A Query made from inputs that change, like a date range or a search: made again when
they do.
A query is made from its inputs, and a new one fetches on its first read, so a view can’t
make one from its inputs every frame. Keep a Live in the view’s state instead, and
update it every frame with the inputs: it makes a new query only when they
changed. The inputs are only hashed (like a key), so pass them borrowed.
Optionally, it keeps showing the previous value while the query for new inputs loads, so that a list doesn’t flicker back to a spinner, and waits for the inputs to settle before it makes the query, so that a search doesn’t ask for every letter typed.
use core::time::Duration;
use egui_kit::remote::{self, Live, Query};
fn search(api: &Api, text: &str) -> Query<Vec<String>, String> {
let (api, owned) = (api.clone(), text.to_owned());
remote::query(move || {
let (api, text) = (api.clone(), owned.clone());
async move { api.search(&text).await }
})
.key(("search", text))
}
struct Search {
api: Api,
text: String,
results: Live<Vec<String>, String>,
}
impl Search {
fn new(api: Api) -> Self {
Self {
api,
text: String::new(),
results: remote::live()
// Ask once the text didn't change for a moment…
.debounce(Duration::from_millis(300))
// …and show the last results until the new ones arrive.
.keep_previous(),
}
}
fn ui(&mut self, ui: &mut egui::Ui) {
ui.text_edit_singleline(&mut self.text);
let results = self
.results
.update(&self.text, || search(&self.api, &self.text));
if let Some(names) = results.data() {
// Dimmed while they're the results of an earlier text.
ui.add_enabled_ui(!results.is_previous(), |ui| {
for name in names.iter() {
ui.label(name);
}
});
}
}
}Implementations§
Source§impl<T: MaybeSend + 'static, E: MaybeSend + 'static> Live<T, E>
impl<T: MaybeSend + 'static, E: MaybeSend + 'static> Live<T, E>
Sourcepub fn keep_previous(self) -> Self
pub fn keep_previous(self) -> Self
While the query for new inputs has no answer yet, shows the last value of the one
before: state is Ready with it, and is_previous
says so. Off by default, so that a view shows Loading for new inputs.
Sourcepub fn debounce(self, after: Duration) -> Self
pub fn debounce(self, after: Duration) -> Self
Makes the query only once the inputs didn’t change for after, the first ones included.
Meanwhile, it shows the query made before (and is_previous).
Sourcepub fn update(
&mut self,
inputs: impl Hash,
make: impl FnOnce() -> Query<T, E>,
) -> &Self
pub fn update( &mut self, inputs: impl Hash, make: impl FnOnce() -> Query<T, E>, ) -> &Self
Call it every frame with the inputs: makes the query with make when they differ from
the last query’s (once they settled), and keeps the one made before
otherwise. Returns self, to read it.
The inputs are hashed, so they may be borrowed: (&from, &to). make should key the
query with them if it’s to be cached.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Drops the query, for when there’s nothing to ask: it’s Loading until the next
update.
Sourcepub fn state(&self) -> QueryState<T, E>
pub fn state(&self) -> QueryState<T, E>
What to show, like Query::state, with the previous value while the query loads if
kept. Loading while no query was made.
Sourcepub fn data(&self) -> Option<Rc<T>>
pub fn data(&self) -> Option<Rc<T>>
The query’s value, like Query::data, or the previous value while it loads if
kept.
Sourcepub fn is_fetching(&self) -> bool
pub fn is_fetching(&self) -> bool
Whether the query is fetching, see Query::is_fetching.
Sourcepub fn is_previous(&self) -> bool
pub fn is_previous(&self) -> bool
Whether what it shows isn’t about the last update’s inputs yet: a
debounce is waiting, or the previous value is
shown while the new query loads.
Trait Implementations§
Auto Trait Implementations§
impl<T, E> !RefUnwindSafe for Live<T, E>
impl<T, E> !Send for Live<T, E>
impl<T, E> !Sync for Live<T, E>
impl<T, E> !UnwindSafe for Live<T, E>
impl<T, E> Freeze for Live<T, E>
impl<T, E> Unpin for Live<T, E>
impl<T, E> UnsafeUnpin for Live<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> 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