Socket
Book a DemoInstallSign in
Socket

task-queue-lib

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

task-queue-lib

nodejs消息队列

latest
npmnpm
Version
1.5.0
Version published
Maintainers
1
Created
Source

TaskQueue

任务队列

介绍

nodejs 任务队列, 针对请求、IO 操作或其他异步操作高并发削峰的解决方案

调试说明

  • pnpm build(构建)
  • pnpm example(示例)
  • pnpm debug(调试源码)

用法介绍

安装依赖

npm install task-queue-libyarn add task-queue-libpnpm install task-queue-lib

引入

ESM

import { TaskQueue } from "task-queue-lib";

CJS

const { TaskQueue } = require("task-queue-lib");

浏览器中

<script src="./node_modules/task-queue-lib/dist/umd/index.js"></script>
<script>
  console.log(TaskQueue);
</script>

使用

切片长度 maxLen

const taskQueue = new TaskQueue({ maxLen: 10 });

新建一个队列

const taskQueue = new TaskQueue({ maxLen: 10 });`
`taskQueue.push([() => {}])

单个队列中的函数,有几个就 push 几个

taskQueue.push(syncFn.bind(null, "args"));

某个队列中的函数全部执行完成后会触发后续操作

taskQueue.push([() => {}]).then(console.log); // [ undefined ]

taskQueue.on(taskQueue.defaultKey, console.log).push([() => {}]);

队列索引,通过第二个参数分组,异步操作成组完成

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' ]

删除前三个异步函数

taskQueue.unshift(3);

初始化当前队列

taskQueue.clearQueue();

队列步进函数和事件

当我们通过 maxLen 将 push 的队列进行拆分,可以监听队列的步进事件,比如队列长度为 10,maxLen 为 3,那么就会执行 4 次步进函数及事件。

// 实例化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();

Keywords

queue

FAQs

Package last updated on 29 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.