pub struct LazyCache<T: 'static> { /* private fields */ }
Expand description
A structure similar to Value but supports Loading/Error states and automatic refresh after defined amount of time.
use vertigo::{Computed, LazyCache, RequestBuilder, AutoJsJson, Resource};
#[derive(AutoJsJson, PartialEq, Clone)]
pub struct Model {
id: i32,
name: String,
}
pub struct TodoState {
posts: LazyCache<Vec<Model>>,
}
impl TodoState {
pub fn new() -> Self {
let posts = RequestBuilder::get("https://some.api/posts")
.ttl_seconds(300)
.lazy_cache(|status, body| {
if status == 200 {
Some(body.into::<Vec<Model>>())
} else {
None
}
});
TodoState {
posts
}
}
}
See “todo” example in vertigo-demo package for more.
Implementations§
source§impl<T> LazyCache<T>
impl<T> LazyCache<T>
pub fn new( request: RequestBuilder, map_response: impl Fn(u32, RequestBody) -> Option<Result<T, String>> + 'static ) -> Self
sourcepub fn force_update(&self, with_loading: bool)
pub fn force_update(&self, with_loading: bool)
Force refresh the value now
pub fn to_computed(&self) -> Computed<Resource<Rc<T>>>
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for LazyCache<T>
impl<T> !RefUnwindSafe for LazyCache<T>
impl<T> !Send for LazyCache<T>
impl<T> !Sync for LazyCache<T>
impl<T> Unpin for LazyCache<T>
impl<T> !UnwindSafe for LazyCache<T>
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
Mutably borrows from an owned value. Read more