Comparing version 2.0.2 to 2.1.0
21
index.js
@@ -114,2 +114,3 @@ var is = require('./is') | ||
if (is.function(resolve)) { | ||
// resolver callback | ||
return function (err, result) { | ||
@@ -126,11 +127,17 @@ if (err) return next(err) | ||
ctx.emit.apply(ctx, args) | ||
return | ||
} | ||
if (index < size) { | ||
} else if (index < size) { | ||
var fn = pipe[index] | ||
index++ | ||
fn.call(self, ctx, next) | ||
// args without next() | ||
if (fn.length < 2) next() | ||
var val = fn.call(self, ctx, next) | ||
if (val && is.function(val.then)) { | ||
// thenable: handle promise | ||
val.then(function (res) { | ||
if (res === undefined) next() // next | ||
else next(null, res) // result | ||
}, next) | ||
} else if (fn.length < 2) { | ||
// args without next() | ||
if (val === undefined) next() // no return, next | ||
else next(null, val) // result | ||
} | ||
} else { | ||
@@ -137,0 +144,0 @@ // trigger empty callback if no more pipe |
{ | ||
"name": "ginga", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Middleware framework for JavaScript functions", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -5,4 +5,4 @@ var tape = require('tape') | ||
function invoke (ctx, done) { | ||
return done(null, ctx.params) | ||
function invoke (ctx) { | ||
return ctx.params | ||
} | ||
@@ -31,6 +31,3 @@ var obj = ginga() | ||
t.deepEqual(res, { a: '1', b: '2' }, 'promise resolve') | ||
}) | ||
.catch(function (err) { | ||
t.error(err, 'no promise error') | ||
}) | ||
}).catch(t.error) | ||
@@ -40,11 +37,6 @@ obj.f1('1', 167) | ||
t.deepEqual(res, { a: '1', c: 167 }, 'promise resolve') | ||
}) | ||
.catch(function (err) { | ||
t.error(err, 'no promise error') | ||
}) | ||
}).catch(t.error) | ||
obj.f2('1') | ||
.then(function (res) { | ||
t.error(res, 'error no resolve') | ||
}) | ||
.then(t.error) | ||
.catch(function (err) { | ||
@@ -51,0 +43,0 @@ t.equal(err.message, 'Too few arguments. Expected at least 2') |
var tape = require('tape') | ||
var ginga = require('../') | ||
var Promise = require('pinkie-promise') | ||
tape('ginga prototype', function (t) { | ||
t.plan(12) | ||
t.plan(10) | ||
@@ -20,3 +21,3 @@ function Clock () { | ||
next(function (result) { | ||
t.equal(result, 167199, 'resolver result') | ||
t.equal(result, 167199, 'callback resolver') | ||
})(null, 167199) | ||
@@ -28,5 +29,10 @@ } | ||
} | ||
function end (ctx, done) { | ||
function end (ctx) { | ||
ctx.logs.push('done') | ||
done(null, ctx.logs) | ||
// then result | ||
return new Promise(function (resolve) { | ||
setTimeout(function () { | ||
resolve(ctx.logs) | ||
}, 10) | ||
}) | ||
} | ||
@@ -38,20 +44,17 @@ var C = ginga(Clock.prototype) | ||
clock2.use( | ||
'tick', | ||
function (ctx) { | ||
ctx.logs.push('more') | ||
}, | ||
function (ctx) { | ||
ctx.logs.push('and more tick') | ||
} | ||
) | ||
clock2.use( | ||
'tock', | ||
function (ctx, next) { | ||
// resolver function err | ||
next(function (res) { | ||
t.error('resolver called') | ||
})('booooom') | ||
} | ||
) | ||
clock2.use('tick', function (ctx) { | ||
// return thenable | ||
return new Promise(function (resolve) { | ||
setTimeout(function () { | ||
ctx.logs.push('more') | ||
resolve() // no value, should do next | ||
}, 10) | ||
}) | ||
}, function (ctx) { | ||
ctx.logs.push('and more tick') | ||
}) | ||
clock2.use('tock', function (ctx, next) { | ||
// resolver callback err | ||
next(t.error)('booooom') | ||
}) | ||
@@ -67,6 +70,6 @@ C.define('tick', end) | ||
}) | ||
clock1.tock(function (err, res) { | ||
t.notOk(err, 'no error') | ||
clock1.tock().then(function (res) { | ||
t.deepEqual(res, ['clock', 'tick', 'tock', 'done']) | ||
}) | ||
}).catch(t.error) | ||
clock2.tick(function (err, res) { | ||
@@ -76,6 +79,5 @@ t.notOk(err, 'no error') | ||
}) | ||
clock2.tock(function (err, res) { | ||
t.notOk(res, 'no result') | ||
clock2.tock().then(t.error).catch(function (err) { | ||
t.equal(err, 'booooom', 'return error') | ||
}) | ||
}) |
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
18940
463