diff --git a/CHANGES.md b/CHANGES.md index 2d2c4dff..58424f65 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ * `Driver::utc_now` (Gets current UTC timestamp) * `Driver::timezone_offset` (Gets browsers time zone offset in seconds) * `chrono::NaiveDate` support in `AutoJsJson` +* `LazyCache::::new_resource()` helper ### Changed diff --git a/Dockerfile b/Dockerfile index 5fec6fb5..d032c6fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM rustlang/rust:nightly-slim as build +FROM rustlang/rust:nightly-slim AS build RUN apt-get update && apt-get install -y cmake pkg-config libssl-dev diff --git a/crates/vertigo/src/fetch/lazy_cache.rs b/crates/vertigo/src/fetch/lazy_cache.rs index 0fd69838..97552f33 100644 --- a/crates/vertigo/src/fetch/lazy_cache.rs +++ b/crates/vertigo/src/fetch/lazy_cache.rs @@ -5,7 +5,7 @@ use crate::{ computed::{context::Context, Value}, get_driver, struct_mut::ValueMut, - transaction, Computed, DomNode, Instant, Resource, ToComputed, + transaction, Computed, DomNode, Instant, JsJsonDeserialize, Resource, ToComputed, }; use super::request_builder::{RequestBody, RequestBuilder}; @@ -262,3 +262,33 @@ impl LazyCache { }) } } + +impl LazyCache { + /// Helper to easily create a lazy cache of Vec deserialized from provided URL base and route + /// + /// ```rust + /// use vertigo::{Computed, LazyCache, RequestBuilder, AutoJsJson, Resource}; + /// + /// #[derive(AutoJsJson, PartialEq, Clone)] + /// pub struct Model { + /// id: i32, + /// name: String, + /// } + /// + /// let posts = LazyCache::>::new_resource("https://some.api", "/posts", 60); + /// ``` + pub fn new_resource(api: &str, path: &str, ttl: u64) -> Self { + let url = [api, path].concat(); + + LazyCache::new( + get_driver().request_get(url).ttl_seconds(ttl), + |status, body| { + if status == 200 { + Some(body.into::()) + } else { + None + } + }, + ) + } +} diff --git a/crates/vertigo/src/router.rs b/crates/vertigo/src/router.rs index 02c429fd..d82bed95 100644 --- a/crates/vertigo/src/router.rs +++ b/crates/vertigo/src/router.rs @@ -1,6 +1,6 @@ use crate::{computed::Value, get_driver, Computed, DomNode, EmbedDom, Reactive, ToComputed}; -/// Router based on hash part of current location. +/// Router based on path or hash part of current location. /// /// ```rust /// use vertigo::{dom, Computed, Reactive, Value, DomNode}; @@ -150,3 +150,9 @@ impl> EmbedDom for Router { self.route.embed() } } + +impl> Default for Router { + fn default() -> Self { + Router::new(true) + } +}