factory.js (2950B)
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 isFunctionArray = require( '@stdlib/assert/is-function-array' ); 24 var isFunction = require( '@stdlib/assert/is-function' ); 25 26 27 // MAIN // 28 29 /** 30 * Returns a reusable waterfall function. 31 * 32 * @param {FunctionArray} fcns - array of functions 33 * @param {Callback} clbk - callback to invoke upon completion 34 * @param {*} [thisArg] - function context 35 * @throws {TypeError} first argument must be an array of functions 36 * @throws {TypeError} second argument must be a function 37 * @returns {Function} waterfall function 38 * 39 * @example 40 * function foo( next ) { 41 * next( null, 'beep' ); 42 * } 43 * 44 * function bar( str, next ) { 45 * console.log( str ); 46 * // => 'beep' 47 * 48 * next(); 49 * } 50 * 51 * function done( error ) { 52 * if ( error ) { 53 * throw error; 54 * } 55 * } 56 * 57 * var fcns = [ foo, bar ]; 58 * 59 * var waterfall = factory( fcns, done ); 60 * 61 * waterfall(); 62 * waterfall(); 63 * waterfall(); 64 */ 65 function factory( fcns, clbk, thisArg ) { 66 if ( !isFunctionArray( fcns ) ) { 67 throw new TypeError( 'invalid argument. First argument must be a function array. Value: `' + fcns + '`.' ); 68 } 69 if ( !isFunction( clbk ) ) { 70 throw new TypeError( 'invalid argument. Second argument must be a function. Value: `' + clbk + '`.' ); 71 } 72 return waterfall; 73 74 /** 75 * Executes functions in series, passing the results of one function as arguments to the next function. 76 * 77 * @private 78 */ 79 function waterfall() { 80 var idx = -1; 81 next(); // eslint-disable-line callback-return 82 83 /** 84 * Executes the next function in the series. 85 * 86 * @private 87 * @param {(Error|null)} error - error object 88 * @param {...*} args - results to pass to next callback 89 * @returns {void} 90 */ 91 function next() { 92 var args; 93 var len; 94 var i; 95 96 // Check for an error... 97 if ( arguments[ 0 ] ) { 98 return clbk( arguments[ 0 ] ); 99 } 100 // Update the counter and check if we have run all functions... 101 idx += 1; 102 if ( idx >= fcns.length ) { 103 return clbk(); 104 } 105 // Copy the remaining arguments... 106 len = arguments.length; 107 args = new Array( len ); 108 for ( i = 0; i < len-1; i++ ) { 109 args[ i ] = arguments[ i+1 ]; 110 } 111 // Add the callback: 112 args[ i ] = next; 113 114 // Apply the arguments to the next function in the waterfall: 115 fcns[ idx ].apply( thisArg, args ); 116 } 117 } 118 } 119 120 121 // EXPORTS // 122 123 module.exports = factory;