time-to-botec

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

factory.js (4179B)


      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 isFunction = require( '@stdlib/assert/is-function' );
     24 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
     25 var PINF = require( '@stdlib/constants/float64/pinf' );
     26 var validate = require( './validate.js' );
     27 var limit = require( './limit.js' );
     28 
     29 
     30 // MAIN //
     31 
     32 /**
     33 * Returns a function to invoke a function `n` times and return an array of accumulated function return values.
     34 *
     35 * ## Notes
     36 *
     37 * -   If a provided function calls the provided callback with a truthy error argument, the function suspends execution and immediately calls the `done` callback for subsequent error handling.
     38 * -   This function does **not** guarantee that execution is asynchronous. To do so, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
     39 *
     40 *
     41 * @param {Options} [options] - function options
     42 * @param {*} [options.thisArg] - execution context
     43 * @param {PositiveInteger} [options.limit] - maximum number of pending invocations at any one time
     44 * @param {boolean} [options.series=false] - boolean indicating whether to wait for a previous invocation to complete before invoking a provided function again
     45 * @param {Function} fcn - function to invoke
     46 * @throws {TypeError} options argument must be an object
     47 * @throws {TypeError} must provide valid options
     48 * @throws {TypeError} last argument must be a function
     49 * @returns {Function} function which invokes a function `n` times and returns an array of accumulated function return values
     50 *
     51 * @example
     52 * function fcn( i, next ) {
     53 *     setTimeout( onTimeout, 0 );
     54 *     function onTimeout() {
     55 *         next( null, i );
     56 *     }
     57 * }
     58 *
     59 * var opts = {
     60 *     'series': true
     61 * };
     62 *
     63 * var mapFunAsync = factory( opts, fcn );
     64 *
     65 * function done( error, results ) {
     66 *     if ( error ) {
     67 *         throw error;
     68 *     }
     69 *     console.log( results );
     70 * }
     71 *
     72 * mapFunAsync( 10, done );
     73 */
     74 function factory( options, fcn ) {
     75 	var opts;
     76 	var err;
     77 	var f;
     78 
     79 	opts = {};
     80 	if ( arguments.length > 1 ) {
     81 		err = validate( opts, options );
     82 		if ( err ) {
     83 			throw err;
     84 		}
     85 		f = fcn;
     86 	} else {
     87 		f = options;
     88 	}
     89 	if ( !isFunction( f ) ) {
     90 		throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+f+'`.' );
     91 	}
     92 	if ( opts.series ) {
     93 		opts.limit = 1;
     94 	} else if ( !opts.limit ) {
     95 		opts.limit = PINF;
     96 	}
     97 	return mapFunAsync;
     98 
     99 	/**
    100 	* Invokes a function `n` times and returns an array of accumulated function return values.
    101 	*
    102 	* @private
    103 	* @param {NonNegativeInteger} n - number of function invocations
    104 	* @param {Callback} done - function to invoke upon completion
    105 	* @throws {TypeError} first argument must be a nonnegative integer
    106 	* @throws {TypeError} last argument must be a function
    107 	* @returns {void}
    108 	*/
    109 	function mapFunAsync( n, done ) {
    110 		if ( !isNonNegativeInteger( n ) ) {
    111 			throw new TypeError( 'invalid argument. Number of function invocations must be a nonnegative integer. Value: `'+n+'`.' );
    112 		}
    113 		if ( !isFunction( done ) ) {
    114 			throw new TypeError( 'invalid argument. Last argument must be a function. Value: `'+done+'`.' );
    115 		}
    116 		return limit( n, opts, f, clbk );
    117 
    118 		/**
    119 		* Callback invoked upon completion.
    120 		*
    121 		* @private
    122 		* @param {(Error|null)} error - error object
    123 		* @param {Array} results - results
    124 		* @returns {void}
    125 		*/
    126 		function clbk( error, results ) {
    127 			if ( error ) {
    128 				return done( error );
    129 			}
    130 			done( null, results );
    131 		}
    132 	}
    133 }
    134 
    135 
    136 // EXPORTS //
    137 
    138 module.exports = factory;