limit.js (3918B)
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 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 25 26 27 // VARIABLES // 28 29 var debug = logger( 'count-by-async:limit' ); 30 31 32 // MAIN // 33 34 /** 35 * Invokes an indicator function once for each element in a collection, limiting the number of concurrently pending functions. 36 * 37 * @private 38 * @param {Collection} collection - input collection 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} indicator - indicator function 43 * @param {Callback} done - function to invoke upon completion or upon encountering an error 44 * @returns {void} 45 */ 46 function limit( collection, opts, indicator, done ) { 47 var maxIndex; 48 var count; 49 var flg; 50 var lim; 51 var len; 52 var idx; 53 var out; 54 var i; 55 56 len = collection.length; 57 debug( 'Collection length: %d', len ); 58 59 out = {}; 60 if ( len === 0 ) { 61 debug( 'Finished processing a collection.' ); 62 return done( null, out ); 63 } 64 if ( len < opts.limit ) { 65 lim = len; 66 } else { 67 lim = opts.limit; 68 } 69 debug( 'Concurrency limit: %d', lim ); 70 debug( 'Number of arguments: %d', indicator.length ); 71 72 maxIndex = len - 1; 73 count = 0; 74 idx = -1; 75 for ( i = 0; i < lim; i++ ) { 76 // This guard is necessary to protect against synchronous functions which exhaust all collection elements... 77 if ( idx < maxIndex ) { 78 next(); // eslint-disable-line callback-return 79 } 80 } 81 /** 82 * Callback to invoke a provided function for the next element in a collection. 83 * 84 * @private 85 */ 86 function next() { 87 var v; 88 var j; 89 90 idx += 1; 91 j = idx; 92 v = collection[ j ]; 93 94 debug( 'Collection element %d: %s.', j, JSON.stringify( v ) ); 95 if ( indicator.length === 2 ) { 96 indicator.call( opts.thisArg, v, cb ); 97 } else if ( indicator.length === 3 ) { 98 indicator.call( opts.thisArg, v, j, cb ); 99 } else { 100 indicator.call( opts.thisArg, v, j, collection, cb ); 101 } 102 /** 103 * Callback invoked once a provided function finishes processing a collection element. 104 * 105 * @private 106 * @param {*} [error] - error 107 * @param {*} [group] - group 108 * @returns {void} 109 */ 110 function cb( error, group ) { 111 if ( flg ) { 112 // Prevent further processing of collection elements: 113 return; 114 } 115 if ( error ) { 116 flg = true; 117 return clbk( error ); 118 } 119 debug( 'Collection element %d group: %s.', j, group ); 120 121 // Checking for an "own" property is necessary to guard against the edge case where an indicator function returns a group identifier which matches a method or property on the `Object` prototype. 122 if ( hasOwnProp( out, group ) ) { 123 out[ group ] += 1; 124 } else { 125 out[ group ] = 1; 126 } 127 clbk(); 128 } 129 } 130 131 /** 132 * Callback invoked once ready to process the next collection element. 133 * 134 * @private 135 * @param {*} [error] - error 136 * @returns {void} 137 */ 138 function clbk( error ) { 139 if ( error ) { 140 debug( 'Encountered an error: %s', error.message ); 141 return done( error ); 142 } 143 count += 1; 144 debug( 'Processed %d of %d collection elements.', count, len ); 145 if ( idx < maxIndex ) { 146 return next(); 147 } 148 if ( count === len ) { 149 debug( 'Finished processing a collection.' ); 150 return done( null, out ); 151 } 152 } 153 } 154 155 156 // EXPORTS // 157 158 module.exports = limit;