百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
W

web-locks

> 编程语言
开源

Web Locks API

99 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Web Locks API

Web Locks API

Web Locks API implementation for Node.js based on worker_threads, Atomics, SharedArrayBuffer, asynchronous functions, and queue.

See specification: wicg.github.io/web-locks/ and documentation: developer.mozilla.org/en-US/docs/Web/API/Lock

This implementation is a part of Metarhia technology stack, needed for the first pilot project of Node.js application server based on parallel programming and workload micro-isolation. Web Locks API is intended to be merged into Node.js in future.

Features

  • Simplest parallel programming primitive to solve a problem of data races and race conditions.
  • Node.js and worker_threads support.
  • Unified API for single-threaded asynchronous locks and multi-threaded locks.
  • Exclusive lock mode (default).
  • AbortSignal support to cancel a lock request while waiting.
  • TypeScript typings included.

Installation

bash
$ npm install web-locks

Usage

javascript
const { locks, AbortController } = require('web-locks');

// Exclusive lock (default)
await locks.request('Resource name', async (lock) => {
  // use named resource; it is released after the callback settles
});

// Cancel a waiting request with AbortSignal
const controller = new AbortController();
const pending = locks.request(
  'Resource name',
  { signal: controller.signal },
  async () => {
    // critical section
  },
);
controller.abort(); // rejects pending with AbortError

Attach workers so locks are coordinated across threads:

javascript
const { Worker } = require('worker_threads');
const { locks } = require('web-locks');

const worker = new Worker('./worker.js');
locks.attach(worker);

API

locks.request(name, handler): Promise

locks.request(name, options, handler): Promise

  • name: string — resource name
  • options.mode?: 'exclusive' — only exclusive is supported
  • options.signal?: AbortSignal — abort waiting for the lock
  • handler: (lock: Lock) => void | Promise — runs while holding the lock

Already-aborted signals reject immediately. Aborting while waiting cancels the queued request and rejects with AbortError (or signal.reason when set).

locks.query(): Promise

  • held: Array — currently held locks
  • pending: Array — queued lock requests

locks.attach(worker): void

Registers a worker_threads.Worker for cross-thread lock coordination.

Exports

  • locks: LockManager
  • AbortController, AbortSignal, AbortError — native when available, otherwise a small polyfill

License

This implementation of Web Locks API is MIT licensed.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptasyncatomicsawaitdata-race

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言