middlewarify
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -37,2 +37,3 @@ /** | ||
* @param {Function=} optCb last argument is callback | ||
* @return {Object} the -master- callback using done(fn). | ||
* @private | ||
@@ -43,10 +44,11 @@ */ | ||
var len = args.length; | ||
var doneArgs; | ||
var isDone = false; | ||
var doneActual = noop; | ||
var done = function() { | ||
isDone = true; | ||
doneArgs = arguments; | ||
doneActual.apply(null, arguments); | ||
}; | ||
var done = noop; | ||
if (__.isFunction(args[len - 1])) { | ||
done = args.pop(); | ||
} | ||
var midds = Array.prototype.slice.call(middObj.midds, 0); | ||
@@ -56,2 +58,10 @@ midds.push(middObj.finalMidd); | ||
middlewarify._fetchAndInvoke(midds, args, done); | ||
return {done: function(fn) { | ||
if (isDone) { | ||
fn.apply(null, doneArgs); | ||
} else { | ||
doneActual = fn; | ||
} | ||
}}; | ||
}; | ||
@@ -58,0 +68,0 @@ |
{ | ||
"name": "middlewarify", | ||
"description": "Apply the middleware pattern to any function.", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"homepage": "https://github.com/thanpolas/middlewarify", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -56,6 +56,11 @@ # Middlewarify | ||
// ... Invoking them all together | ||
tasks.create(function(err){ | ||
tasks.create(); | ||
``` | ||
Invoking the middleware will return an object with a `done` property which you can use to setup your callbacks: | ||
```js | ||
tasks.create().done(function(err) { | ||
// all middleware finished. | ||
}); | ||
``` | ||
@@ -121,3 +126,3 @@ | ||
// run all middleware | ||
crud.create(userDataObject, function(err, arg1, arg2) { | ||
crud.create(userDataObject).done(function(err, arg1, arg2) { | ||
if (err) { /* tough love */ } | ||
@@ -146,2 +151,4 @@ | ||
## Release History | ||
- **v0.0.3**, *02 Aug 2013* | ||
- Added a more explicit way to declare callbacks when invoking the middleware. | ||
- **v0.0.2**, *15 JuL 2013* | ||
@@ -148,0 +155,0 @@ - Big Bang |
11
test.js
@@ -29,2 +29,3 @@ /** | ||
assert.isFunction(obj.create.use, 'obj.create.use should be a function'); | ||
assert.isFunction(obj.create().done, 'obj.create().done should be a function'); | ||
}); | ||
@@ -103,3 +104,3 @@ }); | ||
var bar = {b: 2}; | ||
obj.create(1, foo, bar, function(err){ | ||
obj.create(1, foo, bar).done(function(err){ | ||
assert.notOk(err, 'error should not be truthy'); | ||
@@ -123,3 +124,3 @@ assert.ok(firstMidd.alwaysCalledWith(1, foo, bar), 'firstMidd should be invoked with these arguments'); | ||
obj.create(function(err, arg1, arg2){ | ||
obj.create().done(function(err, arg1, arg2) { | ||
assert.equal(1, arg1, 'Arg1 should be 1'); | ||
@@ -143,3 +144,3 @@ assert.equal(2, arg2, 'Arg2 should be 2'); | ||
}); | ||
obj.create(function(err){ | ||
obj.create().done(function(err){ | ||
assert.instanceOf(err, Error, '"err" should be instanceOf Error'); | ||
@@ -154,3 +155,3 @@ assert.equal(err.message, 'an error', 'Error message should match'); | ||
}); | ||
obj.create(function(err){ | ||
obj.create().done(function(err){ | ||
assert.instanceOf(err, Error, '"err" should be instanceOf Error'); | ||
@@ -169,3 +170,3 @@ assert.equal(err.message, 'an error', 'Error message should match'); | ||
obj.create(function(){ | ||
obj.create().done(function(){ | ||
assert.notOk(middSpy.called, 'second middleware should not be called'); | ||
@@ -172,0 +173,0 @@ }); |
18281
288
165