| Crates.io | weblocks |
| lib.rs | weblocks |
| version | 0.1.0 |
| created_at | 2025-10-22 06:18:40.720753+00 |
| updated_at | 2025-10-22 06:18:40.720753+00 |
| description | RAII-style Rust wrapper for the Web Locks API (navigator.locks) for wasm32 |
| homepage | |
| repository | https://github.com/anchpop/weblocks |
| max_upload_size | |
| id | 1895063 |
| size | 34,754 |
RAII-style Rust wrapper for the Web Locks API (navigator.locks) for wasm32 targets. Converts the callback-based Web Locks API into an ergonomic Rust API.
*Note: this project is 100% AI generated. However I've tested it and it seems to work, and the code seems correct to me.
web_sys unstable APIs: build with --cfg web_sys_unstable_apis.wasm32-unknown-unknown in a browser with Web Locks support..cargo/config.toml (recommended):
[target.wasm32-unknown-unknown]
rustflags = ["--cfg=web_sys_unstable_apis"]
RUSTFLAGS="--cfg web_sys_unstable_apis" \
cargo build --target wasm32-unknown-unknown
Cargo.toml in a wasm project.RUSTFLAGS="--cfg=web_sys_unstable_apis".Example
use weblocks::{acquire, AcquireOptions};
// Acquire an exclusive lock; it is released when `_guard` is dropped.
async fn run() -> Result<(), wasm_bindgen::JsValue> {
let _guard = acquire("my_resource", AcquireOptions::exclusive()).await?;
// do work while holding lock
Ok(())
}
Options
use weblocks::{acquire, AcquireOptions, LockMode};
let opts = AcquireOptions {
mode: LockMode::Shared,
if_available: Some(true),
steal: None,
abort_signal: None,
};
let guard = acquire("shared_name", opts).await?;
js_sys/web_sys directly to set LockOptions properties for broad compatibility.acquire returns an Err(JsValue).Building docs locally
RUSTDOCFLAGS="--cfg web_sys_unstable_apis" cargo doc --open --target wasm32-unknown-unknown
testproj/ with two wasm crates:
lockshim: wraps this crate for JS with acquire/release functions.otherlib: tries a shared lock with ifAvailable to probe availability.cd testprojnpm run build # runs wasm-pack in each cratenpm run serve # serves web/ (requires Node.js; uses npx)