Comparing version 1.1.3 to 1.2.0
28
kgo.js
@@ -11,5 +11,26 @@ var run = require('./run'), | ||
results = {}, | ||
errorHandlers = {}; | ||
errorHandlers = {}, | ||
inFlight, | ||
defaultsDefined; | ||
function kgoFn(name, dependencies, fn){ | ||
if(inFlight){ | ||
throw "No tasks or defaults may be set after kgo is in flight"; | ||
} | ||
if(arguments.length === 1 && name !== null && typeof name === 'object'){ | ||
if(defaultsDefined){ | ||
throw "Defaults may be defined only once per kgo"; | ||
} | ||
for(var key in name){ | ||
if(key in tasks){ | ||
throw "A task is already defined for " + key; | ||
} | ||
results[key] = name[key]; | ||
} | ||
defaultsDefined = true; | ||
return kgoFn; | ||
} | ||
if(typeof name !== 'string'){ | ||
@@ -30,2 +51,6 @@ fn = dependencies; | ||
if(name in results){ | ||
throw "A default with the same name as this task (" + name + ") has already been set"; | ||
} | ||
tasks[name] = { | ||
@@ -47,2 +72,3 @@ name: name, | ||
defer(function(){ | ||
inFlight = true; | ||
run(tasks, results, kgoFn); | ||
@@ -49,0 +75,0 @@ }); |
{ | ||
"name": "kgo", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "Flow control the super easy way", | ||
@@ -26,4 +26,14 @@ "main": "kgo.js", | ||
"testling": { | ||
"files": "./test/index.js" | ||
"files": "./test/index.js", | ||
"browsers": [ | ||
"ie/6..latest", | ||
"chrome/22..latest", | ||
"firefox/16..latest", | ||
"safari/latest", | ||
"opera/11.0..latest", | ||
"iphone/6", | ||
"ipad/6", | ||
"android-browser/latest" | ||
] | ||
} | ||
} |
@@ -118,2 +118,32 @@ kgo | ||
## Defaults | ||
You can define default data for use in later tasks by passing an object into kgo, where the keys in the objects will map to dependency names: | ||
kgo | ||
({ | ||
foo: 1 | ||
}) | ||
('bar', function(done){ | ||
done(null, 2); | ||
}); | ||
('baz', ['foo', 'bar'], function(foo, bar, done){ | ||
}) | ||
This is especially useful when you want to use named functions that need additional parameters to run: | ||
var fs = require('fs'); | ||
kgo | ||
({ | ||
'sourcePath': '/foo/bar' | ||
}) | ||
('files', ['sourcePath'], fs.readdir) | ||
### Note: You may only define defaults once in a kgo block. Extra calls will result in an error. | ||
## Errors | ||
@@ -120,0 +150,0 @@ |
@@ -115,2 +115,69 @@ var test = require('grape'), | ||
}) | ||
}); | ||
test('defaults', function(t){ | ||
t.plan(2); | ||
kgo | ||
({ | ||
things: 1, | ||
stuff: 2 | ||
}) | ||
(['things', 'stuff'], function(things, stuff, done){ | ||
t.equal(things, 1); | ||
t.equal(stuff, 2); | ||
}); | ||
}); | ||
test('defaults with same taskname', function(t){ | ||
t.plan(1); | ||
t.throws(function(){ | ||
kgo | ||
({ | ||
things: 1, | ||
stuff: 2 | ||
}) | ||
('stuff', function(done){ | ||
doAsync(done, null, 2); | ||
}) | ||
(['things', 'stuff'], function(things, stuff, done){ | ||
t.fail('task ran but should not have'); | ||
}); | ||
}, 'cannot define a task with the same name as that of a default'); | ||
}); | ||
test('defaults with same taskname, after task', function(t){ | ||
t.plan(1); | ||
t.throws(function(){ | ||
kgo | ||
('stuff', function(done){ | ||
doAsync(done, null, 2); | ||
}) | ||
({ | ||
things: 1, | ||
stuff: 2 | ||
}) | ||
(['things', 'stuff'], function(things, stuff, done){ | ||
t.fail('task ran but should not have'); | ||
}); | ||
}, 'set defaults containing a key that conflicts with a task name'); | ||
}); | ||
test('double defaults', function(t){ | ||
t.plan(1); | ||
t.throws(function(){ | ||
kgo | ||
({ | ||
things: 1 | ||
}) | ||
({ | ||
stuff: 2 | ||
}) | ||
(['things', 'stuff'], function(things, stuff, done){ | ||
t.fail('task ran but should not have'); | ||
}); | ||
}, 'cannot define defaults twice'); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13783
8
297
161