promise-flow-control
Advanced tools
+42
-1
| var describeFn = require('describefn'); | ||
| var Promise = require('bluebird'); | ||
| var PFC = require('./PromiseFlowControl'); | ||
| var _ = require('lodash'); | ||
| var generateConcurrencyTest = function (CONCURRENCY) { | ||
| var numberOfRunningFunctions = 0; | ||
| var requestFunctionRun = function () { | ||
| if (numberOfRunningFunctions >= CONCURRENCY) { | ||
| throw new Error('Concurrency is set to ' + CONCURRENCY + ' but more functions are running'); | ||
| } | ||
| numberOfRunningFunctions += 1; | ||
| }; | ||
| var releaseFunctionRun = function () { | ||
| numberOfRunningFunctions -= 1; | ||
| }; | ||
| return { | ||
| params: [{ | ||
| first: function () { | ||
| requestFunctionRun(); | ||
| return Promise.delay(10).then(releaseFunctionRun); | ||
| }, | ||
| second: function () { | ||
| requestFunctionRun(); | ||
| return Promise.delay(20).then(releaseFunctionRun); | ||
| }, | ||
| third: ['first', function () { | ||
| requestFunctionRun(); | ||
| return Promise.delay(10).then(releaseFunctionRun); | ||
| }], | ||
| fourth: ['first', function () { | ||
| requestFunctionRun(); | ||
| return Promise.delay(20).then(releaseFunctionRun); | ||
| }], | ||
| }, CONCURRENCY], | ||
| result: { | ||
| isError: false | ||
| } | ||
| }; | ||
| }; | ||
| describeFn({ | ||
@@ -167,3 +205,6 @@ fn: PFC.props, | ||
| } | ||
| } | ||
| }, | ||
| 'can restrict how many functions run at the same time to 1': generateConcurrencyTest(1), | ||
| 'can restrict how many functions run at the same time to 2': generateConcurrencyTest(2) | ||
| } | ||
@@ -170,0 +211,0 @@ }).then(function (results) { |
+2
-2
| { | ||
| "name": "promise-flow-control", | ||
| "version": "1.0.4", | ||
| "version": "1.0.5", | ||
| "description": "async.auto for promises", | ||
@@ -15,3 +15,3 @@ "main": "PromiseFlowControl.js", | ||
| "scripts": { | ||
| "test": "autodoc --test --handlers autodoc-helpers/autodoc-helpers.js --partials ./autodoc-helpers --verbose ./PromiseFlowControl.js", | ||
| "test": "node describeFnTests.js", | ||
| "docs": "autodoc --handlers ./autodoc-helpers/autodoc-helpers.js --partials ./autodoc-helpers ./PromiseFlowControl.js" | ||
@@ -18,0 +18,0 @@ }, |
@@ -100,5 +100,7 @@ 'use strict'; | ||
| */ | ||
| function props (flowConfig) { | ||
| function props (flowConfig, concurrency) { | ||
| // By setting concurrency to n, you restrict PFC.props to only run n functions at the same time | ||
| concurrency = _.isInteger(concurrency) && concurrency > 0 ? concurrency : Infinity; | ||
| // We need to have two different stores here because one of the functions could | ||
@@ -201,3 +203,3 @@ // return "undefined", which we can not differenciate from an unmet dependency. | ||
| return Promise.map(unresolvedDependencies, processTaskForIdentifier).then(function() { | ||
| return Promise.map(unresolvedDependencies, processTaskForIdentifier, {concurrency: concurrency}).then(function() { | ||
| // After our unmet dependencies are resolved, we just call process task again | ||
@@ -214,4 +216,4 @@ // This time it will return the result from (*1*) | ||
| return Promise.map(allIdentifierParams, function (identifier) { | ||
| return processTask(flowConfig[identifier], identifier); | ||
| }).then(function () { | ||
| return processTask(flowConfig[identifier], identifier); | ||
| }, {concurrency: concurrency}).then(function () { | ||
| return mapIdentifiersToResults(allIdentifierParams); | ||
@@ -223,2 +225,2 @@ }); | ||
| module.exports = PFC; | ||
| module.exports = PFC; |
22245
7.75%514
8.67%