navigator.locks
稳定性: 1 - 实验性
navigator.locks 只读属性返回一个 LockManager 实例,可用于协调对可能在同一进程的多个线程之间共享的资源的访问。这个全局实现与 浏览器 LockManager API 的语义相匹配。
【The navigator.locks read-only property returns a LockManager instance that
can be used to coordinate access to resources that may be shared across multiple
threads within the same process. This global implementation matches the semantics
of the browser LockManager API.】
// Request an exclusive lock
await navigator.locks.request('my_resource', async (lock) => {
// The lock has been acquired.
console.log(`Lock acquired: ${lock.name}`);
// Lock is automatically released when the function returns
});
// Request a shared lock
await navigator.locks.request('shared_resource', { mode: 'shared' }, async (lock) => {
// Multiple shared locks can be held simultaneously
console.log(`Shared lock acquired: ${lock.name}`);
});// Request an exclusive lock
navigator.locks.request('my_resource', async (lock) => {
// The lock has been acquired.
console.log(`Lock acquired: ${lock.name}`);
// Lock is automatically released when the function returns
}).then(() => {
console.log('Lock released');
});
// Request a shared lock
navigator.locks.request('shared_resource', { mode: 'shared' }, async (lock) => {
// Multiple shared locks can be held simultaneously
console.log(`Shared lock acquired: ${lock.name}`);
}).then(() => {
console.log('Shared lock released');
});请参阅 worker_threads.locks 以获取详细的 API 文档。
【See worker_threads.locks for detailed API documentation.】