limit.js (4084B)
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 indexOf = require( './../../../index-of' ); 25 26 27 // VARIABLES // 28 29 var debug = logger( 'tabulate-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 tmp; 55 var i; 56 57 len = collection.length; 58 debug( 'Collection length: %d', len ); 59 60 out = []; 61 if ( len === 0 ) { 62 debug( 'Finished processing a collection.' ); 63 return done( null, out ); 64 } 65 tmp = []; 66 if ( len < opts.limit ) { 67 lim = len; 68 } else { 69 lim = opts.limit; 70 } 71 debug( 'Concurrency limit: %d', lim ); 72 debug( 'Number of arguments: %d', indicator.length ); 73 74 maxIndex = len - 1; 75 count = 0; 76 idx = -1; 77 for ( i = 0; i < lim; i++ ) { 78 // This guard is necessary to protect against synchronous functions which exhaust all collection elements... 79 if ( idx < maxIndex ) { 80 next(); // eslint-disable-line callback-return 81 } 82 } 83 /** 84 * Callback to invoke a provided function for the next element in a collection. 85 * 86 * @private 87 */ 88 function next() { 89 var v; 90 var j; 91 92 idx += 1; 93 j = idx; 94 v = collection[ j ]; 95 96 debug( 'Collection element %d: %s.', j, JSON.stringify( v ) ); 97 if ( indicator.length === 2 ) { 98 indicator.call( opts.thisArg, v, cb ); 99 } else if ( indicator.length === 3 ) { 100 indicator.call( opts.thisArg, v, j, cb ); 101 } else { 102 indicator.call( opts.thisArg, v, j, collection, cb ); 103 } 104 /** 105 * Callback invoked once a provided function finishes processing a collection element. 106 * 107 * @private 108 * @param {*} [error] - error 109 * @param {*} [group] - group 110 * @returns {void} 111 */ 112 function cb( error, group ) { 113 var i; 114 if ( flg ) { 115 // Prevent further processing of collection elements: 116 return; 117 } 118 if ( error ) { 119 flg = true; 120 return clbk( error ); 121 } 122 debug( 'Collection element %d group: %s.', j, group ); 123 124 // Determine if we've seen the group/category before. If not, cache a reference which points to its location in the output array; otherwise, update the running count. 125 i = indexOf( tmp, group ); 126 if ( i === -1 ) { 127 tmp.push( group ); 128 out.push( [ group, 1, 0 ] ); 129 } else { 130 out[ i ][ 1 ] += 1; 131 } 132 clbk(); 133 } 134 } 135 136 /** 137 * Callback invoked once ready to process the next collection element. 138 * 139 * @private 140 * @param {*} [error] - error 141 * @returns {void} 142 */ 143 function clbk( error ) { 144 var i; 145 if ( error ) { 146 debug( 'Encountered an error: %s', error.message ); 147 return done( error ); 148 } 149 count += 1; 150 debug( 'Processed %d of %d collection elements.', count, len ); 151 if ( idx < maxIndex ) { 152 return next(); 153 } 154 if ( count === len ) { 155 // Compute percentages... 156 for ( i = 0; i < out.length; i++ ) { 157 out[ i ][ 2 ] = out[ i ][ 1 ] / count; 158 } 159 debug( 'Finished processing a collection.' ); 160 return done( null, out ); 161 } 162 } 163 } 164 165 166 // EXPORTS // 167 168 module.exports = limit;