leptos_use

Function use_web_lock

Source
pub async fn use_web_lock<C, F, R>(
    name: &str,
    callback: C,
) -> Result<R, UseWebLockError>
where C: FnOnce(Lock) -> F + 'static, F: Future<Output = R>, R: 'static,
Expand description

Rustified Web Locks API.

The Web Locks API allows scripts running in one tab or worker to asynchronously acquire a lock, hold it while work is performed, then release it. While held, no other script executing in the same origin can acquire the same lock, which allows a web app running in multiple tabs or workers to coordinate work and the use of resources.

This function requires --cfg=web_sys_unstable_apis to be activated as described in the wasm-bindgen guide.

§Demo

Link to Demo

§Usage

async fn my_process(_lock: web_sys::Lock) -> i32 {
    // do sth
    42
}

spawn_local(async {
    let res = use_web_lock("my_lock", my_process).await;
    assert!(matches!(res, Ok(42)));
});

§Server-Side Rendering

On the server this returns Err(UseWebLockError::Server) and the task is not executed.