npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

task-queue-lib

v1.5.0

Published

nodejs消息队列

Downloads

84

Readme

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();