concurrent
Advanced tools
Comparing version 0.2.2 to 0.3.0
{ | ||
"name": "concurrent", | ||
"main": "browser/concurrent.js", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/pspeter3/concurrent", | ||
@@ -6,0 +6,0 @@ "authors": [ |
@@ -201,4 +201,29 @@ /** | ||
var ValidationError = function(value) { | ||
Error.captureStackTrace(this, this); | ||
this.value = value; | ||
this.message = '' + this.value + ' did not match predicate'; | ||
}; | ||
util.inherits(ValidationError, Error); | ||
ValidationError.prototype.name = 'ValidationError'; | ||
var TimeoutError = function(time) { | ||
Error.captureStackTrace(this, this); | ||
this.time = time; | ||
this.message = 'Future did not resolve in ' + this.time + ' ms'; | ||
}; | ||
util.inherits(TimeoutError, Error); | ||
TimeoutError.prototype.name = 'TimeoutError'; | ||
/** | ||
@@ -243,3 +268,3 @@ * Future | ||
* @param {Array} keys The keys for the callback | ||
* | ||
* | ||
* @return {Function} The callback for async | ||
@@ -286,3 +311,7 @@ */ | ||
setTimeout(function() { | ||
future.reject(new Error('Did not resolve future in ' + duration + ' ms')); | ||
try { | ||
throw new TimeoutError(duration); | ||
} catch (err) { | ||
future.reject(err); | ||
} | ||
}, duration); | ||
@@ -300,13 +329,5 @@ | ||
Future.prototype.fallbackTo = function(promise) { | ||
var future = new this.constructor(); | ||
this.then(function(value) { | ||
future.fulfill(value); | ||
}, function() { | ||
promise.then(function(value) { | ||
future.fulfill(value); | ||
}); | ||
return this.then(null, function(reason) { | ||
return promise; | ||
}); | ||
return future; | ||
}; | ||
@@ -321,15 +342,9 @@ | ||
Future.prototype.filter = function(predicate) { | ||
var future = new this.constructor(); | ||
this.then(function(value) { | ||
if (predicate(value)) { | ||
return future.fulfill(value); | ||
return this.then(function(value) { | ||
if (!predicate(value)) { | ||
throw new ValidationError(value); | ||
} | ||
return future.reject(new Error('Value does not match predicate')); | ||
}, function(reason) { | ||
future.reject(reason); | ||
return value; | ||
}); | ||
return future; | ||
}; | ||
@@ -384,2 +399,12 @@ | ||
/** | ||
* Recovers a future with a function that returns a promise. | ||
* | ||
* @param {Function} callback A function returning a promise | ||
* @return {Future} The new future | ||
*/ | ||
Future.prototype.recoverWith = function(callback) { | ||
return this.then(null, callback); | ||
}; | ||
/** | ||
* Alias for then | ||
@@ -400,11 +425,7 @@ * | ||
Future.prototype.zip = function(promise) { | ||
var future = new this.constructor(); | ||
this.then(function(left) { | ||
promise.then(function(right) { | ||
future.fulfill([left, right]); | ||
return this.then(function(left) { | ||
return promise.then(function(right) { | ||
return [left, right]; | ||
}); | ||
}); | ||
return future; | ||
}; | ||
@@ -414,3 +435,3 @@ | ||
* Sequences a list of futures together | ||
* | ||
* | ||
* @param {Object} tasks Either an object or an array of tasks | ||
@@ -434,3 +455,3 @@ * @return {Future} The future of all the results | ||
}; | ||
if (util.isArray(tasks)) { | ||
@@ -447,9 +468,32 @@ results = []; | ||
} | ||
return future; | ||
}; | ||
/** | ||
* Creates a future fulfilled with a value | ||
* | ||
* @param {Object} value The value of the future to be fulfilled with | ||
* @return {Future} The fulfilled future | ||
*/ | ||
Future.fulfilled = function(value) { | ||
var future = new Future(); | ||
future.fulfill(value); | ||
return future; | ||
}; | ||
/** | ||
* Creates a future rejected with a reason | ||
* | ||
* @param {Object} reason The reason the future was rejected | ||
* @return {Future} The rejected future | ||
*/ | ||
Future.rejected = function(reason) { | ||
var future = new Future(); | ||
future.reject(reason); | ||
return future; | ||
}; | ||
var collections = {}; | ||
@@ -456,0 +500,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(a,b){"function"==typeof define&&define.amd?define(b):"object"==typeof exports?module.exports=b():a.concurrent=b()}(this,function(){var a="function"==typeof setImmediate?setImmediate:function(a){setTimeout(a,0)},b={isArray:function(a){return Array.isArray(a)},inherits:function(a,b){a.super_=b,a.prototype=Object.create(b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}})}},c={PENDING:"pending",FULFILLED:"fulfilled",REJECTED:"rejected"},d=function(b){var c;c=function(){var d=b.callbacks.shift();d&&d[b.state]&&d[b.state](b.value),b.callbacks.length>0&&a(c)},a(c)},e=function(a,b,e){return a.state===c.FULFILLED||a.state===c.REJECTED?a.state:(a.state=b,a.value=e,d(a),a.state)},f=function(a,b,c){return function(d){if("function"!=typeof c)return e(a,b,d);try{var f=c(d);return f&&"function"==typeof f.then?(f.then(function(b){a.fulfill(b)},function(b){a.reject(b)}),a.state):a.fulfill(f)}catch(g){return a.reject(g)}}},g=function(){this.state=c.PENDING,this.callbacks=[]};g.prototype.then=function(a,b){var e=new this.constructor,g={};return g[c.FULFILLED]=f(e,c.FULFILLED,a),g[c.REJECTED]=f(e,c.REJECTED,b),this.callbacks.push(g),this.state!==c.PENDING&&d(this),e},g.prototype.fulfill=function(a){e(this,c.FULFILLED,a)},g.prototype.reject=function(a){e(this,c.REJECTED,a)};var h=function(){g.call(this)};b.inherits(h,g),h.prototype.isCompleted=function(){return this.state!==c.PENDING},h.prototype.onComplete=function(a){this.then(function(b){a(b)},function(b){a(b)})},h.prototype.convert=function(a){var c=this;return function(){var d=Array.prototype.slice.call(arguments),e=d.shift();return e?c.reject(e):(1===d.length&&(d=d.shift()),b.isArray(a)&&(d=a.reduce(function(a,b,c){return a[b]=d[c],a},{})),c.fulfill(d))}},h.prototype.ready=function(a){var b=new this.constructor;return this.then(function(a){b.fulfill(a)}),setTimeout(function(){b.reject(new Error("Did not resolve future in "+a+" ms"))},a),b},h.prototype.fallbackTo=function(a){var b=new this.constructor;return this.then(function(a){b.fulfill(a)},function(){a.then(function(a){b.fulfill(a)})}),b},h.prototype.filter=function(a){var b=new this.constructor;return this.then(function(c){return a(c)?b.fulfill(c):b.reject(new Error("Value does not match predicate"))},function(a){b.reject(a)}),b},h.prototype.map=function(a){return this.then(a)},h.prototype.onFailure=function(a){this.then(null,a)},h.prototype.onSuccess=function(a){this.then(a)},h.prototype.recover=function(a){var b=new this.constructor;return this.then(function(a){b.fulfill(a)},function(){b.fulfill(a)}),b},h.prototype.transform=h.prototype.then,h.prototype.zip=function(a){var b=new this.constructor;return this.then(function(c){a.then(function(a){b.fulfill([c,a])})}),b},h.sequence=function(a){var c,d,e=new h,f=0,g=function(a,b){a.then(function(a){c[b]=a,++f>=d&&e.fulfill(c)},function(a){e.reject(a)})};if(b.isArray(a))c=[],d=a.length,a.forEach(g);else{c={},d=Object.keys(a).length;for(var i in a)g(a[i],i)}return e};var i={},j=i.forEach=function(b,c){var d=new h,e=0;return b.forEach(function(b,f,g){a(function(){try{c(b,f,g)}catch(a){d.reject(a)}++e>=g.length&&d.fulfill(e)})}),d};i.every=function(a,b){var c=new h,d=j(a,function(a,d,e){b(a,d,e)||c.fulfill(!1)});return d.then(function(){c.fulfill(!0)},function(a){c.reject(a)}),c},i.some=function(a,b){var c=new h,d=j(a,function(a,d,e){b(a,d,e)&&c.fulfill(!0)});return d.then(function(){c.fulfill(!1)},function(a){c.reject(a)}),c},i.filter=function(a,b){var c=new h,d=[],e=j(a,function(a,c,e){b(a,c,e)&&d.push(a)});return e.then(function(){c.fulfill(d)},function(a){c.reject(a)}),c},i.map=function(a,b){var c=new h,d=[],e=j(a,function(a,c,e){d.push(b(a,c,e))});return e.then(function(){c.fulfill(d)},function(a){c.reject(a)}),c};var k=i.reverse=function(a){var b=new h,c=[],d=j(a,function(a){c.unshift(a)});return d.then(function(){b.fulfill(c)},function(a){b.reject(a)}),b},l=i.reduce=function(a,b,c){var d,e,f=new h;c?(d=c,e=a):(d=a[0],e=a.slice(1));var g=j(a,function(a,c,e){d=b(d,a,c,e)});return g.then(function(){f.fulfill(d)},function(a){f.reject(a)}),f};return i.reduceRight=function(a,b,c){return k(a).then(function(a){return l(a,b,c)})},{State:c,Promise:g,Future:h,collections:i}}); | ||
!function(a,b){"function"==typeof define&&define.amd?define(b):"object"==typeof exports?module.exports=b():a.concurrent=b()}(this,function(){var a="function"==typeof setImmediate?setImmediate:function(a){setTimeout(a,0)},b={isArray:function(a){return Array.isArray(a)},inherits:function(a,b){a.super_=b,a.prototype=Object.create(b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}})}},c={PENDING:"pending",FULFILLED:"fulfilled",REJECTED:"rejected"},d=function(b){var c;c=function(){var d=b.callbacks.shift();d&&d[b.state]&&d[b.state](b.value),b.callbacks.length>0&&a(c)},a(c)},e=function(a,b,e){return a.state===c.FULFILLED||a.state===c.REJECTED?a.state:(a.state=b,a.value=e,d(a),a.state)},f=function(a,b,c){return function(d){if("function"!=typeof c)return e(a,b,d);try{var f=c(d);return f&&"function"==typeof f.then?(f.then(function(b){a.fulfill(b)},function(b){a.reject(b)}),a.state):a.fulfill(f)}catch(g){return a.reject(g)}}},g=function(){this.state=c.PENDING,this.callbacks=[]};g.prototype.then=function(a,b){var e=new this.constructor,g={};return g[c.FULFILLED]=f(e,c.FULFILLED,a),g[c.REJECTED]=f(e,c.REJECTED,b),this.callbacks.push(g),this.state!==c.PENDING&&d(this),e},g.prototype.fulfill=function(a){e(this,c.FULFILLED,a)},g.prototype.reject=function(a){e(this,c.REJECTED,a)};var h=function(a){Error.captureStackTrace(this,this),this.value=a,this.message=""+this.value+" did not match predicate"};b.inherits(h,Error),h.prototype.name="ValidationError";var i=function(a){Error.captureStackTrace(this,this),this.time=a,this.message="Future did not resolve in "+this.time+" ms"};b.inherits(i,Error),i.prototype.name="TimeoutError";var j=function(){g.call(this)};b.inherits(j,g),j.prototype.isCompleted=function(){return this.state!==c.PENDING},j.prototype.onComplete=function(a){this.then(function(b){a(b)},function(b){a(b)})},j.prototype.convert=function(a){var c=this;return function(){var d=Array.prototype.slice.call(arguments),e=d.shift();return e?c.reject(e):(1===d.length&&(d=d.shift()),b.isArray(a)&&(d=a.reduce(function(a,b,c){return a[b]=d[c],a},{})),c.fulfill(d))}},j.prototype.ready=function(a){var b=new this.constructor;return this.then(function(a){b.fulfill(a)}),setTimeout(function(){try{throw new i(a)}catch(c){b.reject(c)}},a),b},j.prototype.fallbackTo=function(a){return this.then(null,function(){return a})},j.prototype.filter=function(a){return this.then(function(b){if(!a(b))throw new h(b);return b})},j.prototype.map=function(a){return this.then(a)},j.prototype.onFailure=function(a){this.then(null,a)},j.prototype.onSuccess=function(a){this.then(a)},j.prototype.recover=function(a){var b=new this.constructor;return this.then(function(a){b.fulfill(a)},function(){b.fulfill(a)}),b},j.prototype.recoverWith=function(a){return this.then(null,a)},j.prototype.transform=j.prototype.then,j.prototype.zip=function(a){return this.then(function(b){return a.then(function(a){return[b,a]})})},j.sequence=function(a){var c,d,e=new j,f=0,g=function(a,b){a.then(function(a){c[b]=a,++f>=d&&e.fulfill(c)},function(a){e.reject(a)})};if(b.isArray(a))c=[],d=a.length,a.forEach(g);else{c={},d=Object.keys(a).length;for(var h in a)g(a[h],h)}return e},j.fulfilled=function(a){var b=new j;return b.fulfill(a),b},j.rejected=function(a){var b=new j;return b.reject(a),b};var k={},l=k.forEach=function(b,c){var d=new j,e=0;return b.forEach(function(b,f,g){a(function(){try{c(b,f,g)}catch(a){d.reject(a)}++e>=g.length&&d.fulfill(e)})}),d};k.every=function(a,b){var c=new j,d=l(a,function(a,d,e){b(a,d,e)||c.fulfill(!1)});return d.then(function(){c.fulfill(!0)},function(a){c.reject(a)}),c},k.some=function(a,b){var c=new j,d=l(a,function(a,d,e){b(a,d,e)&&c.fulfill(!0)});return d.then(function(){c.fulfill(!1)},function(a){c.reject(a)}),c},k.filter=function(a,b){var c=new j,d=[],e=l(a,function(a,c,e){b(a,c,e)&&d.push(a)});return e.then(function(){c.fulfill(d)},function(a){c.reject(a)}),c},k.map=function(a,b){var c=new j,d=[],e=l(a,function(a,c,e){d.push(b(a,c,e))});return e.then(function(){c.fulfill(d)},function(a){c.reject(a)}),c};var m=k.reverse=function(a){var b=new j,c=[],d=l(a,function(a){c.unshift(a)});return d.then(function(){b.fulfill(c)},function(a){b.reject(a)}),b},n=k.reduce=function(a,b,c){var d,e,f=new j;c?(d=c,e=a):(d=a[0],e=a.slice(1));var g=l(a,function(a,c,e){d=b(d,a,c,e)});return g.then(function(){f.fulfill(d)},function(a){f.reject(a)}),f};return k.reduceRight=function(a,b,c){return m(a).then(function(a){return n(a,b,c)})},{State:c,Promise:g,Future:j,collections:k}}); |
@@ -8,3 +8,3 @@ module.exports = function(grunt) { | ||
}, | ||
all: ['Gruntfile.js', 'index.js', 'lib/*.js', 'test/*.js'] | ||
all: ['Gruntfile.js', 'index.js', 'lib/**/*.js', 'test/*.js'] | ||
}, | ||
@@ -24,2 +24,4 @@ concat: { | ||
'lib/promise.js', | ||
'lib/errors/validation.js', | ||
'lib/errors/timeout.js', | ||
'lib/future.js', | ||
@@ -26,0 +28,0 @@ 'lib/collections.js' |
var Promise = require('./promise'); | ||
var State = require('./state'); | ||
var TimeoutError = require('./errors/timeout'); | ||
var util = require('util'); | ||
var ValidationError = require('./errors/validation'); | ||
@@ -44,3 +46,3 @@ /** | ||
* @param {Array} keys The keys for the callback | ||
* | ||
* | ||
* @return {Function} The callback for async | ||
@@ -87,3 +89,7 @@ */ | ||
setTimeout(function() { | ||
future.reject(new Error('Did not resolve future in ' + duration + ' ms')); | ||
try { | ||
throw new TimeoutError(duration); | ||
} catch (err) { | ||
future.reject(err); | ||
} | ||
}, duration); | ||
@@ -101,13 +107,5 @@ | ||
Future.prototype.fallbackTo = function(promise) { | ||
var future = new this.constructor(); | ||
this.then(function(value) { | ||
future.fulfill(value); | ||
}, function() { | ||
promise.then(function(value) { | ||
future.fulfill(value); | ||
}); | ||
return this.then(null, function(reason) { | ||
return promise; | ||
}); | ||
return future; | ||
}; | ||
@@ -122,15 +120,9 @@ | ||
Future.prototype.filter = function(predicate) { | ||
var future = new this.constructor(); | ||
this.then(function(value) { | ||
if (predicate(value)) { | ||
return future.fulfill(value); | ||
return this.then(function(value) { | ||
if (!predicate(value)) { | ||
throw new ValidationError(value); | ||
} | ||
return future.reject(new Error('Value does not match predicate')); | ||
}, function(reason) { | ||
future.reject(reason); | ||
return value; | ||
}); | ||
return future; | ||
}; | ||
@@ -185,2 +177,12 @@ | ||
/** | ||
* Recovers a future with a function that returns a promise. | ||
* | ||
* @param {Function} callback A function returning a promise | ||
* @return {Future} The new future | ||
*/ | ||
Future.prototype.recoverWith = function(callback) { | ||
return this.then(null, callback); | ||
}; | ||
/** | ||
* Alias for then | ||
@@ -201,11 +203,7 @@ * | ||
Future.prototype.zip = function(promise) { | ||
var future = new this.constructor(); | ||
this.then(function(left) { | ||
promise.then(function(right) { | ||
future.fulfill([left, right]); | ||
return this.then(function(left) { | ||
return promise.then(function(right) { | ||
return [left, right]; | ||
}); | ||
}); | ||
return future; | ||
}; | ||
@@ -215,3 +213,3 @@ | ||
* Sequences a list of futures together | ||
* | ||
* | ||
* @param {Object} tasks Either an object or an array of tasks | ||
@@ -235,3 +233,3 @@ * @return {Future} The future of all the results | ||
}; | ||
if (util.isArray(tasks)) { | ||
@@ -248,4 +246,28 @@ results = []; | ||
} | ||
return future; | ||
}; | ||
/** | ||
* Creates a future fulfilled with a value | ||
* | ||
* @param {Object} value The value of the future to be fulfilled with | ||
* @return {Future} The fulfilled future | ||
*/ | ||
Future.fulfilled = function(value) { | ||
var future = new Future(); | ||
future.fulfill(value); | ||
return future; | ||
}; | ||
/** | ||
* Creates a future rejected with a reason | ||
* | ||
* @param {Object} reason The reason the future was rejected | ||
* @return {Future} The rejected future | ||
*/ | ||
Future.rejected = function(reason) { | ||
var future = new Future(); | ||
future.reject(reason); | ||
return future; | ||
}; |
{ | ||
"name": "concurrent", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "Promises/A+ with Scala awesomeness", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var chai = require('chai'); | ||
var Future = require('../lib/future'); | ||
var spies = require('chai-spies'); | ||
var TimeoutError = require('../lib/errors/timeout'); | ||
var ValidationError = require('../lib/errors/validation'); | ||
@@ -110,3 +112,6 @@ chai.use(spies); | ||
future.then(function(value) { | ||
expect(value).to.eql({left: LEFT, right: RIGHT}); | ||
expect(value).to.eql({ | ||
left: LEFT, | ||
right: RIGHT | ||
}); | ||
done(); | ||
@@ -135,7 +140,5 @@ }); | ||
var readied = future.ready(10); | ||
var spy = chai.spy(function() {}); | ||
readied.then(null, spy); | ||
readied.then(null, function() { | ||
expect(spy).to.have.been.called; | ||
readied.then(null, function(reason) { | ||
expect(reason).to.be.an.instanceof(TimeoutError); | ||
done(); | ||
@@ -240,7 +243,5 @@ }); | ||
}); | ||
var spy = chai.spy(function() {}); | ||
filtered.then(null, spy); | ||
filtered.then(null, function() { | ||
expect(spy).to.have.been.called; | ||
filtered.then(null, function(reason) { | ||
expect(reason).to.be.an.instanceof(ValidationError); | ||
done(); | ||
@@ -381,2 +382,36 @@ }); | ||
describe('#recoverWith', function() { | ||
var fallback = function() { | ||
var future = new Future(); | ||
future.fulfill(RIGHT); | ||
return future; | ||
}; | ||
it('should have recoverWith', function() { | ||
expect(future).to.have.property('recoverWith'); | ||
}); | ||
it('should be called with the normal future value', function(done) { | ||
var recovered = future.recoverWith(fallback); | ||
recovered.then(function(value) { | ||
expect(value).to.eql(LEFT); | ||
done(); | ||
}); | ||
future.fulfill(LEFT); | ||
}); | ||
it('should be called with value on error', function(done) { | ||
var recovered = future.recoverWith(fallback); | ||
recovered.then(function(value) { | ||
expect(value).to.eql(RIGHT); | ||
done(); | ||
}); | ||
future.reject(LEFT); | ||
}); | ||
}); | ||
describe('#transform', function() { | ||
@@ -509,3 +544,6 @@ it('should have transform', function() { | ||
sequenced.then(function(value) { | ||
expect(value).to.eql({left: LEFT, right: RIGHT}); | ||
expect(value).to.eql({ | ||
left: LEFT, | ||
right: RIGHT | ||
}); | ||
done(); | ||
@@ -518,2 +556,30 @@ }); | ||
}); | ||
}); | ||
describe('#fulfilled', function() { | ||
it('should have fulfilled', function() { | ||
expect(Future).to.have.property('fulfilled'); | ||
}); | ||
it('should fulfill a new future', function(done) { | ||
var fulfilled = Future.fulfilled(SUCCESS); | ||
fulfilled.then(function(value) { | ||
expect(value).to.eql(SUCCESS); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('#rejected', function() { | ||
it('should have rejected', function() { | ||
expect(Future).to.have.property('rejected'); | ||
}); | ||
it('should reject a new future', function(done) { | ||
var rejected = Future.rejected(ERROR); | ||
rejected.then(null, function(reason) { | ||
expect(reason).to.eql(ERROR); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
68552
22
1847