Skip to content

Commit

Permalink
Breaking: Move settle functions to own module & add timing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 27, 2016
1 parent 8a34f14 commit 875fac5
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 52 deletions.
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var series = require('./series');
var parallel = require('./parallel');
var settleSeries = require('./settleSeries');
var settleParallel = require('./settleParallel');
'use strict';

var asyncDone = require('async-done');

var createSeries = require('./lib/createSeries');
var createParallel = require('./lib/createParallel');

module.exports = {
series: series,
parallel: parallel,
settleSeries: settleSeries,
settleParallel: settleParallel,
series: createSeries(asyncDone),
parallel: createParallel(asyncDone)
};
21 changes: 21 additions & 0 deletions lib/createParallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var _ = require('lodash');
var async = require('async');

function createSeries(mapFn){

function buildParallel(){
var args = _.flatten(arguments);

function parallel(done){
async.map(args, mapFn, done);
}

return parallel;
}

return buildParallel;
}

module.exports = createSeries;
21 changes: 21 additions & 0 deletions lib/createSeries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var _ = require('lodash');
var async = require('async');

function createSeries(mapFn, onDone){

function buildSeries(){
var args = _.flatten(arguments);

function series(done){
async.mapSeries(args, mapFn, done);
}

return series;
}

return buildSeries;
}

module.exports = createSeries;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
"tap": "~0.4.8"
},
"dependencies": {
"async-settle": "~0.1.0",
"async-done": "~0.2.0",
"async": "~0.2.10",
"async-done": "~0.2.0",
"async-settle": "~0.1.0",
"async-time": "~0.0.1",
"lodash": "~2.4.1"
}
}
17 changes: 0 additions & 17 deletions parallel.js

This file was deleted.

17 changes: 0 additions & 17 deletions series.js

This file was deleted.

9 changes: 9 additions & 0 deletions settle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var series = require('./settle/series');
var parallel = require('./settle/parallel');

module.exports = {
series: series,
parallel: parallel
};
2 changes: 1 addition & 1 deletion settleParallel.js → settle/parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var _ = require('lodash');
var async = require('async');
var asyncSettle = require('async-settle');

var onSettled = require('./lib/onSettled');
var onSettled = require('../lib/onSettled');

function buildParallel(){
var args = _.flatten(arguments);
Expand Down
2 changes: 1 addition & 1 deletion settleSeries.js → settle/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var _ = require('lodash');
var async = require('async');
var asyncSettle = require('async-settle');

var onSettled = require('./lib/onSettled');
var onSettled = require('../lib/onSettled');

function buildParallel(){
var args = _.flatten(arguments);
Expand Down
6 changes: 3 additions & 3 deletions test/settleParallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var test = require('tap').test;

var bach = require('../');
var bach = require('../settle');

function fn1(done){
done(null, 1);
Expand All @@ -23,7 +23,7 @@ function fnError(done){
}

test('should execute functions in parallel and call callback with results on settled', function(t){
bach.settleParallel(fn1, fn2, fn3)(function(errors, results){
bach.parallel(fn1, fn2, fn3)(function(errors, results){
t.notOk(errors, 'errors should be undefined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]');
Expand All @@ -38,7 +38,7 @@ test('should execute functions in parallel and call callback with errors on sett
done(null, 2);
}, 500);
}
bach.settleParallel(fn1, slowFn, fn3, fnError)(function(errors, results){
bach.parallel(fn1, slowFn, fn3, fnError)(function(errors, results){
t.ok(errors, 'errors should be defined');
t.ok(results, 'results should be defined');
t.ok(Array.isArray(errors), 'errors should be an array');
Expand Down
6 changes: 3 additions & 3 deletions test/settleSeries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var test = require('tap').test;

var bach = require('../');
var bach = require('../settle');

function fn1(done){
done(null, 1);
Expand All @@ -23,7 +23,7 @@ function fnError(done){
}

test('should execute functions in series and call callback with results on settled', function(t){
bach.settleSeries(fn1, fn2, fn3)(function(errors, results){
bach.series(fn1, fn2, fn3)(function(errors, results){
t.notOk(errors, 'errors should be undefined');
t.ok(results, 'results should be defined');
t.deepEqual(results, [1, 2, 3], 'results should be [1, 2, 3]');
Expand All @@ -38,7 +38,7 @@ test('should execute functions in series and call callback with errors on settle
done(null, 2);
}, 500);
}
bach.settleSeries(fn1, slowFn, fn3, fnError)(function(errors, results){
bach.series(fn1, slowFn, fn3, fnError)(function(errors, results){
t.ok(errors, 'errors should be defined');
t.ok(results, 'results should be defined');
t.ok(Array.isArray(errors), 'errors should be an array');
Expand Down
41 changes: 41 additions & 0 deletions test/timeParallel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

var test = require('tap').test;

var bach = require('../time');

function fn1(done){
done(null, 1);
}

function fn2(done){
setTimeout(function(){
done(null, 2);
}, 500);
}

function fn3(done){
done(null, 3);
}

function done(){}

test('should emit start event for each function', function(t){
t.plan(3);

bach.on('start', function(evt){
t.ok(evt, 'start event should be defined');
});

bach.parallel(fn1, fn2, fn3)(console.log);
});

test('should emit stop event for each function', function(t){
t.plan(3);

bach.on('stop', function(evt){
t.ok(evt, 'stop event should be defined');
});

bach.parallel(fn1, fn2, fn3)(done);
});
41 changes: 41 additions & 0 deletions test/timeSeries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

var test = require('tap').test;

var bach = require('../time');

function fn1(done){
done(null, 1);
}

function fn2(done){
setTimeout(function(){
done(null, 2);
}, 500);
}

function fn3(done){
done(null, 3);
}

function done(){}

test('should emit start event for each function', function(t){
t.plan(3);

bach.on('start', function(evt){
t.ok(evt, 'start event should be defined');
});

bach.series(fn1, fn2, fn3)(console.log);
});

test('should emit stop event for each function', function(t){
t.plan(3);

bach.on('stop', function(evt){
t.ok(evt, 'stop event should be defined');
});

bach.series(fn1, fn2, fn3)(done);
});
15 changes: 15 additions & 0 deletions time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

var EE = require('events').EventEmitter;

var bus = new EE();

var asyncTime = require('async-time')(bus);

var createSeries = require('./lib/createSeries');
var createParallel = require('./lib/createParallel');

bus.series = createSeries(asyncTime);
bus.parallel = createParallel(asyncTime);

module.exports = bus;

0 comments on commit 875fac5

Please sign in to comment.