mock-promises
Advanced tools
Comparing version 0.0.3 to 0.1.0
@@ -45,5 +45,5 @@ (function() { | ||
function MockPromise(){ | ||
MockPromise = function MockPromise(){ | ||
return this.initialize.apply(this, arguments); | ||
} | ||
}; | ||
@@ -137,2 +137,3 @@ MockPromise.resolve = function(value) { | ||
var promise = this; | ||
var thenPromise = new MockPromise(function(){}); | ||
contractsTracker.add({ | ||
@@ -142,5 +143,6 @@ promise: promise, | ||
errorHandler: arguments[1], | ||
progressHandler: arguments[2] | ||
progressHandler: arguments[2], | ||
thenPromise: thenPromise | ||
}); | ||
return promise; | ||
return thenPromise; | ||
}; | ||
@@ -173,4 +175,17 @@ | ||
this.progressHandler = options.progressHandler; | ||
this.thenPromise = options.thenPromise | ||
}; | ||
function resolvePromise(promise, value) { | ||
promise.isFulfilled = function(){return true;}; | ||
promise.isRejected = function(){return false;}; | ||
promise.inspect = function() {return {value: value};} | ||
} | ||
function rejectPromise(promise, reason) { | ||
promise.isFulfilled = function(){return false;}; | ||
promise.isRejected = function(){return true;}; | ||
promise.inspect = function() {return {reason: reason};} | ||
} | ||
extend(Contract.prototype, { | ||
@@ -180,10 +195,32 @@ execute: function() { | ||
this._executed = true; | ||
var value, nextValue, handler; | ||
if(this.promise.isFulfilled()) { | ||
this.fulfilledHandler.call(this.promise, this.promise.inspect().value); | ||
value = this.promise.inspect().value; | ||
handler = this.fulfilledHandler; | ||
if(handler) { | ||
try { | ||
nextValue = handler.call(this.promise, value); | ||
resolvePromise(this.thenPromise, nextValue); | ||
} catch(e) { | ||
rejectPromise(this.thenPromise, e) | ||
} | ||
} else { | ||
resolvePromise(this.thenPromise, value); | ||
} | ||
} else if(this.promise.isRejected()) { | ||
this.errorHandler.call(this.promise, this.promise.inspect().reason); | ||
value = this.promise.inspect().reason; | ||
handler = this.errorHandler; | ||
if(handler) { | ||
try { | ||
nextValue = handler.call(this.promise, value); | ||
resolvePromise(this.thenPromise, nextValue); | ||
} catch(e) { | ||
rejectPromise(this.thenPromise, e) | ||
} | ||
} else { | ||
rejectPromise(this.thenPromise, value); | ||
} | ||
} else { | ||
throw new Error("this promise is not resolved and should not be executed"); | ||
} | ||
} | ||
@@ -245,3 +282,3 @@ } | ||
var resolvedContracts = this.filter(function(contract) { | ||
return contract.promise.isFulfilled(); | ||
return contract.promise.isFulfilled() || contract.promise.isRejected(); | ||
}); | ||
@@ -248,0 +285,0 @@ each(resolvedContracts, function(contract) { |
{ | ||
"name": "mock-promises", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"author": "Charles Hansen <chansen87@gmail.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15611
276
0