time-to-botec

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

limit.js (3545B)


      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 var objectKeys = require( './../../../keys' );
     25 
     26 
     27 // VARIABLES //
     28 
     29 var debug = logger( 'map-values-async:limit' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Invokes a function once for each own property in a source object, limiting the number of concurrently pending functions.
     36 *
     37 * @private
     38 * @param {Object} obj - source object
     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( obj, opts, fcn, done ) {
     47 	var maxIndex;
     48 	var count;
     49 	var keys;
     50 	var flg;
     51 	var lim;
     52 	var len;
     53 	var idx;
     54 	var out;
     55 	var i;
     56 
     57 	keys = objectKeys( obj );
     58 	len = keys.length;
     59 	debug( 'Number of keys: %d', len );
     60 
     61 	out = {};
     62 	if ( len === 0 ) {
     63 		debug( 'Finished processing an object.' );
     64 		return done( null, out );
     65 	}
     66 	if ( len < opts.limit ) {
     67 		lim = len;
     68 	} else {
     69 		lim = opts.limit;
     70 	}
     71 	debug( 'Concurrency limit: %d', lim );
     72 	debug( 'Number of arguments: %d', fcn.length );
     73 
     74 	maxIndex = len - 1;
     75 	count = 0;
     76 	idx = -1;
     77 	for ( i = 0; i < lim; i++ ) {
     78 		// This guard is necessary to protect against synchronous functions which exhaust all properties...
     79 		if ( idx < maxIndex ) {
     80 			next(); // eslint-disable-line callback-return
     81 		}
     82 	}
     83 	/**
     84 	* Callback to invoke a provided function for the next property.
     85 	*
     86 	* @private
     87 	*/
     88 	function next() {
     89 		var key;
     90 		idx += 1;
     91 		key = keys[ idx ];
     92 		debug( '%s: %s', key, JSON.stringify( obj[ key ] ) );
     93 		if ( fcn.length === 2 ) {
     94 			fcn.call( opts.thisArg, obj[ key ], cb );
     95 		} else if ( fcn.length === 3 ) {
     96 			fcn.call( opts.thisArg, obj[ key ], key, cb );
     97 		} else {
     98 			fcn.call( opts.thisArg, obj[ key ], key, obj, cb );
     99 		}
    100 		/**
    101 		* Callback invoked once a provided function finishes transforming a property value.
    102 		*
    103 		* @private
    104 		* @param {*} [error] - error
    105 		* @param {*} [value] - transformed value
    106 		* @returns {void}
    107 		*/
    108 		function cb( error, value ) {
    109 			if ( flg ) {
    110 				// Prevent further processing of properties:
    111 				return;
    112 			}
    113 			if ( error ) {
    114 				flg = true;
    115 				return clbk( error );
    116 			}
    117 			debug( 'Transform result => %s: %s', key, JSON.stringify( value ) );
    118 			out[ key ] = value;
    119 			clbk();
    120 		}
    121 	}
    122 
    123 	/**
    124 	* Callback invoked once ready to process the next property.
    125 	*
    126 	* @private
    127 	* @param {*} [error] - error
    128 	* @returns {void}
    129 	*/
    130 	function clbk( error ) {
    131 		if ( error ) {
    132 			debug( 'Encountered an error: %s', error.message );
    133 			return done( error );
    134 		}
    135 		count += 1;
    136 		debug( 'Processed %d of %d properties.', count, len );
    137 		if ( idx < maxIndex ) {
    138 			return next();
    139 		}
    140 		if ( count === len ) {
    141 			debug( 'Finished processing an object.' );
    142 			return done( null, out );
    143 		}
    144 	}
    145 }
    146 
    147 
    148 // EXPORTS //
    149 
    150 module.exports = limit;