-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1055 from caolan/autoinject
autoInject
- Loading branch information
Showing
4 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import auto from './auto'; | ||
import forOwn from 'lodash/forOwn'; | ||
import arrayMap from 'lodash/_arrayMap'; | ||
import clone from 'lodash/_baseClone'; | ||
import isArray from 'lodash/isArray'; | ||
|
||
var argsRegex = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; | ||
|
||
function parseParams(func) { | ||
return func.toString().match(argsRegex)[1].split(/\s*\,\s*/); | ||
} | ||
|
||
export default function autoInject(tasks, callback) { | ||
var newTasks = {}; | ||
|
||
forOwn(tasks, function (taskFn, key) { | ||
var params; | ||
|
||
if (isArray(taskFn)) { | ||
params = clone(taskFn); | ||
taskFn = params.pop(); | ||
|
||
newTasks[key] = clone(params).concat(newTask); | ||
} else if (taskFn.length === 0) { | ||
throw new Error("autoInject task functions require explicit parameters."); | ||
} else if (taskFn.length === 1) { | ||
// no dependencies, use the function as-is | ||
newTasks[key] = taskFn; | ||
} else { | ||
params = parseParams(taskFn); | ||
params.pop(); | ||
|
||
newTasks[key] = clone(params).concat(newTask); | ||
} | ||
|
||
function newTask(results, taskCb) { | ||
var newArgs = arrayMap(params, function (name) { | ||
return results[name]; | ||
}); | ||
newArgs.push(taskCb); | ||
taskFn.apply(null, newArgs); | ||
} | ||
}); | ||
|
||
auto(newTasks, callback); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
var async = require('../lib'); | ||
var expect = require('chai').expect; | ||
var _ = require('lodash'); | ||
|
||
describe('autoInject', function () { | ||
|
||
it("basics", function (done) { | ||
var callOrder = []; | ||
async.autoInject({ | ||
task1: function(task2, callback){ | ||
expect(task2).to.equal(2); | ||
setTimeout(function(){ | ||
callOrder.push('task1'); | ||
callback(null, 1); | ||
}, 25); | ||
}, | ||
task2: function(callback){ | ||
setTimeout(function(){ | ||
callOrder.push('task2'); | ||
callback(null, 2); | ||
}, 50); | ||
}, | ||
task3: function(task2, callback){ | ||
expect(task2).to.equal(2); | ||
callOrder.push('task3'); | ||
callback(null, 3); | ||
}, | ||
task4: function(task1, task2, callback){ | ||
expect(task1).to.equal(1); | ||
expect(task2).to.equal(2); | ||
callOrder.push('task4'); | ||
callback(null, 4); | ||
}, | ||
task5: function(task2, callback){ | ||
expect(task2).to.equal(2); | ||
setTimeout(function(){ | ||
callOrder.push('task5'); | ||
callback(null, 5); | ||
}, 0); | ||
}, | ||
task6: function(task2, callback){ | ||
expect(task2).to.equal(2); | ||
callOrder.push('task6'); | ||
callback(null, 6); | ||
} | ||
}, | ||
function(err, results){ | ||
expect(results.task6).to.equal(6); | ||
expect(callOrder).to.eql(['task2','task6','task3','task5','task1','task4']); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should work with array tasks', function (done) { | ||
var callOrder = []; | ||
|
||
async.autoInject({ | ||
task1: function (cb) { | ||
callOrder.push('task1'); | ||
cb(null, 1); | ||
}, | ||
task2: ['task3', function (task3, cb) { | ||
expect(task3).to.equal(3); | ||
callOrder.push('task2'); | ||
cb(null, 2); | ||
}], | ||
task3: function (cb) { | ||
callOrder.push('task3'); | ||
cb(null, 3); | ||
} | ||
}, function () { | ||
expect(callOrder).to.eql(['task1','task3','task2']); | ||
done(); | ||
}); | ||
}); | ||
|
||
}); |