time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

limit.js (3483B)


      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( 'inmap-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 updates a collection in-place.
     35 *
     36 * @private
     37 * @param {Collection} collection - input collection
     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( collection, opts, fcn, done ) {
     46 	var maxIndex;
     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();
     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 	maxIndex = len - 1;
     70 	count = 0;
     71 	idx = -1;
     72 	for ( i = 0; i < lim; i++ ) {
     73 		// This guard is necessary to protect against synchronous functions which exhaust all collection elements...
     74 		if ( idx < maxIndex ) {
     75 			next(); // eslint-disable-line callback-return
     76 		}
     77 	}
     78 	/**
     79 	* Callback to invoke a provided function for the next element in a collection.
     80 	*
     81 	* @private
     82 	*/
     83 	function next() {
     84 		var j;
     85 		idx += 1;
     86 		j = idx;
     87 		debug( 'Collection element %d: %s.', j, JSON.stringify( collection[ j ] ) );
     88 		if ( fcn.length === 2 ) {
     89 			fcn.call( opts.thisArg, collection[ j ], cb );
     90 		} else if ( fcn.length === 3 ) {
     91 			fcn.call( opts.thisArg, collection[ j ], j, cb );
     92 		} else {
     93 			fcn.call( opts.thisArg, collection[ j ], j, collection, cb );
     94 		}
     95 		/**
     96 		* Callback invoked once a provided function finishes processing a collection element.
     97 		*
     98 		* @private
     99 		* @param {*} [error] - error
    100 		* @param {*} [result] - result
    101 		* @returns {void}
    102 		*/
    103 		function cb( error, result ) {
    104 			if ( flg ) {
    105 				// Prevent further processing of collection elements:
    106 				return;
    107 			}
    108 			if ( error ) {
    109 				flg = true;
    110 				return clbk( error );
    111 			}
    112 			collection[ j ] = result;
    113 			clbk();
    114 		}
    115 	}
    116 
    117 	/**
    118 	* Callback invoked once ready to process the next collection element.
    119 	*
    120 	* @private
    121 	* @param {*} [error] - error
    122 	* @returns {void}
    123 	*/
    124 	function clbk( error ) {
    125 		if ( error ) {
    126 			debug( 'Encountered an error: %s', error.message );
    127 			return done( error );
    128 		}
    129 		count += 1;
    130 		debug( 'Processed %d of %d collection elements.', count, len );
    131 		if ( idx < maxIndex ) {
    132 			return next();
    133 		}
    134 		if ( count === len ) {
    135 			debug( 'Finished processing a collection.' );
    136 			return done();
    137 		}
    138 	}
    139 }
    140 
    141 
    142 // EXPORTS //
    143 
    144 module.exports = limit;