1.5.0 • Published 22 days ago

task-queue-lib v1.5.0

Weekly downloads
-
License
ISC
Repository
-
Last release
22 days ago

TaskQueue

Task queue

Introduction

nodejs task queue, a solution for high-concurrency peaking for requests, IO operations, or other asynchronous operations

Debugging instructions

  1. pnpm build
  2. pnpm example
  3. pnpm debug (debug source)

Usage introduction

Install dependencies

npm install task-queue-lib or yarn add task-queue-lib or pnpm install task-queue-lib

Introduce

ESM

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

CJS

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

in your browser

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

Use

Slice length maxLen

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

Creates a new queue

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

push as many functions as you have in a single queue

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

Subsequent operations are triggered when all the functions in a queue have finished executing

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

or

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

Queue index, grouped by the second parameter, asynchronous operations are completed in groups

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

Removes the first three asynchronous functions

taskQueue.unshift(3);

initializes the current queue

taskQueue.clearQueue();

Queue stepping functions and events

When we split the push queue using maxLen, we can listen for the step events of the queue, for example, if the queue length is 10 and maxLen is 3, then four steps and events will be executed.

// When the TaskQueue object is instantiated, the incoming function stepCb receives a message for each execution of the queue. In addition, by using the on method, you can listen for the queue's step events. The event name rule is' ${key}:${step} ', key is the second parameter of taskQueue.push, and step is the current queue's step value.

const stopFn = async () => {
  const stopQueue = new TaskQueue({
    maxLen: 1,
    stepCb: (data) => {
      // data Type Reference: 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(); // Stop subsequent operations on the queue
    });
  }
};
stopFn();
1.5.0

22 days ago

1.4.2

4 months ago

1.4.1

4 months ago

1.3.3

4 months ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.3.2

1 year ago

1.2.3

1 year ago

1.3.1

1 year ago

1.2.2

1 year ago

1.3.0

1 year ago

1.2.1

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.0

2 years ago