Socket
Socket
Sign inDemoInstall

concurrent-tasks

Package Overview
Dependencies
0
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    concurrent-tasks

A simple task runner which will run tasks concurrently while maintaining limits.


Version published
Weekly downloads
1.7K
decreased by-1.74%
Maintainers
1
Created
Weekly downloads
Ā 

Readme

Source

Concurrent Tasks

npm size npm GitHub issues GitHub forks GitHub stars

A simple task runner which will run all tasks till completion, while maintaining concurrency limits.

The following is a quick reference guide to help you get friendly with Concurrent Tasks. Visit the website for a detailed documentation.

Table of Contents

šŸ‘‹šŸ¼ Introduction

Concurrent Tasks mimics a priority queue by using JavaScript's inbuilt array data type. Each task is a function which signals completion back to the TaskRunner. Once tasks are added, the instance starts executing them until the concurrency criteria is met. Once even a single task is complete (it calls the done callback), the next task in the queue is picked up.

What can I use it with?

The minimalism of Concurrent Tasks makes it an easy-to-use solution across any framework or flavour of JavaScript. It has ZERO dependencies and can be used virtually in any scenario.

  • āœ… Vanilla JavaScript
  • āœ… Frontend Frameworks (React, Vue, Angular, etc)
  • āœ… Backend Frameworks (Express, Hapi, Koa, etc)
  • āœ… NPM Module
  • āœ… Node CLI Application

šŸŽ¬ Getting Started

Via NPM

npm install concurrent-tasks

Or the script tag

<script src="https://cdn.jsdelivr.net/npm/concurrent-tasks/umd/concurrent-tasks.min.jss" type="text/javascript"></script>

Usage

import TaskRunner from 'concurrent-tasks';
ā€‹
const runner = new TaskRunner();
ā€‹
function generateTasks() {
    const tasks = [];
    let count = 1000;
    while(count) {
        tasks.push(done => {
            setTimeout(() => {
                done();
            }, Math.random() * 1000)
        });
        count--;
    }
    return tasks;
}
ā€‹
runner.addMultiple(generateTasks());

Important: Each task passed to the task runner, necessarily has to call the done function. If not, your queue won't process properly.

šŸ The Done Callback

In JavaScript, it gets very difficult for tasks to talk to each other. A lot of times, we need to maintain a map of running tasks and call a function which will update the value and call the next task.

Concurrent Tasks is a JavaScript module, which runs multiple tasks concurrently until all the tasks are complete. It needs a way to figure out when a particular task has been completed.

Solution

Gulp solves this problem by either accepting a return of a Gulp task, or by calling a function done. Similarly, to solve the exact same problem, each task passed to the TaskRunner has access to a special function called done (ingenuity max).

Purpose

The purpose of this function is simple: Tell the instance when a particular task is complete! Internally, the done function does a fair amount of work. It:

  • Makes a free slot available for the internal runner.
  • Updates completion counts and calls the internal runner.
  • Updates the time elapsed from start, until the function calling done's completion.
  • Calls the internal runner to pick up the next task in the priority queue.

Examples

For some examples on how you can use the callback, head over to the docs.

āš’ Configuration

PropertyTypeDefaultDescription
concurrencyNumber3Set the batch size of the task runner.
autoStartBooleantrueDecides whether to start executing tasks automatically.
nameStringRunner <instance-count>A unique name to identify the TaskRunner instance.
onAddFunctionundefinedFired every time add is called.
onStartFunctionundefinedFired every time the runner goes from idle to working state.
onDoneFunctionundefinedFired each time a task calls done callback.
onEndFunctionundefinedFired every time the runner goes from working to idle state.

šŸ•¹ API

The following methods are provided by Concurrent Tasks to help you in manipulating your task list and get the most out of the module.

add

Adds a task to the task list.

add(task: Function, [first: Boolean])

addFirst

Adds a task to the beginning of the task list.

addFirst(task: Function, [first: Boolean])

addMultiple

Adds a collection of tasks to the task list.

addMultiple(tasks: Array.Function, [first: Boolean])

addMultipleFirst

Adds a collection of tasks to the beginning task list.

addMultipleFirst(tasks: Array.Function)

remove

Remove a task from the task list.

remove([first: Boolean])

removeFirst

Remove the first task from the task list.

removeFirst();

removeAt

Remove a task at a particular index from the task list.

removeAt(index: Number)

removeAll

Removes all tasks in the task list.

removeAll();

start

Programmatically start processing the first batch of tasks from the task list.

start();

setConcurrency

Set the concurrency limit on an already-running or a idle instance.

setConcurrency(concurrency: Number)

isBusy

Get the current state of the instance (idle or busy).

isBusy();

šŸ’ŖšŸ¼ Powered by Concurrent Tasks

If you'd like to showcase any:

  • Website
  • Package
  • Framework
  • API

That's been powered by Concurrent Tasks, you can get in touch on Twitter or just use #poweredByConcurrentTasks and it'll be featured here!

šŸ‘©šŸ»ā€šŸ’» Contributing

We ā¤ļø contributions! We are looking for people who echo our sentiments and share the same idea about Concurrent Tasks. Check out the contributing guidelines.

šŸ§ Issues

For any issues or queries you might have about the table, please feel free to create one in the issues section.

šŸ—ŗ Roadmap

  • āœ… Custom concurrency.
  • āœ… Events for task addition, idle state change, done callback firing.
  • āœ… Programmatic and automatic start.
  • āœ… Different kinds of addition and removal.
  • āŒ Priority value for each task.
  • āŒ Storing tasks as a map of priorities.
  • āŒ Adding/removing tasks to/from an existing priority.
  • āŒ Adding/removing multiple tasks to/from an existing priority.
  • āŒ Adding tasks to a new priority.
  • āŒ Adding multiple tasks to a new priority.
  • āŒ Removal of a priority.

šŸ”‘ License

This project is under the MIT License. You can checkout the project's license for more info.

Copyright Ā© 2018.

Keywords

FAQs

Last updated on 16 Dec 2018

Did you know?

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with āš”ļø by Socket Inc