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

@antmind/task-pool

v0.2.1

Published

A simple Node.js functional tasks pool implementation, supported both synchronous and asynchronous functions.

Downloads

9

Readme

@antmind/task-pool

Latest version NPM Github Actions build Codacy Badge codecov License

English | 简体中文

@antmind/task-pool is a simple Node.js functional tasks pool implementation, supported both synchronous and asynchronous functions.

Installation

  • Using NPM:

    npm install --save @antmind/task-pool
  • Using Yarn:

    yarn add @antmind/task-pool

Getting Started

  1. Import TaskPool and Task from @antmind/task-pool.

  2. Create a new task pool instance, and you can set concurrency limit if you need.

  3. Create tasks instance and add them into task pool.

  4. Call exec() method to execute functions.

Example

import { Task, TaskPool } from '@antmind/task-pool';

const pool = new TaskPool();

for (let i = 5; i > 0; i -= 1) {
  const task = new Task((val: any) => val, i);
  pool.addTask(task);
}

pool.exec().then((data: any) => console.log(data));
// [ 5, 4, 3, 2, 1 ]

Concurrency Control

You can limit the task concurrency number by concurrency option, and this value must equal or greater than 0.

import { Task, TaskPool } from '@antmind/task-pool';

const pool = new TaskPool({ concurrency: 3 });

for (let i = 5; i > 0; i -= 1) {
  pool.addTask(
    new Task(
      (val: any) => new Promise((resolve: Function) => {
        setTimeout(
          () => {
            console.log(`num: ${val}`);
            resolve(val);
          },
          val * 100,
        );
      }),
      i,
    ),
  );
}

pool.exec().then((data) => console.log(data));
// num: 3
// num: 4
// num: 5
// num: 1
// num: 2
// [ 5, 4, 3, 2, 1 ]

Unlimited concurrency mode

You can set concurrency option as 0 to enable unlimited concurrency mode, it's similar with Promise.all.

import { Task, TaskPool } from '@antmind/task-pool';

const pool = new TaskPool({ concurrency: 0 });

for (let i = 5; i > 0; i -= 1) {
  pool.addTask(
    new Task(
      (val: any) => new Promise((resolve: Function) => {
        setTimeout(
          () => {
            console.log(`num: ${val}`);
            resolve(val);
          },
          val * 100,
        );
      }),
      i,
    ),
  );
}

pool.exec().then((data) => console.log(data));
// num: 1
// num: 2
// num: 3
// num: 4
// num: 5
// [ 5, 4, 3, 2, 1 ]

Configurations

  • concurrency: The tasks maximum concurrency limit number, it should be a integer number greater or equals to 0, and the default value is 30. Set this option value to 0 to enable unlimited concurrency mode.

  • throwsError: Throw error when some task failed if this option set to true, and do not throw error if set to false (you can get errors by getErrors() method). The default value is true.

APIs

Class TaskPool

Constructor

  • constructor()

  • constructor(options: TaskPoolOptions)

  • constructor(task: Task | Task[], options?: TaskPoolOptions)

Methods

  • exec(): Promise<any[]>

    Execute all tasks in the pool, and it'll return a result array after executing.

  • addTask(task: Task): number

    Add a task into task pool, and it'll return the task id.

  • addTasks(tasks: Task[]): number[]

    Add a tasks array into task pool, and it'll return the tasks' id.

  • setConcurrency(concurrency: number): void

    Set concurrency limits.

  • getErrors(): Array<Error | undefined>

    Get errors of last execution, and the index of error is same as task index.

  • getTask(id: number): Task | null

    Get task by id.

Class Task

Constructor

  • constructor(func: Function, ...args: any[])

Method

  • exec(): any

    Execute this task.

  • setArgs(...args: any[]): void

    Set function arguments.

License

This project has been published under MIT license, you can get more detail in LICENSE file.