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::{get_driver, Computed, LazyCache, SerdeRequest, Resource};
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, SerdeRequest, PartialEq, Clone)]
pub struct Model {
id: i32,
name: String,
}
pub struct TodoState {
posts: LazyCache<Vec<Model>>,
}
impl TodoState {
pub fn new() -> Self {
let posts = LazyCache::new(300, move || {
let request = get_driver()
.request("https://some.api/posts")
.get();
async move {
request.await.into(|status, body| {
if status == 200 {
Some(body.into_vec::<Model>())
} else {
None
}
})
}
});
TodoState {
posts
}
}
}
See “todo” example in vertigo-demo package for more.
Implementations
sourceimpl<T> LazyCache<T>
impl<T> LazyCache<T>
pub fn new<Fut: Future<Output = Resource<T>> + 'static, F: Fn() -> Fut + 'static>(
max_age: InstantType,
loader: F
) -> Self
pub fn get(&self, context: &Context) -> Resource<Rc<T>>
pub fn force_update(&self, with_loading: bool)
pub fn needs_update(&self) -> bool
pub fn to_computed(&self) -> Computed<Resource<Rc<T>>>
Trait Implementations
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more