Comparing version 0.0.5 to 0.0.6
@@ -8,3 +8,2 @@ // Generated by CoffeeScript 1.6.3 | ||
inject: {}, | ||
deferral: {}, | ||
schedule: {}, | ||
@@ -11,0 +10,0 @@ match: {}, |
// Generated by CoffeeScript 1.6.3 | ||
var argsOf, defer; | ||
defer = require('when').defer; | ||
argsOf = require('../util').argsOf; | ||
module.exports = function(Preparator, decoratedFn) { | ||
var argPosition; | ||
Preparator || (Preparator = {}); | ||
Preparator.timeout || (Preparator.timeout = 0); | ||
decoratedFn || (decoratedFn = Preparator); | ||
if (typeof decoratedFn !== 'function') { | ||
throw new Error('expected function as last arg'); | ||
} | ||
if (Preparator.resolver != null) { | ||
argPosition = argsOf(decoratedFn).indexOf(Preparator.resolver); | ||
} | ||
return function() { | ||
var arg, deferral, newArgs, reject, resolver, timeout, _i, _len; | ||
deferral = defer(); | ||
Preparator.context = typeof Preparator.context === 'undefined' ? this : Preparator.context; | ||
if (Preparator.resolver != null) { | ||
if (argPosition >= 0) { | ||
if (Preparator.timeout !== 0) { | ||
timeout = setTimeout((function() { | ||
deferral.notify({ | ||
event: 'timeout' | ||
}); | ||
if (Preparator.resolveOnTimeout) { | ||
return deferral.resolve(); | ||
} | ||
return deferral.reject(new Error('timeout')); | ||
}), Preparator.timeout); | ||
} | ||
resolver = function(result) { | ||
if (timeout != null) { | ||
clearTimeout(timeout); | ||
} | ||
return deferral.resolve(result); | ||
}; | ||
reject = function(error) { | ||
if (timeout != null) { | ||
clearTimeout(timeout); | ||
} | ||
return deferral.reject(error); | ||
}; | ||
resolver.resolve = resolver; | ||
resolver.reject = reject; | ||
resolver.notify = deferral.notify; | ||
newArgs = []; | ||
for (_i = 0, _len = arguments.length; _i < _len; _i++) { | ||
arg = arguments[_i]; | ||
newArgs.push(arg); | ||
} | ||
newArgs[argPosition] = resolver; | ||
decoratedFn.apply(Preparator.context, newArgs); | ||
} else { | ||
decoratedFn.apply(Preparator.context, arguments); | ||
deferral.resolve(); | ||
} | ||
return deferral.promise; | ||
} | ||
decoratedFn.apply(Preparator.context, arguments); | ||
deferral.resolve(); | ||
return deferral.promise; | ||
}; | ||
}; |
@@ -21,10 +21,11 @@ // Generated by CoffeeScript 1.6.3 | ||
} | ||
if (Preparator.timeout == null) { | ||
Preparator.timeout = 0; | ||
} | ||
if (Preparator.parallel == null) { | ||
Preparator.parallel = true; | ||
} | ||
if (Preparator.context == null) { | ||
Preparator.context = this; | ||
} | ||
Preparator.onError; | ||
return (function(context, beforeAllDone) { | ||
var beforeAll, calls, queue, queueRemainingLength, running; | ||
var beforeAll, calls, errorHandler, queue, queueRemainingLength, running; | ||
context.signature = argsOf(decoratedFn); | ||
@@ -52,2 +53,15 @@ queue = []; | ||
}; | ||
errorHandler = function(defer, error) { | ||
var done; | ||
if (typeof Preparator.onError !== 'function') { | ||
return defer.reject(error); | ||
} | ||
done = function(result) { | ||
if (result instanceof Error) { | ||
return defer.reject(result); | ||
} | ||
return defer.resolve(result); | ||
}; | ||
return Preparator.onError(done, context, error); | ||
}; | ||
beforeAll = function() { | ||
@@ -65,3 +79,3 @@ var defer, done; | ||
if (result instanceof Error) { | ||
return defer.reject(result); | ||
return errorHandler(defer, result); | ||
} | ||
@@ -122,2 +136,10 @@ return defer.resolve(result); | ||
}); | ||
Object.defineProperty(context, 'skip', { | ||
enumerable: true, | ||
get: function() { | ||
return function() { | ||
return queue[_id].skip = true; | ||
}; | ||
} | ||
}); | ||
return function() { | ||
@@ -127,3 +149,3 @@ var finished, fn, run; | ||
fn = function(finished, args) { | ||
var afterAll, afterEach, arg, beforeEach, callDecoratedFn, fnDone, id, inject, _i, _len; | ||
var afterAll, afterEach, arg, beforeEach, callDecoratedFn, id, inject, resolver, _i, _len; | ||
id = seq++; | ||
@@ -135,7 +157,4 @@ inject = []; | ||
} | ||
fnDone = function(result) { | ||
resolver = function(result) { | ||
_id = id; | ||
if (Preparator.timeout != null) { | ||
clearTimeout(queue[id].timer); | ||
} | ||
if (result instanceof Error) { | ||
@@ -151,3 +170,2 @@ return queue[id].defer.reject(result); | ||
done: false, | ||
timeout: false, | ||
defer: Defer(), | ||
@@ -159,13 +177,2 @@ altDefer: false, | ||
}; | ||
if (Preparator.timeout !== 0) { | ||
queue[id].timer = setTimeout((function() { | ||
if (typeof Preparator.onTimeout === 'function') { | ||
return Preparator.onTimeout(fnDone, { | ||
type: 'fn' | ||
}, context); | ||
} else { | ||
return fnDone(); | ||
} | ||
}), Preparator.timeout); | ||
} | ||
beforeEach = function() { | ||
@@ -178,3 +185,2 @@ var defer, done; | ||
done = function(result) { | ||
_id = id; | ||
finished.notify({ | ||
@@ -184,3 +190,3 @@ beforeEach: result | ||
if (result instanceof Error) { | ||
return defer.reject(result); | ||
return errorHandler(defer, result); | ||
} | ||
@@ -194,9 +200,22 @@ return defer.resolve(result); | ||
callDecoratedFn = function() { | ||
var element; | ||
_id = id; | ||
if (queue[id].altDefer) { | ||
decoratedFn.apply(this, queue[id].first.concat(inject).concat(queue[id].last)); | ||
} else { | ||
decoratedFn.apply(this, [fnDone].concat(queue[id].first.concat(inject).concat(queue[id].last))); | ||
element = queue[id]; | ||
if (element.skip) { | ||
return element.defer.resolve(); | ||
} | ||
return queue[id].defer.promise; | ||
process.nextTick(function() { | ||
var error; | ||
try { | ||
if (element.altDefer) { | ||
return decoratedFn.apply(Preparator.context, element.first.concat(inject).concat(element.last)); | ||
} else { | ||
return decoratedFn.apply(Preparator.context, [resolver].concat(element.first.concat(inject).concat(element.last))); | ||
} | ||
} catch (_error) { | ||
error = _error; | ||
return errorHandler(element.defer, error); | ||
} | ||
}); | ||
return element.defer.promise; | ||
}; | ||
@@ -211,3 +230,2 @@ afterEach = function() { | ||
done = function(result) { | ||
_id = id; | ||
finished.notify({ | ||
@@ -217,3 +235,3 @@ afterEach: result | ||
if (result instanceof Error) { | ||
return defer.reject(result); | ||
return errorHandler(defer, result); | ||
} | ||
@@ -230,5 +248,2 @@ return defer.resolve(result); | ||
queue[id].done = true; | ||
if (Preparator.timeout != null) { | ||
clearTimeout(queue[id].timer); | ||
} | ||
if (queueRemainingLength() !== 0) { | ||
@@ -245,3 +260,3 @@ return defer.resolve(); | ||
if (result instanceof Error) { | ||
return defer.reject(result); | ||
return errorHandler(defer, result); | ||
} | ||
@@ -257,12 +272,4 @@ return defer.resolve(result); | ||
}, function(error) { | ||
_id = id; | ||
if (Preparator.onError instanceof Function) { | ||
Preparator.onError(error, context); | ||
} | ||
return finished.reject(error); | ||
}, function(status) { | ||
_id = id; | ||
if (Preparator.onNotify instanceof Function) { | ||
Preparator.onNotify(status, context); | ||
} | ||
return finished.notify(status); | ||
@@ -269,0 +276,0 @@ }); |
{ | ||
"name": "also", | ||
"description": "decorators", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"author": "nomilous <nomilous@gmail.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -1,2 +0,2 @@ | ||
### Version 0.0.5 (unstable) | ||
### Version 0.0.6 (unstable) | ||
@@ -3,0 +3,0 @@ `npm install also` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
78308
519