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_apisto be activated as described in the wasm-bindgen guide.
Demo
Usage
use leptos::prelude::*;
use leptos_use_web_lock::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
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns Err(UseWebLockError::Server) and the task is not executed.
Installation
cargo add leptos-use-web-lock
Options
UseWebLockOptions
Options for use_web_lock_with_options.
Every option is set with a builder method of the same name, e.g. UseWebLockOptions::default().mode(...). See also the API docs.
mode
Type: LockMode
The default mode is LockMode::Exclusive, but LockMode::Shared can be specified.
There can be only one Exclusive holder of a lock, but multiple Shared requests can be
granted at the same time. This can be used to implement the
readers-writer pattern.
if_available
Type: bool
If true, the lock request will fail if the lock cannot be granted immediately without
waiting. The callback is invoked with null. Defaults to false.
steal
Type: bool
If true, then any held locks with the same name will be released, and the request will
be granted, preempting any queued requests for it. Defaults to false.