@mareers/async-throttle
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

async-throttle

A TypeScript library for managing and executing asynchronous functions with constraints and logging capabilities.

Installation

You can install the package using npm:

npm install @mareers/async-throttle

Usage

Import the AsyncThrottle class and the TAsyncThrottleFunction type from the package:

import { AsyncThrottle, TAsyncThrottleFunction } from '@mareers/async-throttle';

Create an instance of AsyncThrottle with the desired options:

const asyncThrottle = new AsyncThrottle({
    maxThreshold: 3,
    delayExecutions: 1000,
});

Add functions to the queue using the addToQueue method:

const task: TAsyncThrottleFunction<[number, number], string> = {
    args: [1, 2000],
    function: async (taskId: number, delay: number) => {
        await new Promise((resolve) => setTimeout(resolve, delay));
        return `Task ${taskId} completed`;
    },
};

asyncThrottle.addToQueue(task);

You can check the current status of the queue using the currentStatus getter:

console.log(asyncThrottle.currentStatus);

To clear the queue, use the clearQueue method:

asyncThrottle.clearQueue();

To stop the async throttle execution, call the stop method:

asyncThrottle.stop();

Async Throttle provides a bunch of events to listen to.

  • result The result of an execution
  • resultError The error-result of an Execution
  • empty Queue is empty
  • add Enqueued something
  • stop Stopped the AsyncThrottler
  • clear Clear the queue
  • log Internal Log
asyncThrottle.on('result', (res) => console.log(`RESULT: ${JSON.stringify(res)}`));
asyncThrottle.on('resultError', (res) => console.log(`RESULT_ERROR: ${JSON.stringify(res)}`));
asyncThrottle.on('empty', () => {
    asyncThrottle.stop();
});
asyncThrottle.on('add', () => {
    // do when something is added
});
asyncThrottle.on('stop', () => {
    console.log(`Execution stopped`);
});
asyncThrottle.on('clear', () => {
    console.log(`Queue cleared`);
});
asyncThrottle.on('log', (message) => console.log(`LOG: ${message}`));

Options

The AsyncThrottle constructor accepts an options object with the following properties:

maxThreshold (required): The maximum number of tasks that can be executed concurrently. delayExecutions (required): The delay (in milliseconds) between each execution loop.

Types

TAsyncThrottleFunction<TArgs extends Array<any>, TResponse>: Represents an asynchronous function with arguments of type TArgs and a return type of Promise<TResponse>.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on the GitHub repository.

License

This package is released under the MIT License.

Note

If you're passing database connection in the args, the connection will not be available when the function gets executed. You'd have to do something like this.

import { createConnection } from 'your-database-library';
let connection = await createConnection({
    // Connection configuration
});

function someDbOperation(connection) {
    // some operation that uses db connection
}

// Do
const task: TAsyncThrottleFunction<[], string> = {
    args: [],
    function: async () => {
        someDbOperation(connection);
    },
};

// Don't
const task2: TAsyncThrottleFunction<[], string> = {
    args: [connection], // Connection might not be available
    function: someDbOperation,
};

Readme

Keywords

none

Package Sidebar

Install

npm i @mareers/async-throttle

Weekly Downloads

1,538

Version

1.0.2

License

none

Unpacked Size

10.1 kB

Total Files

7

Last publish

Collaborators

  • bsreeram08