# task-queue-lib

> nodejs消息队列

Latest version **1.5.0** (published 2024-04-29) · ISC license · 0 weekly downloads

## Install

```sh
npm install task-queue-lib
pnpm add task-queue-lib
yarn add task-queue-lib
bun add task-queue-lib
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.0 |
| Published | 2024-04-29 |
| First published | 2022-05-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 61.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | DieHunter |
| Maintainers | diehunter |
| Keywords | queue, task, task-queue |

## Links

- npm: https://www.npmjs.com/package/task-queue-lib
- Repository: https://gitee.com/DieHunter/task-queue
- npm.io page: https://npm.io/package/task-queue-lib

## Dependencies (1)

- [utils-lib-js](https://npm.io/package/utils-lib-js.md) ^2.0.13

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 1.5.0 (latest) — 2024-04-29
- 1.4.2 — 2024-01-17
- 1.4.1 — 2024-01-17
- 1.3.3 — 2024-01-11
- 1.3.2 — 2023-04-27
- 1.3.1 — 2023-04-26
- 1.3.0 — 2023-04-26
- 1.2.3 — 2023-04-25
- 1.2.2 — 2023-04-14
- 1.2.1 — 2023-04-14
- 1.2.0 — 2023-03-28
- 1.1.1 — 2023-03-25
- 1.1.0 — 2023-02-04
- 1.0.11 — 2022-05-31
- 1.0.10 — 2022-05-26
- … 10 more at https://npm.io/package/task-queue-lib/versions

## README

# TaskQueue

任务队列

## 介绍

nodejs 任务队列, 针对请求、IO 操作或其他异步操作高并发削峰的解决方案

## 调试说明

1.  pnpm build（构建）
2.  pnpm example（示例）
3.  pnpm debug（调试源码）

## 用法介绍

### 安装依赖

`npm install task-queue-lib`
或
`yarn add task-queue-lib`
或
`pnpm install task-queue-lib`

### 引入

#### ESM

```javascript
import { TaskQueue } from "task-queue-lib";
```

#### CJS

```javascript
const { TaskQueue } = require("task-queue-lib");
```

#### 浏览器中

```html
<script src="./node_modules/task-queue-lib/dist/umd/index.js"></script>
<script>
  console.log(TaskQueue);
</script>
```

### 使用

#### 切片长度 maxLen

```javascript
const taskQueue = new TaskQueue({ maxLen: 10 });
```

#### 新建一个队列

```javascript
const taskQueue = new TaskQueue({ maxLen: 10 });`
`taskQueue.push([() => {}])
```

#### 单个队列中的函数，有几个就 push 几个

```javascript
taskQueue.push(syncFn.bind(null, "args"));
```

#### 某个队列中的函数全部执行完成后会触发后续操作

```javascript
taskQueue.push([() => {}]).then(console.log); // [ undefined ]
```

或

```javascript
taskQueue.on(taskQueue.defaultKey, console.log).push([() => {}]);
```

#### 队列索引，通过第二个参数分组，异步操作成组完成

```javascript
const fn = (params) => params;
taskQueue.push([fn.bind(this, "hello")], "task1");
taskQueue.push([fn.bind(this, "world")], "task1").then(console.log); // [ 'hello', 'world' ]
taskQueue.push([fn.bind(this, "world")], "task2").then(console.log); // [ 'world' ]
```

#### 删除前三个异步函数

```javascript
taskQueue.unshift(3);
```

#### 初始化当前队列

```javascript
taskQueue.clearQueue();
```

#### 队列步进函数和事件

当我们通过 maxLen 将 push 的队列进行拆分，可以监听队列的步进事件，比如队列长度为 10，maxLen 为 3，那么就会执行 4 次步进函数及事件。

```javascript
// 实例化TaskQueue对象时，传入函数stepCb可以收到队列每次执行的消息。此外通过使用on方法，可以监听队列的步进事件，事件命名规则是`${key}:${step}`，key是taskQueue.push的第二个参数，step是当前队列的步进值。

const stopFn = async () => {
  const stopQueue = new TaskQueue({
    maxLen: 1,
    stepCb: (data) => {
      // data类型参考：IStepCbParams
      console.log(data);
    },
  });
  const key = "key1";
  const queue = [
    () => Promise.resolve("hello"),
    () => Promise.resolve("1"),
    () => Promise.resolve("stop"),
    () => Promise.resolve("2"),
    () => Promise.reject("3"),
  ];
  stopQueue.push(queue, "key1");
  for (let i = 1; i < queue.length + 1; i++) {
    stopQueue.on(key + `:${i}`, (data) => {
      const isStop = data.step === 3;
      console.log(isStop);
      if (isStop) stopQueue.clearQueue(); // 停止队列后续操作
    });
  }
};
stopFn();
```

---
_Source: https://npm.io/package/task-queue-lib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
