limit.js (3597B)
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-right-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 and iterating from right to left. 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 count; 48 var flg; 49 var lim; 50 var len; 51 var idx; 52 var i; 53 54 len = collection.length; 55 debug( 'Collection length: %d', len ); 56 57 if ( len === 0 ) { 58 debug( 'Finished processing a collection.' ); 59 return done( null, acc ); 60 } 61 if ( len < opts.limit ) { 62 lim = len; 63 } else { 64 lim = opts.limit; 65 } 66 debug( 'Concurrency limit: %d', lim ); 67 debug( 'Number of arguments: %d', fcn.length ); 68 69 count = 0; 70 idx = len; 71 for ( i = 0; i < lim; i++ ) { 72 // This guard is necessary to protect against synchronous functions which exhaust all collection elements... 73 if ( idx > 0 ) { 74 next(); // eslint-disable-line callback-return 75 } 76 } 77 /** 78 * Callback to invoke a provided function for the next element in a collection. 79 * 80 * @private 81 */ 82 function next() { 83 idx -= 1; 84 debug( 'Collection element %d: %s.', idx, JSON.stringify( collection[ idx ] ) ); 85 if ( fcn.length === 3 ) { 86 fcn.call( opts.thisArg, acc, collection[ idx ], cb ); 87 } else if ( fcn.length === 4 ) { 88 fcn.call( opts.thisArg, acc, collection[ idx ], idx, cb ); 89 } else { 90 fcn.call( opts.thisArg, acc, collection[ idx ], idx, collection, cb ); // eslint-disable-line max-len 91 } 92 /** 93 * Callback invoked once a provided function finishes processing a collection element. 94 * 95 * @private 96 * @param {*} [error] - error 97 * @param {*} [result] - accumulation result 98 * @returns {void} 99 */ 100 function cb( error, result ) { 101 if ( flg ) { 102 // Prevent further processing of collection elements: 103 return; 104 } 105 if ( error ) { 106 flg = true; 107 return clbk( error ); 108 } 109 debug( 'Accumulator: %s', JSON.stringify( result ) ); 110 acc = result; 111 clbk(); 112 } 113 } 114 115 /** 116 * Callback invoked once ready to process the next collection element. 117 * 118 * @private 119 * @param {*} [error] - error 120 * @returns {void} 121 */ 122 function clbk( error ) { 123 if ( error ) { 124 debug( 'Encountered an error: %s', error.message ); 125 return done( error ); 126 } 127 count += 1; 128 debug( 'Processed %d of %d collection elements.', count, len ); 129 if ( idx > 0 ) { 130 return next(); 131 } 132 if ( count === len ) { 133 debug( 'Finished processing a collection.' ); 134 return done( null, acc ); 135 } 136 } 137 } 138 139 140 // EXPORTS // 141 142 module.exports = limit;