@buttercup/channel-queue

中文文档
1.5.0发布于 2024/12/12MITTypeScriptCommonJS近 30 天下载 2849

A queue management library with channels

原始使用文档
文档目录8 节
  1. About
  2. Installation
  3. Usage
  4. Error Handling
  5. Stacking
  6. Parallel Execution
  7. Waiting and Clearing
  8. Development & Supported Node Versions

channel-queue

A queue management library with channels.

Tests status npm version

About

This library provides a queue system, organised by channels (or "topics"), that allows for finer-grain control over asynchronous method execution. It allows for queuing tasks by channels (string-based names) - tasks executing sequentially when the previous task is done. It also allows for 3 priority levels : normal, high and tail.

Check out the API documentation.

Installation

Simply run:

npm install @buttercup/channel-queue --save

Usage

The simplest usage is to just queue some tasks:

import { ChannelQueue } from "@buttercup/channel-queue";

const queue = new ChannelQueue();
const work = queue.channel("myChannel").enqueue(() => 123);
// 'work' will resolve with 123

const someChannel = queue.channel("someChannel");
someChannel.enqueue(job1);
someChannel.enqueue(job2);
someChannel.enqueue(job3);

You can push tasks above others by setting them as high-priority:

import { ChannelQueue, TaskPriority } from "@buttercup/channel-queue";

const queue = new ChannelQueue();
const workChannel = queue.channel("work");
// some other tasks added to the queue...
workChannel.enqueue(importantJobMethod, TaskPriority.High).then(result => {
    // 'result' is the resolved result from the 'importantJobMethod' function
});

Tasks can also be run as "low" priority or at the tail end of the queue:

import { ChannelQueue, TaskPriority } from "@buttercup/channel-queue";

const queue = new ChannelQueue();
const workChannel = queue.channel("work");
// some other tasks added to the queue...
workChannel.enqueue(runNearEnd, TaskPriority.Tail);

Error Handling

Regardless whether an enqueued task fails or not, the channel will continue processing after it's done.

By default, if the task enqueued in enqueue throws an error, it will throw at the enqueue call:

// The following throws:
await channel.enqueue(() => Promise.reject(new Error("Fails!")));

You can disable enqueued functions throwing errors by setting tasksThrow to false:

channel.tasksThrow = false;
// The following will not throw:
await channel.enqueue(() => Promise.reject(new Error("Fails!")));

Disabling this is really only useful if you use waitForEmpty with the throwForFailures set to true. This allows you to delay error handling until the channel is waited upon:

channel.tasksThrow = false;
channel.enqueue(() => /* ... */)
channel.enqueue(() => Promise.reject(new Error("Fails!")));

// Later:
await channel.waitForEmpty({ throwForFailures: true });

Stacking

Items can be "stacked", meaning that if specified, items can be limited to only 1 pending item in queue. All items of the same stack name would simply queue on the same item and not create more tasks. The stack can be specified when enqueuing:

import { ChannelQueue } from "@buttercup/channel-queue";

const queue = new ChannelQueue();
const workChannel = queue.channel("work");

const promise1 = workChannel.enqueue(saveWorkFn, undefined, /* Stack ID */ "save");
// work start
const promise2 = workChannel.enqueue(saveWorkFn, undefined, "save");
const promise3 = workChannel.enqueue(saveWorkFn, undefined, "save");
// promise2 and promise3 will be equal, as promise2 was still in the queue when promise3

Parallel Execution

Tasks can be run in parallel using the ParallelChannel class. You can create a parallel channel, in place of a regular channel, by calling ChannelQueue#createParallelChannel:

import { ChannelQueue } from "@buttercup/channel-queue";

const queue = new ChannelQueue();
const workChannel = queue.createParallelChannel("work");
workChannel.enqueue(someTask);

// The same channel can be fetched later using the familiar channel() method:
queue.channel("work"); // The parallel channel

Parallel channels, like their name implies, can run tasks in parallel. Instead of running them head-to-tail like regular channels, parallel channels can execute many tasks side-by-side. You can also limit them to a certain number of threads (default is 2) by calling queue.createParallelChannel("name", 5) (where 5 is the maximum number of simultaneous tasks).

Parallel channels by default do not run tasks of different priorities simultaneously. This means that if the current running tasks are high-priority, no normal priority tasks will be started. This feature can be disabled by running parallelChannel.canRunAcrossTaskTypes = true.

Waiting and Clearing

You can wait for a channel to empty (having all tasks completed) by using waitForEmpty:

channel.enqueue(someTask);
channel.enqueue(someTask);

// Later:
await channel.waitForEmpty();

You can clear all enqueued items in a Channel by calling clear():

queue.channel("myChannel").clear();

Development & Supported Node Versions

This library is intended to be used with NodeJS version 6 and later.

To contribute, clone this project and run npm install before beginning development.

To test, simply run npm test.

版本与发布记录

@buttercup/channel-queue 共发布 16 个版本,首次发布于 2017/10/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.0latest2024/12/121.9 年前
1.4.02023/11/132 年前
1.3.02023/11/073 年前
1.2.02022/04/204 年前
1.1.12022/04/194 年前
1.1.02022/04/144 年前

关于 @buttercup/channel-queue 的常见问题

@buttercup/channel-queue 是什么?

A queue management library with channels

如何安装 @buttercup/channel-queue?

在项目目录运行 npm install @buttercup/channel-queue,或使用 pnpm add / yarn add / bun add。

@buttercup/channel-queue 支持 TypeScript 吗?

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

@buttercup/channel-queue 支持 ESM 还是 CommonJS?

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

@buttercup/channel-queue 可以商用吗?

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

@buttercup/channel-queue 的下载量有多少?

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

@buttercup/channel-queue 还在维护吗?

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