Commit 8a24804f authored by Yehuda Katz's avatar Yehuda Katz

Add RSVP.hash

parent 257996c1
define( define(
["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/defer","rsvp/config","exports"], ["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __exports__) { function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __exports__) {
"use strict"; "use strict";
var EventTarget = __dependency1__.EventTarget; var EventTarget = __dependency1__.EventTarget;
var Promise = __dependency2__.Promise; var Promise = __dependency2__.Promise;
var denodeify = __dependency3__.denodeify; var denodeify = __dependency3__.denodeify;
var all = __dependency4__.all; var all = __dependency4__.all;
var defer = __dependency5__.defer; var hash = __dependency5__.hash;
var config = __dependency6__.config; var defer = __dependency6__.defer;
var config = __dependency7__.config;
function configure(name, value) { function configure(name, value) {
config[name] = value; config[name] = value;
...@@ -16,6 +17,7 @@ define( ...@@ -16,6 +17,7 @@ define(
__exports__.Promise = Promise; __exports__.Promise = Promise;
__exports__.EventTarget = EventTarget; __exports__.EventTarget = EventTarget;
__exports__.all = all; __exports__.all = all;
__exports__.hash = hash;
__exports__.defer = defer; __exports__.defer = defer;
__exports__.denodeify = denodeify; __exports__.denodeify = denodeify;
__exports__.configure = configure; __exports__.configure = configure;
......
...@@ -32,23 +32,16 @@ var define, requireModule; ...@@ -32,23 +32,16 @@ var define, requireModule;
})(); })();
define("rsvp/all", define("rsvp/all",
["rsvp/promise","exports"], ["rsvp/defer","exports"],
function(__dependency1__, __exports__) { function(__dependency1__, __exports__) {
"use strict"; "use strict";
var Promise = __dependency1__.Promise; var defer = __dependency1__.defer;
function all(promises) { function all(promises) {
var i, results = [], resolve, reject; var results = [], deferred = defer(), remaining = promises.length;
var allPromise = new Promise(function(allResolver, allRejecter) {
resolve = allResolver;
reject = allRejecter;
});
var remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
resolve([]); deferred.resolve([]);
} }
var resolver = function(index) { var resolver = function(index) {
...@@ -60,22 +53,22 @@ define("rsvp/all", ...@@ -60,22 +53,22 @@ define("rsvp/all",
var resolveAll = function(index, value) { var resolveAll = function(index, value) {
results[index] = value; results[index] = value;
if (--remaining === 0) { if (--remaining === 0) {
resolve(results); deferred.resolve(results);
} }
}; };
var rejectAll = function(error) { var rejectAll = function(error) {
reject(error); deferred.reject(error);
}; };
for (i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll); promises[i].then(resolver(i), rejectAll);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promises[i]);
} }
} }
return allPromise; return deferred.promise;
} }
__exports__.all = all; __exports__.all = all;
...@@ -270,6 +263,60 @@ define("rsvp/events", ...@@ -270,6 +263,60 @@ define("rsvp/events",
__exports__.EventTarget = EventTarget; __exports__.EventTarget = EventTarget;
}); });
define("rsvp/hash",
["rsvp/defer","exports"],
function(__dependency1__, __exports__) {
"use strict";
var defer = __dependency1__.defer;
function size(object) {
var size = 0;
for (var prop in object) {
size++;
}
return size;
}
function hash(promises) {
var results = {}, deferred = defer(), remaining = size(promises);
if (remaining === 0) {
deferred.resolve({});
}
var resolver = function(prop) {
return function(value) {
resolveAll(prop, value);
};
};
var resolveAll = function(prop, value) {
results[prop] = value;
if (--remaining === 0) {
deferred.resolve(results);
}
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var prop in promises) {
if (promises[prop] && typeof promises[prop].then === 'function') {
promises[prop].then(resolver(prop), rejectAll);
} else {
resolveAll(prop, promises[prop]);
}
}
return deferred.promise;
}
__exports__.hash = hash;
});
define("rsvp/node", define("rsvp/node",
["rsvp/promise","rsvp/all","exports"], ["rsvp/promise","rsvp/all","exports"],
function(__dependency1__, __dependency2__, __exports__) { function(__dependency1__, __dependency2__, __exports__) {
...@@ -456,15 +503,16 @@ define("rsvp/promise", ...@@ -456,15 +503,16 @@ define("rsvp/promise",
}); });
define("rsvp", define("rsvp",
["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/defer","rsvp/config","exports"], ["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __exports__) { function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __exports__) {
"use strict"; "use strict";
var EventTarget = __dependency1__.EventTarget; var EventTarget = __dependency1__.EventTarget;
var Promise = __dependency2__.Promise; var Promise = __dependency2__.Promise;
var denodeify = __dependency3__.denodeify; var denodeify = __dependency3__.denodeify;
var all = __dependency4__.all; var all = __dependency4__.all;
var defer = __dependency5__.defer; var hash = __dependency5__.hash;
var config = __dependency6__.config; var defer = __dependency6__.defer;
var config = __dependency7__.config;
function configure(name, value) { function configure(name, value) {
config[name] = value; config[name] = value;
...@@ -473,6 +521,7 @@ define("rsvp", ...@@ -473,6 +521,7 @@ define("rsvp",
__exports__.Promise = Promise; __exports__.Promise = Promise;
__exports__.EventTarget = EventTarget; __exports__.EventTarget = EventTarget;
__exports__.all = all; __exports__.all = all;
__exports__.hash = hash;
__exports__.defer = defer; __exports__.defer = defer;
__exports__.denodeify = denodeify; __exports__.denodeify = denodeify;
__exports__.configure = configure; __exports__.configure = configure;
......
(function(){var e,t;(function(){var n={},r={};e=function(e,t,r){n[e]={deps:t,callback:r}};t=function(e){if(r[e]){return r[e]}r[e]={};var i=n[e],o=i.deps,s=i.callback,f=[],c;for(var u=0,a=o.length;u<a;u++){if(o[u]==="exports"){f.push(c={})}else{f.push(t(o[u]))}}var l=s.apply(this,f);return r[e]=c||l}})();e("rsvp/all",["rsvp/promise","exports"],function(e,t){"use strict";var n=e.Promise;function r(e){var t,r=[],i,o;var s=new n(function(e,t){i=e;o=t});var f=e.length;if(f===0){i([])}var c=function(e){return function(t){u(e,t)}};var u=function(e,t){r[e]=t;if(--f===0){i(r)}};var a=function(e){o(e)};for(t=0;t<e.length;t++){if(e[t]&&typeof e[t].then==="function"){e[t].then(c(t),a)}else{u(t,e[t])}}return s}t.all=r});e("rsvp/async",["exports"],function(e){"use strict";var t=typeof window!=="undefined"?window:{};var n=t.MutationObserver||t.WebKitMutationObserver;var r;if(typeof process!=="undefined"&&{}.toString.call(process)==="[object process]"){r=function(e,t){process.nextTick(function(){e.call(t)})}}else if(n){var i=[];var o=new n(function(){var e=i.slice();i=[];e.forEach(function(e){var t=e[0],n=e[1];t.call(n)})});var s=document.createElement("div");o.observe(s,{attributes:true});window.addEventListener("unload",function(){o.disconnect();o=null});r=function(e,t){i.push([e,t]);s.setAttribute("drainQueue","drainQueue")}}else{r=function(e,t){setTimeout(function(){e.call(t)},1)}}e.async=r});e("rsvp/config",["rsvp/async","exports"],function(e,t){"use strict";var n=e.async;var r={};r.async=n;t.config=r});e("rsvp/defer",["rsvp/promise","exports"],function(e,t){"use strict";var n=e.Promise;function r(){var e={};var t=new n(function(t,n){e.resolve=t;e.reject=n});e.promise=t;return e}t.defer=r});e("rsvp/events",["exports"],function(e){"use strict";var t=function(e,t){this.type=e;for(var n in t){if(!t.hasOwnProperty(n)){continue}this[n]=t[n]}};var n=function(e,t){for(var n=0,r=e.length;n<r;n++){if(e[n][0]===t){return n}}return-1};var r=function(e){var t=e._promiseCallbacks;if(!t){t=e._promiseCallbacks={}}return t};var i={mixin:function(e){e.on=this.on;e.off=this.off;e.trigger=this.trigger;return e},on:function(e,t,i){var o=r(this),s,f;e=e.split(/\s+/);i=i||this;while(f=e.shift()){s=o[f];if(!s){s=o[f]=[]}if(n(s,t)===-1){s.push([t,i])}}},off:function(e,t){var i=r(this),o,s,f;e=e.split(/\s+/);while(s=e.shift()){if(!t){i[s]=[];continue}o=i[s];f=n(o,t);if(f!==-1){o.splice(f,1)}}},trigger:function(e,n){var i=r(this),o,s,f,c,u;if(o=i[e]){for(var a=0;a<o.length;a++){s=o[a];f=s[0];c=s[1];if(typeof n!=="object"){n={detail:n}}u=new t(e,n);f.call(c,u)}}}};e.EventTarget=i});e("rsvp/node",["rsvp/promise","rsvp/all","exports"],function(e,t,n){"use strict";var r=e.Promise;var i=t.all;function o(e,t){return function(n,r){if(n){t(n)}else if(arguments.length>2){e(Array.prototype.slice.call(arguments,1))}else{e(r)}}}function s(e){return function(){var t=Array.prototype.slice.call(arguments),n,s;var f=new r(function(e,t){n=e;s=t});i(t).then(function(t){t.push(o(n,s));try{e.apply(this,t)}catch(r){s(r)}});return f}}n.denodeify=s});e("rsvp/promise",["rsvp/config","rsvp/events","exports"],function(e,t,n){"use strict";var r=e.config;var i=t.EventTarget;var o=function(){};var s=function(e){var t=this;if(typeof e!=="function"){throw new TypeError("You must pass a resolver function as the sole argument to the promise constructor")}if(!(t instanceof s)){return new s(e)}var n=function(e){c(t,e);n=o;r=o};var r=function(e){a(t,e);n=o;r=o};this.on("promise:resolved",function(e){this.trigger("success",{detail:e.detail})},this);this.on("promise:failed",function(e){this.trigger("error",{detail:e.detail})},this);e(n,r)};var f=function(e,t,n,r){var i=typeof n==="function",o,s,f,u;if(i){try{o=n(r.detail);f=true}catch(l){u=true;s=l}}else{o=r.detail;f=true}if(o&&typeof o.then==="function"){o.then(function(e){c(t,e)},function(e){a(t,e)})}else if(i&&f){c(t,o)}else if(u){a(t,s)}else if(e==="resolve"){c(t,o)}else if(e==="reject"){a(t,o)}};s.prototype={constructor:s,then:function(e,t){var n=new s(function(){});if(this.isFulfilled){r.async(function(){f("resolve",n,e,{detail:this.fulfillmentValue})},this)}if(this.isRejected){r.async(function(){f("reject",n,t,{detail:this.rejectedReason})},this)}this.on("promise:resolved",function(t){f("resolve",n,e,t)});this.on("promise:failed",function(e){f("reject",n,t,e)});return n}};i.mixin(s.prototype);function c(e,t){if(t&&typeof t.then==="function"){t.then(function(t){c(e,t)},function(t){a(e,t)})}else{u(e,t)}}function u(e,t){r.async(function(){e.trigger("promise:resolved",{detail:t});e.isFulfilled=true;e.fulfillmentValue=t})}function a(e,t){r.async(function(){e.trigger("promise:failed",{detail:t});e.isRejected=true;e.rejectedReason=t})}n.Promise=s});e("rsvp",["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/defer","rsvp/config","exports"],function(e,t,n,r,i,o,s){"use strict";var f=e.EventTarget;var c=t.Promise;var u=n.denodeify;var a=r.all;var l=i.defer;var v=o.config;function p(e,t){v[e]=t}s.Promise=c;s.EventTarget=f;s.all=a;s.defer=l;s.denodeify=u;s.configure=p});window.RSVP=t("rsvp")})(); (function(){var e,t;(function(){var n={},r={};e=function(e,t,r){n[e]={deps:t,callback:r}};t=function(e){if(r[e]){return r[e]}r[e]={};var i=n[e],o=i.deps,s=i.callback,f=[],c;for(var a=0,u=o.length;a<u;a++){if(o[a]==="exports"){f.push(c={})}else{f.push(t(o[a]))}}var v=s.apply(this,f);return r[e]=c||v}})();e("rsvp/all",["rsvp/defer","exports"],function(e,t){"use strict";var n=e.defer;function r(e){var t=[],r=n(),i=e.length;if(i===0){r.resolve([])}var o=function(e){return function(t){s(e,t)}};var s=function(e,n){t[e]=n;if(--i===0){r.resolve(t)}};var f=function(e){r.reject(e)};for(var c=0;c<e.length;c++){if(e[c]&&typeof e[c].then==="function"){e[c].then(o(c),f)}else{s(c,e[c])}}return r.promise}t.all=r});e("rsvp/async",["exports"],function(e){"use strict";var t=typeof window!=="undefined"?window:{};var n=t.MutationObserver||t.WebKitMutationObserver;var r;if(typeof process!=="undefined"&&{}.toString.call(process)==="[object process]"){r=function(e,t){process.nextTick(function(){e.call(t)})}}else if(n){var i=[];var o=new n(function(){var e=i.slice();i=[];e.forEach(function(e){var t=e[0],n=e[1];t.call(n)})});var s=document.createElement("div");o.observe(s,{attributes:true});window.addEventListener("unload",function(){o.disconnect();o=null});r=function(e,t){i.push([e,t]);s.setAttribute("drainQueue","drainQueue")}}else{r=function(e,t){setTimeout(function(){e.call(t)},1)}}e.async=r});e("rsvp/config",["rsvp/async","exports"],function(e,t){"use strict";var n=e.async;var r={};r.async=n;t.config=r});e("rsvp/defer",["rsvp/promise","exports"],function(e,t){"use strict";var n=e.Promise;function r(){var e={};var t=new n(function(t,n){e.resolve=t;e.reject=n});e.promise=t;return e}t.defer=r});e("rsvp/events",["exports"],function(e){"use strict";var t=function(e,t){this.type=e;for(var n in t){if(!t.hasOwnProperty(n)){continue}this[n]=t[n]}};var n=function(e,t){for(var n=0,r=e.length;n<r;n++){if(e[n][0]===t){return n}}return-1};var r=function(e){var t=e._promiseCallbacks;if(!t){t=e._promiseCallbacks={}}return t};var i={mixin:function(e){e.on=this.on;e.off=this.off;e.trigger=this.trigger;return e},on:function(e,t,i){var o=r(this),s,f;e=e.split(/\s+/);i=i||this;while(f=e.shift()){s=o[f];if(!s){s=o[f]=[]}if(n(s,t)===-1){s.push([t,i])}}},off:function(e,t){var i=r(this),o,s,f;e=e.split(/\s+/);while(s=e.shift()){if(!t){i[s]=[];continue}o=i[s];f=n(o,t);if(f!==-1){o.splice(f,1)}}},trigger:function(e,n){var i=r(this),o,s,f,c,a;if(o=i[e]){for(var u=0;u<o.length;u++){s=o[u];f=s[0];c=s[1];if(typeof n!=="object"){n={detail:n}}a=new t(e,n);f.call(c,a)}}}};e.EventTarget=i});e("rsvp/hash",["rsvp/defer","exports"],function(e,t){"use strict";var n=e.defer;function r(e){var t=0;for(var n in e){t++}return t}function i(e){var t={},i=n(),o=r(e);if(o===0){i.resolve({})}var s=function(e){return function(t){f(e,t)}};var f=function(e,n){t[e]=n;if(--o===0){i.resolve(t)}};var c=function(e){i.reject(e)};for(var a in e){if(e[a]&&typeof e[a].then==="function"){e[a].then(s(a),c)}else{f(a,e[a])}}return i.promise}t.hash=i});e("rsvp/node",["rsvp/promise","rsvp/all","exports"],function(e,t,n){"use strict";var r=e.Promise;var i=t.all;function o(e,t){return function(n,r){if(n){t(n)}else if(arguments.length>2){e(Array.prototype.slice.call(arguments,1))}else{e(r)}}}function s(e){return function(){var t=Array.prototype.slice.call(arguments),n,s;var f=new r(function(e,t){n=e;s=t});i(t).then(function(t){t.push(o(n,s));try{e.apply(this,t)}catch(r){s(r)}});return f}}n.denodeify=s});e("rsvp/promise",["rsvp/config","rsvp/events","exports"],function(e,t,n){"use strict";var r=e.config;var i=t.EventTarget;var o=function(){};var s=function(e){var t=this;if(typeof e!=="function"){throw new TypeError("You must pass a resolver function as the sole argument to the promise constructor")}if(!(t instanceof s)){return new s(e)}var n=function(e){c(t,e);n=o;r=o};var r=function(e){u(t,e);n=o;r=o};this.on("promise:resolved",function(e){this.trigger("success",{detail:e.detail})},this);this.on("promise:failed",function(e){this.trigger("error",{detail:e.detail})},this);e(n,r)};var f=function(e,t,n,r){var i=typeof n==="function",o,s,f,a;if(i){try{o=n(r.detail);f=true}catch(v){a=true;s=v}}else{o=r.detail;f=true}if(o&&typeof o.then==="function"){o.then(function(e){c(t,e)},function(e){u(t,e)})}else if(i&&f){c(t,o)}else if(a){u(t,s)}else if(e==="resolve"){c(t,o)}else if(e==="reject"){u(t,o)}};s.prototype={constructor:s,then:function(e,t){var n=new s(function(){});if(this.isFulfilled){r.async(function(){f("resolve",n,e,{detail:this.fulfillmentValue})},this)}if(this.isRejected){r.async(function(){f("reject",n,t,{detail:this.rejectedReason})},this)}this.on("promise:resolved",function(t){f("resolve",n,e,t)});this.on("promise:failed",function(e){f("reject",n,t,e)});return n}};i.mixin(s.prototype);function c(e,t){if(t&&typeof t.then==="function"){t.then(function(t){c(e,t)},function(t){u(e,t)})}else{a(e,t)}}function a(e,t){r.async(function(){e.trigger("promise:resolved",{detail:t});e.isFulfilled=true;e.fulfillmentValue=t})}function u(e,t){r.async(function(){e.trigger("promise:failed",{detail:t});e.isRejected=true;e.rejectedReason=t})}n.Promise=s});e("rsvp",["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","exports"],function(e,t,n,r,i,o,s,f){"use strict";var c=e.EventTarget;var a=t.Promise;var u=n.denodeify;var v=r.all;var l=i.hash;var p=o.defer;var h=s.config;function d(e,t){h[e]=t}f.Promise=a;f.EventTarget=c;f.all=v;f.hash=l;f.defer=p;f.denodeify=u;f.configure=d});window.RSVP=t("rsvp")})();
define( define(
["rsvp/promise","exports"], ["rsvp/defer","exports"],
function(__dependency1__, __exports__) { function(__dependency1__, __exports__) {
"use strict"; "use strict";
var Promise = __dependency1__.Promise; var defer = __dependency1__.defer;
function all(promises) { function all(promises) {
var i, results = [], resolve, reject; var results = [], deferred = defer(), remaining = promises.length;
var allPromise = new Promise(function(allResolver, allRejecter) {
resolve = allResolver;
reject = allRejecter;
});
var remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
resolve([]); deferred.resolve([]);
} }
var resolver = function(index) { var resolver = function(index) {
...@@ -27,22 +20,22 @@ define( ...@@ -27,22 +20,22 @@ define(
var resolveAll = function(index, value) { var resolveAll = function(index, value) {
results[index] = value; results[index] = value;
if (--remaining === 0) { if (--remaining === 0) {
resolve(results); deferred.resolve(results);
} }
}; };
var rejectAll = function(error) { var rejectAll = function(error) {
reject(error); deferred.reject(error);
}; };
for (i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll); promises[i].then(resolver(i), rejectAll);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promises[i]);
} }
} }
return allPromise; return deferred.promise;
} }
__exports__.all = all; __exports__.all = all;
......
define(
["rsvp/defer","exports"],
function(__dependency1__, __exports__) {
"use strict";
var defer = __dependency1__.defer;
function size(object) {
var size = 0;
for (var prop in object) {
size++;
}
return size;
}
function hash(promises) {
var results = {}, deferred = defer(), remaining = size(promises);
if (remaining === 0) {
deferred.resolve({});
}
var resolver = function(prop) {
return function(value) {
resolveAll(prop, value);
};
};
var resolveAll = function(prop, value) {
results[prop] = value;
if (--remaining === 0) {
deferred.resolve(results);
}
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var prop in promises) {
if (promises[prop] && typeof promises[prop].then === 'function') {
promises[prop].then(resolver(prop), rejectAll);
} else {
resolveAll(prop, promises[prop]);
}
}
return deferred.promise;
}
__exports__.hash = hash;
});
...@@ -2,6 +2,7 @@ import { EventTarget } from "rsvp/events"; ...@@ -2,6 +2,7 @@ import { EventTarget } from "rsvp/events";
import { Promise } from "rsvp/promise"; import { Promise } from "rsvp/promise";
import { denodeify } from "rsvp/node"; import { denodeify } from "rsvp/node";
import { all } from "rsvp/all"; import { all } from "rsvp/all";
import { hash } from "rsvp/hash";
import { defer } from "rsvp/defer"; import { defer } from "rsvp/defer";
import { config } from "rsvp/config"; import { config } from "rsvp/config";
...@@ -9,4 +10,4 @@ function configure(name, value) { ...@@ -9,4 +10,4 @@ function configure(name, value) {
config[name] = value; config[name] = value;
} }
export { Promise, EventTarget, all, defer, denodeify, configure }; export { Promise, EventTarget, all, hash, defer, denodeify, configure };
\ No newline at end of file \ No newline at end of file
import { Promise } from "rsvp/promise"; import { defer } from "rsvp/defer";
function all(promises) { function all(promises) {
var i, results = [], resolve, reject; var results = [], deferred = defer(), remaining = promises.length;
var allPromise = new Promise(function(allResolver, allRejecter) {
resolve = allResolver;
reject = allRejecter;
});
var remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
resolve([]); deferred.resolve([]);
} }
var resolver = function(index) { var resolver = function(index) {
...@@ -23,22 +16,22 @@ function all(promises) { ...@@ -23,22 +16,22 @@ function all(promises) {
var resolveAll = function(index, value) { var resolveAll = function(index, value) {
results[index] = value; results[index] = value;
if (--remaining === 0) { if (--remaining === 0) {
resolve(results); deferred.resolve(results);
} }
}; };
var rejectAll = function(error) { var rejectAll = function(error) {
reject(error); deferred.reject(error);
}; };
for (i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') { if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll); promises[i].then(resolver(i), rejectAll);
} else { } else {
resolveAll(i, promises[i]); resolveAll(i, promises[i]);
} }
} }
return allPromise; return deferred.promise;
} }
export { all }; export { all };
\ No newline at end of file
import { defer } from "rsvp/defer";
function size(object) {
var size = 0;
for (var prop in object) {
size++;
}
return size;
}
function hash(promises) {
var results = {}, deferred = defer(), remaining = size(promises);
if (remaining === 0) {
deferred.resolve({});
}
var resolver = function(prop) {
return function(value) {
resolveAll(prop, value);
};
};
var resolveAll = function(prop, value) {
results[prop] = value;
if (--remaining === 0) {
deferred.resolve(results);
}
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var prop in promises) {
if (promises[prop] && typeof promises[prop].then === 'function') {
promises[prop].then(resolver(prop), rejectAll);
} else {
resolveAll(prop, promises[prop]);
}
}
return deferred.promise;
}
export { hash };
\ No newline at end of file
...@@ -322,6 +322,92 @@ describe("RSVP extensions", function() { ...@@ -322,6 +322,92 @@ describe("RSVP extensions", function() {
}); });
}); });
describe("RSVP.hash", function() {
it('should exist', function() {
assert(RSVP.hash);
});
specify('fulfilled only after all of the promise values are fulfilled', function(done) {
var firstResolved, secondResolved, firstResolver, secondResolver;
var first = new RSVP.Promise(function(resolve) {
firstResolver = resolve;
});
first.then(function() {
firstResolved = true;
});
var second = new RSVP.Promise(function(resolve) {
secondResolver = resolve;
});
second.then(function() {
secondResolved = true;
});
setTimeout(function() {
firstResolver(true);
}, 0);
setTimeout(function() {
secondResolver(true);
}, 0);
RSVP.hash({ first: first, second: second }).then(function(values) {
assert(values.first);
assert(values.second);
done();
});
});
specify('rejected as soon as a promise is rejected', function(done) {
var firstResolver, secondResolver;
var first = new RSVP.Promise(function(resolve, reject) {
firstResolver = { resolve: resolve, reject: reject };
});
var second = new RSVP.Promise(function(resolve, reject) {
secondResolver = { resolve: resolve, reject: reject };
});
setTimeout(function() {
firstResolver.reject({});
}, 0);
setTimeout(function() {
secondResolver.resolve(true);
}, 5000);
RSVP.hash({ first: first, second: second }).then(function() {
assert(false);
}, function() {
assert(first.isRejected);
assert(!second.isResolved);
done();
});
});
specify('resolves an empty hash passed to RSVP.all()', function(done) {
RSVP.hash({}).then(function(results) {
assert.deepEqual(results, {});
done();
});
});
specify('works with a mix of promises and thenables and non-promises', function(done) {
var promise = new RSVP.Promise(function(resolve) { resolve(1); });
var syncThenable = { then: function (onFulfilled) { onFulfilled(2); } };
var asyncThenable = { then: function (onFulfilled) { setTimeout(function() { onFulfilled(3); }, 0); } };
var nonPromise = 4;
RSVP.hash({ promise: promise, syncThenable: syncThenable, asyncThenable: asyncThenable, nonPromise: nonPromise }).then(function(results) {
assert.deepEqual(results, { promise: 1, syncThenable: 2, asyncThenable: 3, nonPromise: 4 });
done();
});
});
});
describe("RSVP.all", function() { describe("RSVP.all", function() {
it('should exist', function() { it('should exist', function() {
assert(RSVP.all); assert(RSVP.all);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment