Skip to main content

Module locks

Module locks 

Source
Expand description

The origin’s lock manager, as a container’s single-writer lock.

A lock entry naming a process is how a container is held on a filesystem, and it cannot be how one is held in a browser: nothing there can be asked whether the tab that wrote an entry is still open, so an entry a crashed tab left behind would hold the document for good. The Web Locks API answers exactly that question and is the browser’s own answer to it — a lock lives as long as the context holding it, and the browser takes it back when the tab goes.

How a lock is held. navigator.locks.request(name, …, callback) grants the lock for as long as the promise the callback returns is unsettled, so Held is a promise nobody has resolved yet and Held::release is the call that resolves it. Releasing is therefore one synchronous call, which is what the container needs of it — a lock is given up as the handle holding it is dropped. On a tab close nothing is called at all and the browser releases the lock itself, which is the case a lock entry could not survive.

ifAvailable is what makes a second claim an answer rather than a wait: a document already open in another view is refused here and read-only there, which is one document per tab.

The bindings are reached through js_sys::Reflect rather than web_sys::LockManager, which is behind --cfg=web_sys_unstable_apis — a flag that would apply to every crate in the workspace’s wasm build for the sake of three calls.

Structs§

Held
A lock held for as long as this value lives.

Functions§

manager 🔒
request
Ask for name without waiting for whoever has it: None is another view of the same document holding it.
unheld 🔒