middlewarify
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -33,2 +33,3 @@ /** | ||
middObj.mainCallback = noopMidd; | ||
middObj.mainCallback.isMain = true; | ||
@@ -46,2 +47,3 @@ /** | ||
middObj.mainCallback = optFinalCb; | ||
middObj.mainCallback.isMain = true; | ||
} | ||
@@ -93,3 +95,5 @@ | ||
} | ||
middlewarify._fetchAndInvoke(midds, args, deferred); | ||
middlewarify._fetchAndInvoke(midds, args, deferred, { | ||
mainCallbackReturnValue: null, | ||
}); | ||
}); | ||
@@ -104,12 +108,16 @@ }; | ||
* @param {Promise.Defer} deferred Deferred object. | ||
* @param {Object} store A map, helper for main cb return value. | ||
* @private | ||
*/ | ||
middlewarify._fetchAndInvoke = function(midds, args, deferred) { | ||
middlewarify._fetchAndInvoke = function(midds, args, deferred, store) { | ||
if (midds.length === 0) { | ||
return deferred.resolve(); | ||
return deferred.resolve(store.mainCallbackReturnValue); | ||
} | ||
var midd = midds.shift(); | ||
middlewarify._invoke(midd, args).then(function() { | ||
middlewarify._fetchAndInvoke(midds, args, deferred); | ||
middlewarify._invoke(midd, args).then(function(val) { | ||
if (midd.isMain) { | ||
store.mainCallbackReturnValue = val; | ||
} | ||
middlewarify._fetchAndInvoke(midds, args, deferred, store); | ||
}, deferred.reject.bind(deferred)); | ||
@@ -130,3 +138,3 @@ }; | ||
if (!maybePromise || typeof maybePromise.then !== 'function') { | ||
resolve(); | ||
resolve(maybePromise); | ||
} else { | ||
@@ -133,0 +141,0 @@ maybePromise.then(resolve, reject); |
{ | ||
"name": "middlewarify", | ||
"description": "Apply the middleware pattern to any function.", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"homepage": "https://github.com/thanpolas/middlewarify", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -31,2 +31,3 @@ # Middlewarify | ||
console.log('createTask Final Fn to be invoked'); | ||
return 'a value'; | ||
} | ||
@@ -67,3 +68,3 @@ | ||
// ... Invoking them all together | ||
tasks.create(); | ||
tasks.create() | ||
// prints | ||
@@ -73,2 +74,6 @@ // middleware 1 | ||
// createTask Final Fn to be invoked | ||
.then(function(val) { | ||
console.log(val); | ||
// prints: "a value" | ||
}); | ||
``` | ||
@@ -100,2 +105,3 @@ | ||
console.log('Invoked Second'); | ||
return 999; | ||
}; | ||
@@ -121,4 +127,5 @@ | ||
// invoke all middleware | ||
tasks.create().then(function(){ | ||
tasks.create().then(function(val){ | ||
// at this point all middleware have finished. | ||
console.log(val); // 999 | ||
}, function(err) { | ||
@@ -144,4 +151,11 @@ // handle error | ||
You can pass a third argument, the `optMainCallback`, a Function. This can be considered the main payload of your middleware. | ||
You can pass a third argument, the `optMainCallback`, a Function. This will be the *Main* callback of your middleware, the result returned, or resolved if a promise is used, will get passed to the final promise: | ||
```js | ||
crud.create().then(function(val) { | ||
// this is the final promise. | ||
// val is passed from the Main callback. | ||
}); | ||
``` | ||
`optOptions` defines behavior. Both `optOptions` and `optMainCallback` are optional and can be interswitched, i.e. you can pass options as a third argument, read on for examples and what are the available options. | ||
@@ -248,2 +262,4 @@ | ||
## Release History | ||
- **v0.3.1**, *09 Feb 2014* | ||
- Main Callback now passes value to final promise. | ||
- **v0.3.0**, *09 Feb 2014* | ||
@@ -250,0 +266,0 @@ - Removed callback API, 100% Promise based API now. |
@@ -95,2 +95,8 @@ /** | ||
setup(function() { | ||
}); | ||
teardown(function(){ | ||
}); | ||
test('6.3.1 Three arguments', function(done) { | ||
obj = Object.create(null); | ||
@@ -104,8 +110,3 @@ mainMidd = sinon.spy(); | ||
obj.create.after(thirdMidd); | ||
}); | ||
teardown(function(){ | ||
}); | ||
test('6.3.1 Three arguments', function(done) { | ||
var foo = {a: 1}; | ||
@@ -122,2 +123,38 @@ var bar = {b: 2}; | ||
}); | ||
suite('6.3.2 Main Callback arguments pipes to final promise', function() { | ||
function invoke(returnValue) { | ||
obj = Object.create(null); | ||
mainMidd = function() { | ||
return returnValue; | ||
}; | ||
firstMidd = sinon.spy(); | ||
secondMidd = sinon.spy(); | ||
thirdMidd = sinon.spy(); | ||
midd.make(obj, 'create', mainMidd, {beforeAfter: true}); | ||
obj.create.before(firstMidd, secondMidd); | ||
obj.create.after(thirdMidd); | ||
} | ||
test('6.3.2.1 Using a promise', function(done) { | ||
var prom = new Promise(function(resolve){ | ||
resolve('value'); | ||
}); | ||
invoke(prom); | ||
obj.create().then(function(val) { | ||
assert.equal(val, 'value'); | ||
}).then(done, done); | ||
}); | ||
test('6.3.2.2 Using a string', function(done) { | ||
invoke('value'); | ||
obj.create().then(function(val) { | ||
assert.equal(val, 'value'); | ||
}).then(done, done); | ||
}); | ||
test('6.3.2.3 Using a number', function(done) { | ||
invoke(9); | ||
obj.create().then(function(val) { | ||
assert.equal(val, 9); | ||
}).then(done, done); | ||
}); | ||
}); | ||
}); | ||
@@ -124,0 +161,0 @@ |
@@ -176,3 +176,3 @@ /** | ||
function invoke(returnValue, done) { | ||
var lastMidd = function() {return returnValue;}; | ||
var lastMidd = function() {}; | ||
var firstMidd = function() {return returnValue;}; | ||
@@ -250,1 +250,37 @@ var obj = Object.create(null); | ||
suite('3.5.2 Main Callback arguments pipes to final promise', function() { | ||
var obj; | ||
function invoke(returnValue) { | ||
obj = Object.create(null); | ||
var mainMidd = function() { | ||
return returnValue; | ||
}; | ||
var firstMidd = sinon.spy(); | ||
var secondMidd = sinon.spy(); | ||
var thirdMidd = sinon.spy(); | ||
midd.make(obj, 'create', mainMidd); | ||
obj.create.use(firstMidd, secondMidd, thirdMidd); | ||
} | ||
test('3.5.2.1 Using a promise', function(done) { | ||
var prom = new Promise(function(resolve){ | ||
resolve('value'); | ||
}); | ||
invoke(prom); | ||
obj.create().then(function(val) { | ||
assert.equal(val, 'value'); | ||
}).then(done, done); | ||
}); | ||
test('3.5.2.2 Using a string', function(done) { | ||
invoke('value'); | ||
obj.create().then(function(val) { | ||
assert.equal(val, 'value'); | ||
}).then(done, done); | ||
}); | ||
test('3.5.2.3 Using a number', function(done) { | ||
invoke(9); | ||
obj.create().then(function(val) { | ||
assert.equal(val, 9); | ||
}).then(done, done); | ||
}); | ||
}); |
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
39027
793
283