plimit-lit
Advanced tools
Comparing version 1.4.1 to 1.5.0
@@ -5,4 +5,3 @@ /** | ||
* @param {number} concurrency | ||
* @returns {(fn: Function, ...args: any[]) => Promise<any>} | ||
*/ | ||
export function pLimit(concurrency: number): (fn: Function, ...args: any[]) => Promise<any>; | ||
export function pLimit(concurrency: number): <Arguments extends unknown[], RType>(fn: (...args: Arguments) => RType | PromiseLike<RType>, ...args: Arguments) => Promise<RType>; |
@@ -7,5 +7,3 @@ import { Queue } from 'queue-lit'; | ||
* @param {number} concurrency | ||
* @returns {(fn: Function, ...args: any[]) => Promise<any>} | ||
*/ | ||
function pLimit(concurrency) { | ||
@@ -15,12 +13,10 @@ if (!((Number.isInteger(concurrency) || concurrency === Infinity) && concurrency > 0)) { | ||
} | ||
const queue = new Queue(); | ||
let activeCount = 0; | ||
/** | ||
* next updates the activeCount and executes the next queued item. | ||
*/ | ||
const next = () => { | ||
activeCount--; | ||
if (queue.size > 0) { | ||
@@ -30,2 +26,3 @@ queue.pop()(); | ||
}; | ||
/** | ||
@@ -39,17 +36,12 @@ * run executes a given `fn` passing `args`. Inside the `run` closure any | ||
*/ | ||
const run = async (fn, resolve, args) => { | ||
activeCount++; | ||
const result = (async () => fn(...args))(); | ||
resolve(result); | ||
try { | ||
await result; | ||
} catch {} | ||
next(); | ||
}; | ||
/** | ||
@@ -61,7 +53,4 @@ * enqueue enqueues a given `fn` to be executed while limiting concurrency. | ||
*/ | ||
const enqueue = (fn, resolve, args) => { | ||
queue.push(run.bind(null, fn, resolve, args)); | ||
(async () => { | ||
@@ -73,3 +62,2 @@ // NOTE(joel): This function needs to wait until the next microtask | ||
await Promise.resolve(); | ||
if (activeCount < concurrency && queue.size > 0) { | ||
@@ -80,15 +68,15 @@ queue.pop()(); | ||
}; | ||
/** | ||
* generator defines the public api of `pLimit` and allows enqueueing promise | ||
* returning functions while limiting their concurrency. | ||
* @param {Function} fn | ||
* @param {...any} args | ||
* @returns {Promise<any>} | ||
* @param {(...args: Arguments) => PromiseLike<RType> | RType} fn | ||
* @param {Arguments} args | ||
* @returns {Promise<RType>} | ||
* @template {unknown[]} Arguments | ||
* @template RType | ||
*/ | ||
const generator = (fn, ...args) => new Promise(resolve => { | ||
enqueue(fn, resolve, args); | ||
}); | ||
Object.defineProperties(generator, { | ||
@@ -95,0 +83,0 @@ activeCount: { |
@@ -7,5 +7,3 @@ import { Queue } from 'queue-lit'; | ||
* @param {number} concurrency | ||
* @returns {(fn: Function, ...args: any[]) => Promise<any>} | ||
*/ | ||
function pLimit(concurrency) { | ||
@@ -15,12 +13,10 @@ if (!((Number.isInteger(concurrency) || concurrency === Infinity) && concurrency > 0)) { | ||
} | ||
const queue = new Queue(); | ||
let activeCount = 0; | ||
/** | ||
* next updates the activeCount and executes the next queued item. | ||
*/ | ||
const next = () => { | ||
activeCount--; | ||
if (queue.size > 0) { | ||
@@ -30,2 +26,3 @@ queue.pop()(); | ||
}; | ||
/** | ||
@@ -39,17 +36,12 @@ * run executes a given `fn` passing `args`. Inside the `run` closure any | ||
*/ | ||
const run = async (fn, resolve, args) => { | ||
activeCount++; | ||
const result = (async () => fn(...args))(); | ||
resolve(result); | ||
try { | ||
await result; | ||
} catch {} | ||
next(); | ||
}; | ||
/** | ||
@@ -61,7 +53,4 @@ * enqueue enqueues a given `fn` to be executed while limiting concurrency. | ||
*/ | ||
const enqueue = (fn, resolve, args) => { | ||
queue.push(run.bind(null, fn, resolve, args)); | ||
(async () => { | ||
@@ -73,3 +62,2 @@ // NOTE(joel): This function needs to wait until the next microtask | ||
await Promise.resolve(); | ||
if (activeCount < concurrency && queue.size > 0) { | ||
@@ -80,15 +68,15 @@ queue.pop()(); | ||
}; | ||
/** | ||
* generator defines the public api of `pLimit` and allows enqueueing promise | ||
* returning functions while limiting their concurrency. | ||
* @param {Function} fn | ||
* @param {...any} args | ||
* @returns {Promise<any>} | ||
* @param {(...args: Arguments) => PromiseLike<RType> | RType} fn | ||
* @param {Arguments} args | ||
* @returns {Promise<RType>} | ||
* @template {unknown[]} Arguments | ||
* @template RType | ||
*/ | ||
const generator = (fn, ...args) => new Promise(resolve => { | ||
enqueue(fn, resolve, args); | ||
}); | ||
Object.defineProperties(generator, { | ||
@@ -95,0 +83,0 @@ activeCount: { |
{ | ||
"name": "plimit-lit", | ||
"description": "This package is a helper to run multiple promise-returning & async functions with limited concurrency.", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"author": "Joel Voß <mail@joelvoss.com>", | ||
@@ -35,6 +35,6 @@ "license": "MIT", | ||
"dependencies": { | ||
"queue-lit": "^1.4.0" | ||
"queue-lit": "^1.5.0" | ||
}, | ||
"devDependencies": { | ||
"@jvdx/core": "^2.19.0" | ||
"@jvdx/core": "^3.1.0" | ||
}, | ||
@@ -41,0 +41,0 @@ "prettier": "@jvdx/prettier-config", |
Sorry, the diff of this file is not supported yet
12141
261
Updatedqueue-lit@^1.5.0