Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "kgo", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Flow control the super easy way", | ||
@@ -5,0 +5,0 @@ "main": "kgo.js", |
kgo | ||
=== | ||
A stupidly easy flow control function. | ||
Stupidly easy flow control. | ||
@@ -22,3 +22,6 @@ ## Why | ||
kgo(name, deps, fn)(name, deps, fn)(name, deps, fn) | ||
kgo | ||
(name, deps, fn) | ||
(name, deps, fn) | ||
(name, deps, fn); | ||
@@ -33,31 +36,36 @@ ## Example | ||
kgo('things', function(cb){ | ||
kgo | ||
('things', function(done){ | ||
//Something async | ||
setTimeout(function(){ | ||
cb(null, 1); | ||
done(null, 1); | ||
}, 100); | ||
})('stuff', function(cb){ | ||
}) | ||
('stuff', function(done){ | ||
//Something async | ||
setTimeout(function(){ | ||
cb(null, 2); | ||
done(null, 2); | ||
}, 100); | ||
})('whatsits', ['things', 'stuff'], function(things, stuff, cb){ | ||
}) | ||
('whatsits', ['things', 'stuff'], function(things, stuff, done){ | ||
//Something async | ||
setTimeout(function(){ | ||
cb(null, things + stuff); | ||
done(null, things + stuff); | ||
}, 100); | ||
})('dooby', ['things'], function(things, cb){ | ||
}) | ||
('dooby', ['things'], function(things, done){ | ||
//Something async | ||
setTimeout(function(){ | ||
cb(null, things/2); | ||
done(null, things/2); | ||
}, 100); | ||
})(['whatsits', 'dooby'], function(whatsits, dooby, cb){ | ||
}) | ||
(['whatsits', 'dooby'], function(whatsits, dooby, done){ | ||
@@ -71,2 +79,4 @@ //Done | ||
## Async Mapping | ||
You can do an async map over items by setting the count of the items in the callback: | ||
@@ -76,5 +86,5 @@ | ||
kgo('items', function(cb){ | ||
cb(null, items); | ||
})('doubled', ['items'], function(items, cb){ | ||
kgo('items', function(done){ | ||
done(null, items); | ||
})('doubled', ['items'], function(items, done){ | ||
@@ -87,3 +97,3 @@ // Here we tell kgo that we will be returning an array of results, not just one. | ||
// Call the callback as usual, but make sure it is called as many times as you said it would be. | ||
cb(null, items[i]*2); | ||
done(null, items[i]*2); | ||
} | ||
@@ -94,2 +104,21 @@ })(['doubled'], function(doubled){ | ||
## Ignoring dependency results | ||
You will often not need the result of a dependency, and it's annoying to have unused parameters in your functions. | ||
You can specify that you have a dependancy, whos result you don't want, by prefixing the dependancy name with an exclamation mark: | ||
kgo | ||
('a', function(done){ | ||
done(null, 'foo'); | ||
}) | ||
('b', ['!a'], function(done){ | ||
done(null, 'bar'); | ||
}) | ||
(['b'], function(b){ | ||
// here b will be "bar" | ||
}) | ||
## Errors | ||
@@ -104,6 +133,6 @@ | ||
kgo | ||
(calls) | ||
(more calls) | ||
(task) | ||
(another task) | ||
.on('error', function(error, stepName){ | ||
}); |
15
run.js
@@ -0,1 +1,3 @@ | ||
var ignoreDependency = /^\!.+/; | ||
function Step(task, args, done){ | ||
@@ -43,7 +45,16 @@ this._task = task; | ||
for(var i = 0; i < dependants.length; i++) { | ||
if(!(dependants[i] in results)){ | ||
var dependantName = dependants[i], | ||
ignore = dependantName.match(ignoreDependency); | ||
if(ignore){ | ||
dependantName = dependantName.slice(1); | ||
} | ||
if(!(dependantName in results)){ | ||
return; | ||
} | ||
args.push(results[dependants[i]]); | ||
if(!ignore){ | ||
args.push(results[dependantName]); | ||
} | ||
} | ||
@@ -50,0 +61,0 @@ } |
var test = require('grape'), | ||
kgo = require('../'); | ||
function doAsync(cb, error, result){ | ||
function doAsync(done, error, result){ | ||
setTimeout(function(){ | ||
cb(error, result); | ||
done(error, result); | ||
}, 100); | ||
@@ -13,7 +13,7 @@ } | ||
kgo('things', function(cb){ | ||
doAsync(cb, null, 1); | ||
})('stuff', ['things'], function(things, cb){ | ||
doAsync(cb, null, 2 + things); | ||
})(['stuff'], function(stuff, cb){ | ||
kgo('things', function(done){ | ||
doAsync(done, null, 1); | ||
})('stuff', ['things'], function(things, done){ | ||
doAsync(done, null, 2 + things); | ||
})(['stuff'], function(stuff, done){ | ||
t.equal(stuff, 3); | ||
@@ -26,7 +26,7 @@ }); | ||
kgo('things', function(cb){ | ||
doAsync(cb, null, 1); | ||
})('stuff', function(cb){ | ||
doAsync(cb, null, 2); | ||
})(['things', 'stuff'], function(things, stuff, cb){ | ||
kgo('things', function(done){ | ||
doAsync(done, null, 1); | ||
})('stuff', function(done){ | ||
doAsync(done, null, 2); | ||
})(['things', 'stuff'], function(things, stuff, done){ | ||
t.equal(things, 1); | ||
@@ -42,8 +42,8 @@ t.equal(stuff, 2); | ||
kgo('items', function(cb){ | ||
doAsync(cb, null, items); | ||
})('doubled', ['items'], function(items, cb){ | ||
kgo('items', function(done){ | ||
doAsync(done, null, items); | ||
})('doubled', ['items'], function(items, done){ | ||
this.count(items.length); | ||
for(var i = 0; i < items.length; i++){ | ||
doAsync(cb, null, items[i]*2); | ||
doAsync(done, null, items[i]*2); | ||
} | ||
@@ -60,7 +60,7 @@ })(['doubled'], function(doubled){ | ||
kgo('things', function(cb){ | ||
doAsync(cb, null, 1); | ||
})('stuff', ['things'], function(things, cb){ | ||
cb(new Error('stuff screwed up')); | ||
})(['stuff'], function(stuff, cb){ | ||
kgo('things', function(done){ | ||
doAsync(done, null, 1); | ||
})('stuff', ['things'], function(things, done){ | ||
done(new Error('stuff screwed up')); | ||
})(['stuff'], function(stuff, done){ | ||
t.equal(stuff, 3); | ||
@@ -71,2 +71,42 @@ }).on('error', function(error, name){ | ||
}); | ||
}); | ||
test('returnless', function(t){ | ||
t.plan(2); | ||
kgo | ||
('a', function(done){ | ||
doAsync(done, null, 1); | ||
}) | ||
('b', ['a'], function(a, done){ | ||
doAsync(done, null, 1); | ||
}) | ||
(['b'], function(b){ | ||
t.pass('got first task'); | ||
}) | ||
(['b'], function(b){ | ||
t.pass('got second task'); | ||
}); | ||
}); | ||
test('ignore dependencies', function(t){ | ||
t.plan(1); | ||
kgo | ||
('a', function(done){ | ||
doAsync(done, null, 1); | ||
}) | ||
('b', ['!a'], function(done){ | ||
doAsync(done, null, 1); | ||
}) | ||
(['b'], function(b){ | ||
t.equal(b, 1, 'got correct parameter'); | ||
}) | ||
}); |
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
10495
8
227
131