limit.js (3251B)
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( 'map-function-async:limit' ); 29 30 31 // MAIN // 32 33 /** 34 * Invokes a function `n` times, limiting the number of concurrently pending invocations, and returns an array of accumulated function return values. 35 * 36 * @private 37 * @param {NonNegativeInteger} n - number of function invocations 38 * @param {Options} opts - function options 39 * @param {*} [opts.thisArg] - execution context 40 * @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations 41 * @param {Function} fcn - function to invoke 42 * @param {Callback} done - function to invoke upon completion or upon encountering an error 43 * @returns {void} 44 */ 45 function limit( n, opts, fcn, done ) { 46 var count; 47 var flg; 48 var out; 49 var lim; 50 var idx; 51 var m; 52 var i; 53 54 debug( 'Number of invocations: %d', n ); 55 56 // Note: we explicitly preallocate in order to facilitate inserting a function result into its associated output array index. This means we do not ensure "fast" elements for large output arrays. 57 out = new Array( n ); 58 if ( n === 0 ) { 59 debug( 'Finished invoking a function.' ); 60 return done( null, out ); 61 } 62 if ( n < opts.limit ) { 63 lim = n; 64 } else { 65 lim = opts.limit; 66 } 67 debug( 'Concurrency limit: %d', lim ); 68 69 count = 0; 70 idx = -1; 71 m = n - 1; 72 for ( i = 0; i < lim; i++ ) { 73 // This guard is necessary to protect against synchronous functions which exhaust all invocations... 74 if ( idx < m ) { 75 next(); // eslint-disable-line callback-return 76 } 77 } 78 /** 79 * Callback to process the next function invocation. 80 * 81 * @private 82 */ 83 function next() { 84 var j; 85 idx += 1; 86 j = idx; 87 debug( 'Invocation number: %d', j ); 88 fcn.call( opts.thisArg, j, cb ); 89 90 /** 91 * Callback invoked once a provided function completes. 92 * 93 * @private 94 * @param {*} [error] - error 95 * @param {*} [result] - result 96 * @returns {void} 97 */ 98 function cb( error, result ) { 99 if ( flg ) { 100 // Prevent further invocations: 101 return; 102 } 103 if ( error ) { 104 flg = true; 105 return clbk( error ); 106 } 107 out[ j ] = result; 108 clbk(); 109 } 110 } 111 112 /** 113 * Callback invoked once ready to process the next invocation. 114 * 115 * @private 116 * @param {*} [error] - error 117 * @returns {void} 118 */ 119 function clbk( error ) { 120 if ( error ) { 121 debug( 'Encountered an error: %s', error.message ); 122 return done( error ); 123 } 124 count += 1; 125 debug( 'Completed invocation %d of %d.', count, n ); 126 if ( idx < m ) { 127 return next(); 128 } 129 if ( count === n ) { 130 debug( 'Finished invoking a function.' ); 131 return done( null, out ); 132 } 133 } 134 } 135 136 137 // EXPORTS // 138 139 module.exports = limit;