All files limit.js

100% Statements 138/138
100% Branches 18/18
100% Functions 4/4
100% Lines 138/138

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 1393x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 8x 12x 4x 4x 12x 12x 12x 12x 12x 12x 30x 30x 26x 26x 30x 12x 12x 12x 12x 12x 12x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 6x 6x 6x 30x 6x 6x 6x 18x 18x 30x 30x 12x 12x 12x 12x 12x 12x 12x 12x 12x 24x 6x 6x 6x 18x 18x 24x 4x 4x 24x 6x 6x 6x 24x 12x 3x 3x 3x 3x 3x  
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var logger = require( 'debug' );
 
 
// VARIABLES //
 
var debug = logger( 'parallel-async:limit' );
 
 
// MAIN //
 
/**
* Invokes functions in a provided array, limiting the number of concurrently pending functions.
*
* @private
* @param {FunctionArray} fcns - array of functions
* @param {Options} opts - function options
* @param {*} [opts.thisArg] - execution context
* @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations
* @param {Callback} done - function to invoke upon completion or upon encountering an error
* @returns {void}
*/
function limit( fcns, opts, done ) {
	var maxIndex;
	var count;
	var flg;
	var lim;
	var len;
	var idx;
	var out;
	var i;
 
	len = fcns.length;
	debug( 'Number of functions: %d', len );
 
	out = new Array( len ); // eslint-disable-line stdlib/no-new-array
	if ( len < opts.limit ) {
		lim = len;
	} else {
		lim = opts.limit;
	}
	debug( 'Concurrency limit: %d', lim );
 
	maxIndex = len - 1;
	count = 0;
	idx = -1;
	for ( i = 0; i < lim; i++ ) {
		// This guard is necessary to protect against synchronous functions...
		if ( idx < maxIndex ) {
			next(); // eslint-disable-line node/callback-return
		}
	}
	/**
	* Callback to invoke the next function.
	*
	* @private
	*/
	function next() {
		var i;
 
		idx += 1;
 
		// Cache the current index value to allow storing results later:
		i = idx;
 
		fcns[ idx ].call( opts.thisArg, resolve );
 
		/**
		* Callback invoked once a provided function finishes.
		*
		* @private
		* @param {*} [error] - error
		* @param {*} [results] - results
		* @returns {void}
		*/
		function resolve( error, results ) {
			if ( flg ) {
				// Prevent further processing:
				return;
			}
			if ( error ) {
				flg = true;
				return clbk( error );
			}
			out[ i ] = results;
			clbk();
		}
	}
 
	/**
	* Callback invoked once ready to process the next function.
	*
	* @private
	* @param {*} [error] - error
	* @returns {void}
	*/
	function clbk( error ) {
		if ( error ) {
			debug( 'Encountered an error: %s', error.message );
			return done( error );
		}
		count += 1;
		debug( 'Processed %d of %d functions.', count, len );
		if ( idx < maxIndex ) {
			return next();
		}
		if ( count === len ) {
			debug( 'Finished processing the functions.' );
			return done( null, out );
		}
	}
}
 
 
// EXPORTS //
 
module.exports = limit;