@divine/synchronization

中文文档
1.2.1发布于 2020/07/31MITTypeScriptCommonJS近 30 天下载 4748

The Divine Synchronization Library

原始使用文档
文档目录11 节
  1. Introduction
  2. About fairness
  3. Provided primitives
  4. DDRQueue
  5. Queue and FairQueue
  6. Condition and FairCondition
  7. Signal and FairSignal
  8. PubSub and FairPubSub
  9. Semaphore and FairSemaphore
  10. Mutex and FairMutex
  11. Barrier , Lightswitch and ReadWriteLock

The Divine Synchronization Library

npm version Build Status Coverage Status

Introduction

Many JavaScript or TypeScript developers don't think much about synchronization, even if they should. While it's true that JavaScript in Node.js or on the web is single-threaded, anytime an asynchronous function call is invoked, something else might be executed before control is handed back to your callback.

This library contains a classical set of high-quality synchronization primitives that can be used to protect critical sections in such code.

It's written in TypeScript (which means it provides excellent IDE/editor support out of the box) and down-compiled to ES5, so it works equally well in Node.js or in any web browser (a Promise polyfill might be required, though).

About fairness

Most synchronization primitives in this library has a "fair" counterpart, that requires the waiter to provide an ID token. The waiters are then woken up not in FIFO order but in such a way that each ID is processed before a waiter with a duplicate ID.

Provided primitives

This section lists the synchronization primitives provided by this library.

DDRQueue

Although not really a classical primitive, it's a fair queue based on the paper Efficient Fair Queuing Using Deficit Round-Robin. It's more or less just a TypeScript rewrite of Matt Lavin's drr-fair-queue.

This queue is the foundation of all the "fair" variants of the synchronization primitives.

Queue and FairQueue

A blocking queue (strict FIFO, or fair).

Condition and FairCondition

A condition variable implementation with notify(), notifyAll and wait() methods.

Signal and FairSignal

A trivial extension to the condition variable that allows a value to be passed to the waiter.

NOTE: Don't mistake this for a Pub/Sub primitive! While possible, it's very difficult to use these two primitives as event emitters without losing messages. See below instead.

PubSub and FairPubSub

A Pub/Sub primitive, with configurable capacity, blocking publishing and generator-based subscription, suitable to be used in a for await-loop.

Semaphore and FairSemaphore

A classical semaphore that can be signaled and waited for. There is also a non-blocking take() method available.

Mutex and FairMutex

Just a semaphore initialized to 1 and with more appropriate method names.

Barrier, Lightswitch and ReadWriteLock

A few extra primitives taken more or less verbatim from the The Little Book of Semaphores by Allen B. Downey. There are no fair variants of these.

版本与发布记录

@divine/synchronization 共发布 4 个版本,首次发布于 2018/12/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.1latest2020/07/316 年前
1.2.02020/07/286 年前
1.1.02020/06/186 年前
1.0.02018/12/087 年前

关于 @divine/synchronization 的常见问题

@divine/synchronization 是什么?

The Divine Synchronization Library

如何安装 @divine/synchronization?

在项目目录运行 npm install @divine/synchronization,或使用 pnpm add / yarn add / bun add。

@divine/synchronization 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@divine/synchronization 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@divine/synchronization 可以商用吗?

@divine/synchronization 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@divine/synchronization 的下载量有多少?

根据 npm 官方下载统计,@divine/synchronization 在 2026/10/13 – 2026/11/11 这 30 天内被下载 4,748 次,日均约 158 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@divine/synchronization 还在维护吗?

最新版本 1.2.1 发布于 2020/07/31(6 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/12/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。