navigator.locks


稳定性: 1 - 实验性的

¥Stability: 1 - Experimental

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');
});

有关详细的 API 文档,请参阅 worker.locks

¥See worker.locks for detailed API documentation.