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