limit.js (3621B)
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( 'some-by-async:limit' ); 29 30 31 // MAIN // 32 33 /** 34 * Invokes a predicate 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 {PositiveInteger} n - number of elements 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} predicate - predicate function 43 * @param {Callback} done - function to invoke upon completion or upon encountering an error 44 * @returns {void} 45 */ 46 function limit( collection, n, opts, predicate, done ) { 47 var maxIndex; 48 var count; 49 var flg; 50 var lim; 51 var len; 52 var idx; 53 var cnt; 54 var i; 55 56 len = collection.length; 57 debug( 'Collection length: %d', len ); 58 59 if ( len === 0 ) { 60 debug( 'Finished processing a collection.' ); 61 return done( null, false ); 62 } 63 if ( len < opts.limit ) { 64 lim = len; 65 } else { 66 lim = opts.limit; 67 } 68 debug( 'Concurrency limit: %d', lim ); 69 debug( 'Number of arguments: %d', predicate.length ); 70 71 maxIndex = len - 1; 72 count = 0; // processed element count 73 idx = -1; 74 cnt = 0; // success count 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 /** 83 * Callback to invoke a provided function for the next element in a collection. 84 * 85 * @private 86 */ 87 function next() { 88 idx += 1; 89 debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); 90 if ( predicate.length === 2 ) { 91 predicate.call( opts.thisArg, collection[ idx ], clbk ); 92 } else if ( predicate.length === 3 ) { 93 predicate.call( opts.thisArg, collection[ idx ], idx, clbk ); 94 } else { 95 predicate.call( opts.thisArg, collection[ idx ], idx, collection, clbk ); // eslint-disable-line max-len 96 } 97 } 98 99 /** 100 * Callback invoked once a provided function finishes processing a collection element. 101 * 102 * @private 103 * @param {*} [error] - error 104 * @param {*} [result] - test result 105 * @returns {void} 106 */ 107 function clbk( error, result ) { 108 if ( flg ) { 109 // Prevent further processing of collection elements: 110 return; 111 } 112 if ( error ) { 113 flg = true; 114 debug( 'Encountered an error: %s', error.message ); 115 return done( error ); 116 } 117 count += 1; 118 debug( 'Processed %d of %d collection elements.', count, len ); 119 120 debug( 'Test result: %s', !!result ); 121 if ( result && !flg ) { 122 cnt += 1; 123 if ( cnt === n ) { 124 flg = true; 125 debug( 'Finished processing a collection.' ); 126 return done( null, true ); 127 } 128 } 129 if ( idx < maxIndex ) { 130 return next(); 131 } 132 if ( count === len ) { 133 debug( 'Finished processing a collection.' ); 134 return done( null, false ); 135 } 136 } 137 } 138 139 140 // EXPORTS // 141 142 module.exports = limit;