time-to-botec

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

limit.js (3453B)


      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( 'every-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 {Options} opts - function options
     39 * @param {*} [opts.thisArg] - execution context
     40 * @param {PositiveInteger} [opts.limit] - maximum number of pending function invocations
     41 * @param {Function} predicate - predicate function
     42 * @param {Callback} done - function to invoke upon completion or upon encountering an error
     43 * @returns {void}
     44 */
     45 function limit( collection, opts, predicate, done ) {
     46 	var count;
     47 	var flg;
     48 	var lim;
     49 	var len;
     50 	var idx;
     51 	var i;
     52 
     53 	len = collection.length;
     54 	debug( 'Collection length: %d', len );
     55 
     56 	if ( len === 0 ) {
     57 		debug( 'Finished processing a collection.' );
     58 		return done( null, true );
     59 	}
     60 	if ( len < opts.limit ) {
     61 		lim = len;
     62 	} else {
     63 		lim = opts.limit;
     64 	}
     65 	debug( 'Concurrency limit: %d', lim );
     66 	debug( 'Number of arguments: %d', predicate.length );
     67 
     68 	count = 0;
     69 	idx = len;
     70 	for ( i = 0; i < lim; i++ ) {
     71 		// This guard is necessary to protect against synchronous functions which exhaust all collection elements...
     72 		if ( idx > 0 ) {
     73 			next(); // eslint-disable-line callback-return
     74 		}
     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 ( predicate.length === 2 ) {
     86 			predicate.call( opts.thisArg, collection[ idx ], clbk );
     87 		} else if ( predicate.length === 3 ) {
     88 			predicate.call( opts.thisArg, collection[ idx ], idx, clbk );
     89 		} else {
     90 			predicate.call( opts.thisArg, collection[ idx ], idx, collection, clbk ); // eslint-disable-line max-len
     91 		}
     92 	}
     93 
     94 	/**
     95 	* Callback invoked once a provided function finishes processing a collection element.
     96 	*
     97 	* @private
     98 	* @param {*} [error] - error
     99 	* @param {*} [result] - test result
    100 	* @returns {void}
    101 	*/
    102 	function clbk( error, result ) {
    103 		if ( flg ) {
    104 			// Prevent further processing of collection elements:
    105 			return;
    106 		}
    107 		if ( error ) {
    108 			flg = true;
    109 			debug( 'Encountered an error: %s', error.message );
    110 			return done( error );
    111 		}
    112 		count += 1;
    113 		debug( 'Processed %d of %d collection elements.', count, len );
    114 
    115 		debug( 'Test result: %s', !!result );
    116 		if ( !result && !flg ) {
    117 			flg = true;
    118 			debug( 'Finished processing a collection.' );
    119 			return done( null, false );
    120 		}
    121 		if ( idx > 0 ) {
    122 			return next();
    123 		}
    124 		if ( count === len ) {
    125 			debug( 'Finished processing a collection.' );
    126 			return done( null, true );
    127 		}
    128 	}
    129 }
    130 
    131 
    132 // EXPORTS //
    133 
    134 module.exports = limit;