use_web_lock
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
Usage
use leptos::prelude::*;
use leptos_use::use_web_lock;
async fn my_process(_lock: web_sys::Lock) -> i32 {
// do sth
42
}
#[component]
fn Demo() -> impl IntoView {
leptos::task::spawn_local(async {
let res = use_web_lock("my_lock", my_process).await;
assert!(matches!(res, Ok(42)));
});
view! { }
}
Server-Side Rendering
On the server this returns Err(UseWebLockError::Server)
and the task is not executed.
Feature
This function is only available if the crate feature
use_web_lock
is enabled