middlewarify
Advanced tools
Comparing version 0.3.8 to 0.4.0
@@ -132,18 +132,31 @@ /** | ||
* @param {Object} deferred contains resolve, reject fns. | ||
* @param {boolean=} optAfter If next middleware is after the main callback. | ||
* @return {Promise} A promise. | ||
* @private | ||
*/ | ||
middlewarify._fetchAndInvoke = function(midds, args, store, deferred) { | ||
middlewarify._fetchAndInvoke = function(midds, args, store, deferred, optAfter) { | ||
if (!midds.length) { | ||
return deferred.resolve(store.mainCallbackReturnValue); | ||
} | ||
var isAfter = !!optAfter; | ||
var midd = midds.shift(); | ||
Promise.try(midd, args) | ||
.then(function(val) { | ||
// check for return value and after-main CB | ||
// if pass then replace the main callback return value with the one | ||
// provided | ||
if (isAfter && typeof val !== 'undefined') { | ||
store.mainCallbackReturnValue = val; | ||
args.splice(-1, 1, val); | ||
} | ||
if (midd.isMain) { | ||
store.mainCallbackReturnValue = val; | ||
args.push(val); | ||
isAfter = true; | ||
} | ||
middlewarify._fetchAndInvoke(midds, args, store, deferred); | ||
middlewarify._fetchAndInvoke(midds, args, store, deferred, isAfter); | ||
}) | ||
@@ -150,0 +163,0 @@ .catch(function(err) { |
{ | ||
"name": "middlewarify", | ||
"description": "Apply the middleware pattern to any function.", | ||
"version": "0.3.8", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/thanpolas/middlewarify", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -139,6 +139,6 @@ # Middlewarify | ||
tasks.create().then(function(val){ | ||
// at this point all middleware have finished. | ||
console.log(val); // 999 | ||
// at this point all middleware have finished. | ||
console.log(val); // 999 | ||
}, function(err) { | ||
// handle error | ||
// handle error | ||
}); | ||
@@ -272,3 +272,3 @@ ``` | ||
#### After Hooks get the Result too | ||
#### After & Last Hooks get the Result | ||
@@ -289,5 +289,25 @@ If your middleware if a Before / After type, then all `.after()` hooks will receive an extra argument representing the resolving value. | ||
#### After & Last Hooks can alter the Result | ||
All After & Last hooks may alter the result as long as they return any type of value except `undefined`. | ||
```js | ||
middlewarify.make(crud, 'create', function() { | ||
return 'abc'; | ||
}); | ||
crud.create.after(function(result) { | ||
// return an altered outcome | ||
return 'def'; | ||
}); | ||
crud.create().then(function(result) { | ||
console.log(result); // prints "def" | ||
}); | ||
``` | ||
## Release History | ||
- **v0.4.0**, *25 Jul 2014* | ||
- Now After & Last middlewares may alter the result value by returning a non undefined value. | ||
- **v0.3.8**, *24 Jul 2014* | ||
@@ -294,0 +314,0 @@ - Implemented `.last()` middleware type in beforeAfter family. |
@@ -12,3 +12,3 @@ /** | ||
suite('6. Before / After middleware', function() { | ||
suite('6. Before / After / Last middleware', function() { | ||
@@ -320,1 +320,64 @@ setup(function() {}); | ||
}); | ||
suite('6.8 Resolving Value can be altered by After & Last middleware', function() { | ||
var obj; | ||
setup(function(){ | ||
obj = Object.create(null); | ||
}); | ||
test('6.8.1 After middleware can alter outcome ASYNC', function(done) { | ||
midd.make(obj, 'create', function() { | ||
return 'abc'; | ||
},{beforeAfter: true}); | ||
obj.create.after(function() { | ||
return Promise.resolve('def'); | ||
}); | ||
obj.create.after(function(resolveValue) { | ||
assert.equal(resolveValue, 'def'); | ||
}); | ||
obj.create().then(done.bind(null, null), done); | ||
}); | ||
test('6.8.2 After middleware can alter outcome SYNC', function(done) { | ||
midd.make(obj, 'create', function() { | ||
return 'abc'; | ||
},{beforeAfter: true}); | ||
obj.create.after(function() { | ||
return 'def'; | ||
}); | ||
obj.create.after(function(resolveValue) { | ||
assert.equal(resolveValue, 'def'); | ||
}); | ||
obj.create().then(done.bind(null, null), done); | ||
}); | ||
test('6.8.3 Last middleware can alter outcome ASYNC', function(done) { | ||
midd.make(obj, 'create', function() { | ||
return 'abc'; | ||
},{beforeAfter: true}); | ||
obj.create.last(function() { | ||
return Promise.resolve('def'); | ||
}); | ||
obj.create.last(function(resolveValue) { | ||
assert.equal(resolveValue, 'def'); | ||
}); | ||
obj.create().then(done.bind(null, null), done); | ||
}); | ||
test('6.8.4 Last middleware can alter outcome SYNC', function(done) { | ||
midd.make(obj, 'create', function() { | ||
return 'abc'; | ||
},{beforeAfter: true}); | ||
obj.create.last(function() { | ||
return 'def'; | ||
}); | ||
obj.create.last(function(resolveValue) { | ||
assert.equal(resolveValue, 'def'); | ||
}); | ||
obj.create().then(done.bind(null, null), 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
47944
996
350