Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bagpipes.flow(..) - support last argument - callback(err,ctx) #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/bagpipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,25 @@ Bagpipes.prototype.getPipe = function getPipe(pipeName, pipeDef) {
return this.pipes[pipeName] = this.createPipe(pipeDef);
};

Bagpipes.prototype.play = function play(pipe, context) {
Bagpipes.prototype.play = function play(pipe, context, done) {

if (typeof context === 'function') {
done = context;
context = undefined;
}
assert(typeof context === 'undefined' || typeof context === 'object', 'context must be an object');
assert(typeof done === 'undefined' || typeof done === 'function' && done.length == 2, 'end-callback must be a function that expects 2 argument - error, and the context');

if (!context) { context = {}; }

var pw = pipeworks();
pw.fit(function(context, next) { pipe.siphon(context, next); });
if (context._finish) { pw.fit(context._finish); }
if (done) {
pw.fit( function done_success(ctx, _) { done(null, ctx) } );
pw.fault( function done_fault(ctx, err) { done (err, ctx) } );
}

pw.flow(context);
};

Expand Down
28 changes: 28 additions & 0 deletions test/bagpipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,33 @@ describe('bagpipes', function() {
context.error.should.be.an.Error;
context.output.should.eql('test');
done();
});
describe('when provided with a done(err,ctx) callback', function() {
describe('and the pipe flow succeeds', function() {
it('should pass context to the done', function(done){
var userFittingsDirs = [ path.resolve(__dirname, './fixtures/fittings') ];
var pipe = [ 'test', 'test', 'emit', 'test' ];
var bagpipes = Bagpipes.create({ pipe: pipe }, { userFittingsDirs: userFittingsDirs });
var context = {};
bagpipes.play(bagpipes.getPipe('pipe'), context, function(err, context) {
context.output.should.eql('test');
done(err);
});
})
});

describe('and the pipe flow fails', function() {
it('should pass error and context to the done', function(done){
var userFittingsDirs = [ path.resolve(__dirname, './fixtures/fittings') ];
var pipe = [ 'test', 'test', 'emit', 'test', 'error' ];
var bagpipes = Bagpipes.create({ pipe: pipe }, { userFittingsDirs: userFittingsDirs });
var context = {};
bagpipes.play(bagpipes.getPipe('pipe'), context, function(err, context) {
should(err).be.an.Error;
context.output.should.eql('test');
done()
});
})
})
})
});