Attribute Macro lazy

#[lazy]
Expand description

The #[lazy] macro indicates that a function can be lazy-loaded from a separate WebAssembly (WASM) binary.

The first time the function is called, calling the function will first load that other binary, then call the function. On subsequent calls it will be called immediately, but still return asynchronously to maintain the same API.

#[lazy] can be used to annotate synchronous or async functions. In both cases, the final function will be async and must be called as such.

All parameters and output types should be concrete types, with no generics or impl Trait types.

This should be used in tandem with a suitable build process, such as cargo leptos --split.


#[lazy]
fn lazy_synchronous_function() -> String {
    "Hello, lazy world!".to_string()
}

#[lazy]
async fn lazy_async_function() -> String {
    /* do something that requires async work */
    "Hello, lazy async world!".to_string()
}

async fn use_lazy_functions() {
    // synchronous function has been converted to async
    let value1 = lazy_synchronous_function().await;

    // async function is still async
    let value1 = lazy_async_function().await;
}