limit.js (3611B)
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-right-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 and iterating from right to left. 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 count; 48 var flg; 49 var lim; 50 var len; 51 var idx; 52 var cnt; 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, false ); 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', predicate.length ); 69 70 count = 0; // processed element count 71 idx = len; 72 cnt = 0; // success count 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 > 0 ) { 76 next(); // eslint-disable-line callback-return 77 } 78 } 79 80 /** 81 * Callback to invoke a provided function for the next element in a collection. 82 * 83 * @private 84 */ 85 function next() { 86 idx -= 1; 87 debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); 88 if ( predicate.length === 2 ) { 89 predicate.call( opts.thisArg, collection[ idx ], clbk ); 90 } else if ( predicate.length === 3 ) { 91 predicate.call( opts.thisArg, collection[ idx ], idx, clbk ); 92 } else { 93 predicate.call( opts.thisArg, collection[ idx ], idx, collection, clbk ); // eslint-disable-line max-len 94 } 95 } 96 97 /** 98 * Callback invoked once a provided function finishes processing a collection element. 99 * 100 * @private 101 * @param {*} [error] - error 102 * @param {*} [result] - test result 103 * @returns {void} 104 */ 105 function clbk( error, result ) { 106 if ( flg ) { 107 // Prevent further processing of collection elements: 108 return; 109 } 110 if ( error ) { 111 flg = true; 112 debug( 'Encountered an error: %s', error.message ); 113 return done( error ); 114 } 115 count += 1; 116 debug( 'Processed %d of %d collection elements.', count, len ); 117 118 debug( 'Test result: %s', !!result ); 119 if ( result && !flg ) { 120 cnt += 1; 121 if ( cnt === n ) { 122 flg = true; 123 debug( 'Finished processing a collection.' ); 124 return done( null, true ); 125 } 126 } 127 if ( idx > 0 ) { 128 return next(); 129 } 130 if ( count === len ) { 131 debug( 'Finished processing a collection.' ); 132 return done( null, false ); 133 } 134 } 135 } 136 137 138 // EXPORTS // 139 140 module.exports = limit;