Skip to content

Commit fbe7cdc

Browse files
committed
docs: update documentation for #[lazy] and #[lazy_route]
1 parent 14884bc commit fbe7cdc

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

leptos_macro/src/lib.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,14 +1030,41 @@ pub fn memo(input: TokenStream) -> TokenStream {
10301030
memo::memo_impl(input)
10311031
}
10321032

1033-
/// The `#[lazy]` macro marks an `async` function as a function that can be lazy-loaded from a
1034-
/// separate (WebAssembly) binary.
1033+
/// The `#[lazy]` macro indicates that a function can be lazy-loaded from a separate WebAssembly (WASM) binary.
10351034
///
10361035
/// The first time the function is called, calling the function will first load that other binary,
1037-
/// then call the function. On subsequent call it will be called immediately, but still return
1036+
/// then call the function. On subsequent calls it will be called immediately, but still return
10381037
/// asynchronously to maintain the same API.
10391038
///
1040-
/// All parameters and output types should be concrete types, with no generics.
1039+
/// `#[lazy]` can be used to annotate synchronous or `async` functions. In both cases, the final function will be
1040+
/// `async` and must be called as such.
1041+
///
1042+
/// All parameters and output types should be concrete types, with no generics or `impl Trait` types.
1043+
///
1044+
/// This should be used in tandem with a suitable build process, such as `cargo leptos --split`.
1045+
///
1046+
/// ```rust
1047+
/// # use leptos_macro::lazy;
1048+
///
1049+
/// #[lazy]
1050+
/// fn lazy_synchronous_function() -> String {
1051+
/// "Hello, lazy world!".to_string()
1052+
/// }
1053+
///
1054+
/// #[lazy]
1055+
/// async fn lazy_async_function() -> String {
1056+
/// /* do something that requires async work */
1057+
/// "Hello, lazy async world!".to_string()
1058+
/// }
1059+
///
1060+
/// async fn use_lazy_functions() {
1061+
/// // synchronous function has been converted to async
1062+
/// let value1 = lazy_synchronous_function().await;
1063+
///
1064+
/// // async function is still async
1065+
/// let value1 = lazy_async_function().await;
1066+
/// }
1067+
/// ```
10411068
#[proc_macro_attribute]
10421069
#[proc_macro_error]
10431070
pub fn lazy(args: proc_macro::TokenStream, s: TokenStream) -> TokenStream {

router_macro/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ quote = { workspace = true, default-features = true }
1919
syn = { features = ["full"], workspace = true, default-features = true }
2020

2121
[dev-dependencies]
22+
leptos = { path = "../leptos" }
2223
leptos_router = { path = "../router" }
2324
leptos_macro = { path = "../leptos_macro" }
2425

router_macro/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,37 @@ impl ToTokens for Segments {
198198
/// add a [`lazy`] annotation to the `view` method, which will cause the code for the view
199199
/// to lazy-load concurrently with the `data` being loaded for the route.
200200
///
201+
/// ```rust
202+
/// use leptos::prelude::*;
203+
/// use leptos_router::{lazy_route, LazyRoute};
204+
///
205+
/// // the route definition
206+
/// #[derive(Debug)]
207+
/// struct BlogListingRoute {
208+
/// titles: Resource<Vec<String>>
209+
/// }
210+
///
211+
/// #[lazy_route]
212+
/// impl LazyRoute for BlogListingRoute {
213+
/// fn data() -> Self {
214+
/// Self {
215+
/// titles: Resource::new(|| (), |_| async {
216+
/// vec![/* todo: load blog posts */]
217+
/// })
218+
/// }
219+
/// }
220+
///
221+
/// // this function will be lazy-loaded, concurrently with data()
222+
/// fn view(this: Self) -> AnyView {
223+
/// let BlogListingRoute { titles } = this;
224+
///
225+
/// // ... now you can use the `posts` resource with Suspense, etc.,
226+
/// // and return AnyView by calling .into_any() on a view
227+
/// # ().into_any()
228+
/// }
229+
/// }
230+
/// ```
231+
///
201232
/// [`impl LazyRoute`]: https://docs.rs/leptos_router/latest/leptos_router/trait.LazyRoute.html
202233
/// [`lazy`]: https://docs.rs/leptos_macro/latest/leptos_macro/macro.lazy.html
203234
#[proc_macro_attribute]

0 commit comments

Comments
 (0)