Skip to main content

Live

Struct Live 

Source
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);
                }
            });
        }
    }
}

See Queries from changing inputs.

Implementations§

Source§

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

Source

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.

Source

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).

Source

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.

Source

pub fn clear(&mut self)

Drops the query, for when there’s nothing to ask: it’s Loading until the next update.

Source

pub fn query(&self) -> Option<&Query<T, E>>

The query made last, if any.

Source

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.

Source

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

The query’s value, like Query::data, or the previous value while it loads if kept.

Source

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

Why the query’s last fetch failed.

Source

pub fn is_fetching(&self) -> bool

Whether the query is fetching, see Query::is_fetching.

Source

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§

Source§

impl<T: Debug, E: Debug> Debug for Live<T, E>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T, E> Default for Live<T, E>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Forget for Live<T, E>

§

impl<T, E> Freeze for Live<T, E>
where *const RcInner<RefCell<State<T, E>>> is !null: Freeze, PhantomData<RcInner<RefCell<State<T, E>>>>: Freeze, *const RcInner<dyn Fn() -> Pin<Box<dyn Future<Output = Result<T, E>> + Send>>> is !null: Freeze, PhantomData<RcInner<dyn Fn() -> Pin<Box<dyn Future<Output = Result<T, E>> + Send>>>>: Freeze, *const RcInner<Cell<bool>> is !null: Freeze, PhantomData<RcInner<Cell<bool>>>: Freeze, *const RcInner<T> is !null: Freeze, PhantomData<RcInner<T>>: Freeze,

§

impl<T, E> Move for Live<T, E>

§

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

§

impl<T, E> UnsafeUnpin for Live<T, E>
where *const RcInner<RefCell<State<T, E>>> is !null: UnsafeUnpin, PhantomData<RcInner<RefCell<State<T, E>>>>: UnsafeUnpin, *const RcInner<dyn Fn() -> Pin<Box<dyn Future<Output = Result<T, E>> + Send>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn() -> Pin<Box<dyn Future<Output = Result<T, E>> + Send>>>>: UnsafeUnpin, *const RcInner<Cell<bool>> is !null: UnsafeUnpin, PhantomData<RcInner<Cell<bool>>>: UnsafeUnpin, *const RcInner<T> is !null: UnsafeUnpin, PhantomData<RcInner<T>>: 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> ErasedDestructor for T
where T: 'static,

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, 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.