Comparing version 3.0.1 to 3.0.2
16
index.js
@@ -7,2 +7,3 @@ var is = require('./is') | ||
// wrap ginga middleware function | ||
function wrapFn (gen) { | ||
@@ -15,3 +16,3 @@ if (!is.function(gen)) throw new Error('Middleware must be a function') | ||
return function (ctx, next) { | ||
fn.apply(this, arguments) | ||
fn.call(this, ctx, next) | ||
} | ||
@@ -123,6 +124,6 @@ } | ||
if (err || index === size) { | ||
var args = Array.prototype.slice.call(arguments) | ||
// callback when err or end of pipeline | ||
if (callback) callback.apply(self, arguments) | ||
var args = ['end'] | ||
Array.prototype.push.apply(args, arguments) | ||
if (callback) callback.apply(self, args) | ||
args.unshift('end') | ||
ctx.emit.apply(ctx, args) | ||
@@ -133,11 +134,10 @@ } else if (index < size) { | ||
var val = fn.call(self, ctx, next) | ||
if (val && is.function(val.then)) { | ||
// thenable | ||
if (is.promise(val)) { | ||
val.then(function (res) { | ||
next(null, res) | ||
}, function (err) { | ||
next(err || true) | ||
next(err || new Error()) | ||
}) | ||
} else if (fn.length < 2) { | ||
// args without next(), not thenable | ||
// args without next() & not promise, sync func | ||
next(null, val) | ||
@@ -144,0 +144,0 @@ } |
@@ -27,2 +27,5 @@ var is = module.exports | ||
} | ||
is.promise = function (val) { | ||
return val && typeof val.then === 'function' | ||
} | ||
is.generator = require('is-generator-function') |
{ | ||
"name": "ginga", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Middleware framework for async functions using callback, promise or generator", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -103,3 +103,3 @@ # Ginga.js | ||
In ES6 generators, functions can be paused and resumed using the `yield` keyword. | ||
Both promise and callback are 'yieldable' in ginga middleware. | ||
Using [caco](https://github.com/cshum/caco), both promise and callback are 'yieldable' in ginga middleware. | ||
This enables powerful control flow while maintaining compatibility. | ||
@@ -143,4 +143,4 @@ | ||
//define method with params parsing | ||
app.define('test', params('a', 'b:number?', 'c:string?'), function (ctx, done) { | ||
done(null, ctx.params) | ||
app.define('test', params('a', 'b:number?', 'c:string?'), function (ctx) { | ||
return ctx.params | ||
}) | ||
@@ -171,8 +171,7 @@ | ||
var app = ginga() | ||
app.define('test', function (ctx, next) { | ||
app.define('test', function (ctx) { | ||
ctx.logs = ['pre'] | ||
next() | ||
}, function (ctx, done) { | ||
}, function (ctx) { | ||
ctx.logs.push('invoke') | ||
done(null, ctx.logs) | ||
return ctx.logs | ||
}) | ||
@@ -217,15 +216,12 @@ | ||
//prototype hook | ||
A.use('test', function (ctx, next) { | ||
A.use('test', function (ctx) { | ||
ctx.logs.push('A hook') | ||
next() | ||
}) | ||
//instance hook | ||
a1.use('test', function (ctx, next) { | ||
a1.use('test', function (ctx) { | ||
ctx.logs.push('a1 hook') | ||
next() | ||
}) | ||
a2.use('test', function (ctx, next) { | ||
a2.use('test', function (ctx) { | ||
ctx.logs.push('a2 hook') | ||
next() | ||
}) | ||
@@ -232,0 +228,0 @@ |
var tape = require('tape') | ||
var ginga = require('../') | ||
var Promise = require('pinkie-promise') | ||
@@ -5,0 +4,0 @@ tape('ginga prototype', function (t) { |
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
21205
14
239