Comparing version 1.3.0 to 2.0.0
{ | ||
"name": "kgo", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "Flow control the super easy way", | ||
@@ -5,0 +5,0 @@ "main": "kgo.js", |
@@ -73,3 +73,9 @@ kgo | ||
}) | ||
.on('complete', function(){ | ||
// All dones have been called OR an error occured | ||
}); | ||
.on('error', function(error, stepNames){ | ||
// handle the error for the given step. | ||
}); | ||
@@ -80,22 +86,4 @@ The above will log 3, 0.5; | ||
You can do an async map over items by setting the count of the items in the callback: | ||
Removed as of version 2. Use (foreign)[https://www.npmjs.com/package/foreign] instead. | ||
var items = [1,2,3,4]; | ||
kgo('items', function(done){ | ||
done(null, items); | ||
})('doubled', ['items'], function(items, done){ | ||
// Here we tell kgo that we will be returning an array of results, not just one. | ||
this.count(items.length); | ||
for(var i = 0; i < items.length; i++){ | ||
// Call the callback as usual, but make sure it is called as many times as you said it would be. | ||
done(null, items[i]*2); | ||
} | ||
})(['doubled'], function(doubled){ | ||
// Our doubled numbers | ||
}); | ||
## Ignoring dependency results | ||
@@ -118,3 +106,3 @@ | ||
// here b will be "bar" | ||
}) | ||
}); | ||
@@ -133,7 +121,7 @@ ## Defaults | ||
done(null, 2); | ||
}); | ||
}) | ||
('baz', ['foo', 'bar'], function(foo, bar, done){ | ||
}) | ||
}); | ||
@@ -148,3 +136,3 @@ This is especially useful when you want to use named functions that need additional parameters to run: | ||
}) | ||
('files', ['sourcePath'], fs.readdir) | ||
('files', ['sourcePath'], fs.readdir); | ||
@@ -161,3 +149,3 @@ ### Note: You may only define defaults once in a kgo block. Extra calls will result in an error. | ||
done(null, 2, 4); | ||
}); | ||
}) | ||
@@ -167,3 +155,3 @@ ('baz', ['foo', 'bar'], function(foo, bar, done){ | ||
// bar === 4 | ||
}) | ||
}); | ||
@@ -183,2 +171,13 @@ ## Errors | ||
}); | ||
## Complete | ||
the `complete` event will be emitted when either an error has been returned, or all tasks done methods have been called. | ||
kgo | ||
(task) | ||
(another task) | ||
.on('complete', function(){ | ||
}); |
37
run.js
@@ -13,30 +13,21 @@ var ignoreDependency = /^\!.+/; | ||
} | ||
Step.prototype._count = 1; | ||
Step.prototype._runs = 0; | ||
Step.prototype.run = function(){ | ||
var step = this, | ||
results = [], | ||
didError; | ||
this._task.fn.apply(this, this._args.concat([function(error){ | ||
var stepResults = Array.prototype.slice.call(arguments, 1); | ||
results.push(stepResults); | ||
step._runs++; | ||
var result = Array.prototype.slice.call(arguments, 1); | ||
if(error){ | ||
didError = true; | ||
step.done(error); | ||
}else if(!didError && step._runs === step._count){ | ||
step.done(null, results); | ||
}else if(!didError){ | ||
step.done(null, result); | ||
} | ||
}])); | ||
}; | ||
Step.prototype.count = function(number){ | ||
this._parallel = true; | ||
this._count = number; | ||
}; | ||
Step.prototype.done = function(error, results){ | ||
Step.prototype.done = function(error, result){ | ||
if(error){ | ||
return this._done(error); | ||
} | ||
this._done(null, this._parallel ? rotate90(results) : results[0]); | ||
this._done(null, result); | ||
}; | ||
@@ -78,5 +69,11 @@ | ||
function run(tasks, results, emitter){ | ||
var currentTask; | ||
var currentTask, | ||
noMoreTasks = true; | ||
if(emitter._complete){ | ||
return; | ||
} | ||
for(var key in tasks){ | ||
noMoreTasks = false; | ||
currentTask = tasks[key]; | ||
@@ -91,4 +88,9 @@ | ||
function(names, error, taskResults){ | ||
if(emitter._complete){ | ||
return; | ||
} | ||
if(error){ | ||
emitter._complete = true; | ||
emitter.emit('error', error, names); | ||
emitter.emit('complete'); | ||
return; | ||
@@ -105,2 +107,7 @@ } | ||
} | ||
if(noMoreTasks){ | ||
emitter._complete = true; | ||
emitter.emit('complete'); | ||
} | ||
} | ||
@@ -107,0 +114,0 @@ |
@@ -20,3 +20,3 @@ var test = require('grape'), | ||
test('waterfall', function(t){ | ||
t.plan(1); | ||
t.plan(2); | ||
@@ -29,2 +29,6 @@ kgo('things', function(done){ | ||
t.equal(stuff, 3); | ||
done(); | ||
}) | ||
.on('complete', function(){ | ||
t.pass(); | ||
}); | ||
@@ -34,3 +38,3 @@ }); | ||
test('parallel', function(t){ | ||
t.plan(2); | ||
t.plan(3); | ||
@@ -44,27 +48,12 @@ kgo('things', function(done){ | ||
t.equal(stuff, 2); | ||
done(); | ||
}) | ||
.on('complete', function(){ | ||
t.pass(); | ||
}); | ||
}); | ||
test('map-parallel', function(t){ | ||
test('errors', function(t){ | ||
t.plan(3); | ||
var items = [1,2,3,4]; | ||
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(done, null, items[i]*2); | ||
} | ||
})(['doubled'], function(doubled){ | ||
t.equal(doubled.length, 4); | ||
t.equal(doubled[0], 2); | ||
t.equal(doubled[3], 8); | ||
}); | ||
}); | ||
test('errors', function(t){ | ||
t.plan(2); | ||
kgo('things', function(done){ | ||
@@ -76,13 +65,35 @@ doAsync(done, null, 1); | ||
t.equal(stuff, 3); | ||
done(); | ||
}).on('error', function(error, names){ | ||
t.equal(names[0], 'stuff'); | ||
t.equal(error.message, 'stuff screwed up'); | ||
}) | ||
.on('complete', function(){ | ||
t.pass(); | ||
}); | ||
}); | ||
test('returnless', function(t){ | ||
test('multiple errors', function(t){ | ||
t.plan(2); | ||
kgo | ||
('foo', function(done){ | ||
doAsync(done, new Error('foo screwed up'), 1); | ||
}) | ||
('bar', function(done){ | ||
doAsync(done, new Error('bar screwed up'), 1); | ||
}) | ||
.on('error', function(error, names){ | ||
t.pass(); | ||
}) | ||
.on('complete', function(){ | ||
t.pass(); | ||
}); | ||
}); | ||
test('returnless', function(t){ | ||
t.plan(3); | ||
kgo | ||
('a', function(done){ | ||
@@ -96,8 +107,13 @@ doAsync(done, null, 1); | ||
(['b'], function(b){ | ||
(['b'], function(b, done){ | ||
t.pass('got first task'); | ||
done(); | ||
}) | ||
(['b'], function(b){ | ||
(['b'], function(b, done){ | ||
t.pass('got second task'); | ||
done(); | ||
}) | ||
.on('complete', function(){ | ||
t.pass(); | ||
}); | ||
@@ -107,3 +123,3 @@ }); | ||
test('ignore dependencies', function(t){ | ||
t.plan(1); | ||
t.plan(2); | ||
@@ -120,9 +136,13 @@ kgo | ||
(['b'], function(b){ | ||
(['b'], function(b, done){ | ||
t.equal(b, 1, 'got correct parameter'); | ||
done(); | ||
}) | ||
.on('complete', function(){ | ||
t.pass(); | ||
}); | ||
}); | ||
test('defaults', function(t){ | ||
t.plan(2); | ||
t.plan(3); | ||
@@ -137,2 +157,6 @@ kgo | ||
t.equal(stuff, 2); | ||
done(); | ||
}) | ||
.on('complete', function(){ | ||
t.pass(); | ||
}); | ||
@@ -155,2 +179,3 @@ }); | ||
t.fail('task ran but should not have'); | ||
done(); | ||
}); | ||
@@ -174,2 +199,3 @@ }, 'cannot define a task with the same name as that of a default'); | ||
t.fail('task ran but should not have'); | ||
done(); | ||
}); | ||
@@ -192,2 +218,3 @@ }, 'set defaults containing a key that conflicts with a task name'); | ||
t.fail('task ran but should not have'); | ||
done(); | ||
}); | ||
@@ -198,3 +225,3 @@ }, 'cannot define defaults twice'); | ||
test('multiple datas', function(t){ | ||
t.plan(1); | ||
t.plan(3); | ||
@@ -207,31 +234,11 @@ kgo | ||
t.equal(foo, 1); | ||
}); | ||
done(); | ||
}) | ||
(['bar'], function(bar, done){ | ||
t.equal(bar, 2); | ||
}); | ||
}); | ||
test('multiple datas map-parallel', function(t){ | ||
t.plan(6); | ||
var items = [1,2,3,4]; | ||
kgo | ||
('items', function(done){ | ||
doAsync(done, null, items); | ||
done(); | ||
}) | ||
('doubled', 'trippled', ['items'], function(items, done){ | ||
this.count(items.length); | ||
for(var i = 0; i < items.length; i++){ | ||
doAsync(done, null, items[i]*2, items[i]*3); | ||
} | ||
}) | ||
(['doubled', 'trippled'], function(doubled, trippled){ | ||
t.equal(doubled.length, 4); | ||
t.equal(trippled.length, 4); | ||
t.equal(doubled[0], 2); | ||
t.equal(doubled[3], 8); | ||
t.equal(trippled[0], 3); | ||
t.equal(trippled[3], 12); | ||
.on('complete', function(){ | ||
t.pass(); | ||
}); | ||
}); |
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
367
15714
175