这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
request()
方法在
LockManager
interface requests a
锁
object with parameters specifying its name and characteristics. The requested
锁
is passed to a callback, while the function itself returns a
Promise
that resolves with
undefined
.
mode
特性为
选项
parameter may be either
"exclusive"
or
"shared"
.
Request an
"exclusive"
lock when it should only be held by one code instance at a time. This applies to code in both tabs and workers. Use this to represent mutually exclusive access to a resource. When an
"exclusive"
lock for a given name is held, no other lock with the same name can be held.
Request a
"shared"
lock when multiple instances of the code can share access to a resource. When a
"shared"
lock for a given name is held, other
"shared"
locks for the same name can be granted, but no
"exclusive"
locks with that name can be held or granted.
This shared/exclusive lock pattern is common in database transaction architecture, for example to allow multiple simultaneous readers (each requests a
"shared"
lock) but only one writer (a single
"exclusive"
lock). This is known as the readers-writer pattern. In the
IndexedDB API
, this is exposed as
"readonly"
and
"readwrite"
transactions which have the same semantics.
LockManager.request(var promise = name[, {options}], callback)
An identifier for the lock you want to request.
mode
可选
: Either
"exclusive"
or
"shared"
。默认值为
"exclusive"
.
ifAvailable
可选
:若
true
, the lock request will only be granted if it is not already held. If it cannot be granted, the callback will be invoked with
null
而不是
锁
instance. The default value is
false
.
steal
可选
:若
true
, then any held locks with the same name will be released, and the request will be granted, preempting any queued requests for it. The default value is
false
.
signal
可选
: An
AbortSignal
(
signal
property of an
AbortController
); if specified and the
AbortController
is aborted, the lock request is dropped if it was not already granted.
…
A
Promise
that resolves with undefined when the request is granted.
The following example shows the basic use of the
request()
method with an asynchronous function as the callback. Once the callback is invoked, no other running code on this orign can hold `
my_resource
` until the callback returns.
await navigator.locks.request('my_resource', async lock => {
// The lock was granted.
});
The following example shows how to use the
mode
option for readers and writers.
Notice that both functions use a lock called
my_resource
。
do_read()
requests a lock in
'shared'
mode meaning that multiple calls may occur simultaneously across different event handlers, tabs, or workers.
async function do_read() {
await navigator.locks.request('my_resource', {mode: 'shared'}, async lock => {
// Read code here.
});
}
do_write()
function use the same lock but in
'exclusive'
mode which will delay invocation of the
request()
call in
do_read()
until the write operation has completed. This applies across event handlers, tabs, or workers.
function do_write() {
await navigator.locks.request('my_resource', {mode: 'exclusive'}, async lock => {
// Write code here.
});
}
To grab a lock only if it isn't already being held, use the
ifAvailable
option. In this function
await
means the method will not return until the callback is complete. Since the lock is only granted if it was available, this call avoids needing to wait on the lock being released elsehwere.
await navigator.locks.request('my_resource', {ifAvailable: true}, async lock => {
if (!lock) {
// The lock was not granted - get out fast.
return;
}
// The lock was granted, and no other running code in this origin is holding
// the 'my_res_lock' lock until this returns.
});
To only wait for a lock for a short period of time, use the
signal
选项。
const controller = new AbortController();
// Wait at most 200ms.
setTimeout(() => controller.abort(), 200);
try {
await navigator.locks.request('my_resource', {signal: controller.signal}, async lock => {
// The lock was acquired!
});
} catch (ex) {
if (ex.name === 'AbortError') {
// The request aborted before it could be granted.
}
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web Locks API
The definition of 'request()' in that specification. |
草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
request
|
Chrome 69 | Edge ≤79 | Firefox ? | IE ? | Opera 56 | Safari ? | WebView Android 69 | Chrome Android 69 | Firefox Android ? | Opera Android 48 | Safari iOS ? | Samsung Internet Android 10.0 |
完整支持
兼容性未知
实验。期望将来行为有所改变。
LockManager
query()
request()