Commit 94aeaf66 authored by tomhuda's avatar tomhuda

Fixes issue when passing [] to RSVP.all()

parent 21e74d5b
...@@ -243,8 +243,40 @@ define("rsvp", ...@@ -243,8 +243,40 @@ define("rsvp",
}); });
} }
function all(promises) {
var i, results = [];
var allPromise = new Promise();
var remaining = promises.length;
if (remaining === 0) {
allPromise.resolve([]);
}
var resolver = function(index) {
return function(value) {
resolve(index, value);
};
};
var resolve = function(index, value) {
results[index] = value;
if (--remaining === 0) {
allPromise.resolve(results);
}
};
var reject = function(error) {
allPromise.reject(error);
};
for (i = 0; i < remaining; i++) {
promises[i].then(resolver(i), reject);
}
return allPromise;
}
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, raiseOnUncaughtExceptions: true }; RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, all: all, raiseOnUncaughtExceptions: true };
return RSVP; return RSVP;
}); });
...@@ -241,8 +241,40 @@ ...@@ -241,8 +241,40 @@
}); });
} }
function all(promises) {
var i, results = [];
var allPromise = new Promise();
var remaining = promises.length;
if (remaining === 0) {
allPromise.resolve([]);
}
var resolver = function(index) {
return function(value) {
resolve(index, value);
};
};
var resolve = function(index, value) {
results[index] = value;
if (--remaining === 0) {
allPromise.resolve(results);
}
};
var reject = function(error) {
allPromise.reject(error);
};
for (i = 0; i < remaining; i++) {
promises[i].then(resolver(i), reject);
}
return allPromise;
}
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, raiseOnUncaughtExceptions: true }; RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, all: all, raiseOnUncaughtExceptions: true };
exports.RSVP = RSVP; exports.RSVP = RSVP;
})(window); })(window);
(function(e){"use strict";function v(e,t){r.async(function(){e.trigger("promise:resolved",{detail:t}),e.isResolved=!0,e.resolvedValue=t})}function m(e,t){r.async(function(){e.trigger("promise:failed",{detail:t}),e.isRejected=!0,e.rejectedValue=t})}var t=typeof window!="undefined"?window:{},n=t.MutationObserver||t.WebKitMutationObserver,r,i;if(typeof process!="undefined"&&{}.toString.call(process)==="[object process]")i=function(e,t){process.nextTick(function(){e.call(t)})};else if(n){var s=[],o=new n(function(){var e=s.slice();s=[],e.forEach(function(e){var t=e[0],n=e[1];t.call(n)})}),u=document.createElement("div");o.observe(u,{attributes:!0}),window.addEventListener("unload",function(){o.disconnect(),o=null}),i=function(e,t){s.push([e,t]),u.setAttribute("drainQueue","drainQueue")}}else i=function(e,t){setTimeout(function(){e.call(t)},1)};var a=function(e,t){this.type=e;for(var n in t){if(!t.hasOwnProperty(n))continue;this[n]=t[n]}},f=function(e,t){for(var n=0,r=e.length;n<r;n++)if(e[n][0]===t)return n;return-1},l=function(e){var t=e._promiseCallbacks;return t||(t=e._promiseCallbacks={}),t},c={mixin:function(e){return e.on=this.on,e.off=this.off,e.trigger=this.trigger,e},on:function(e,t,n){var r=l(this),i,s;e=e.split(/\s+/),n=n||this;while(s=e.shift())i=r[s],i||(i=r[s]=[]),f(i,t)===-1&&i.push([t,n])},off:function(e,t){var n=l(this),r,i,s;e=e.split(/\s+/);while(i=e.shift()){if(!t){n[i]=[];continue}r=n[i],s=f(r,t),s!==-1&&r.splice(s,1)}},trigger:function(e,t){var n=l(this),r,i,s,o,u;if(r=n[e])for(var f=0,c=r.length;f<c;f++)i=r[f],s=i[0],o=i[1],typeof t!="object"&&(t={detail:t}),u=new a(e,t),s.call(o,u)}},h=function(){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)},p=function(){},d=function(e,t,n,r){var i=typeof n=="function",s,o,u,a;if(i)try{s=n(r.detail),u=!0}catch(f){a=!0,o=f}else s=r.detail,u=!0;s&&typeof s.then=="function"?s.then(function(e){t.resolve(e)},function(e){t.reject(e)}):i&&u?t.resolve(s):a?t.reject(o):t[e](s)};h.prototype={then:function(e,t){var n=new h;return this.isResolved&&r.async(function(){d("resolve",n,e,{detail:this.resolvedValue})},this),this.isRejected&&r.async(function(){d("reject",n,t,{detail:this.rejectedValue})},this),this.on("promise:resolved",function(t){d("resolve",n,e,t)}),this.on("promise:failed",function(e){d("reject",n,t,e)}),n},resolve:function(e){v(this,e),this.resolve=p,this.reject=p},reject:function(e){m(this,e),this.resolve=p,this.reject=p}},c.mixin(h.prototype),r={async:i,Promise:h,Event:a,EventTarget:c,raiseOnUncaughtExceptions:!0},e.RSVP=r})(window); (function(e){"use strict";function v(e,t){r.async(function(){e.trigger("promise:resolved",{detail:t}),e.isResolved=!0,e.resolvedValue=t})}function m(e,t){r.async(function(){e.trigger("promise:failed",{detail:t}),e.isRejected=!0,e.rejectedValue=t})}function g(e){var t,n=[],r=new h,i=e.length;i===0&&r.resolve([]);var s=function(e){return function(t){o(e,t)}},o=function(e,t){n[e]=t,--i===0&&r.resolve(n)},u=function(e){r.reject(e)};for(t=0;t<i;t++)e[t].then(s(t),u);return r}var t=typeof window!="undefined"?window:{},n=t.MutationObserver||t.WebKitMutationObserver,r,i;if(typeof process!="undefined"&&{}.toString.call(process)==="[object process]")i=function(e,t){process.nextTick(function(){e.call(t)})};else if(n){var s=[],o=new n(function(){var e=s.slice();s=[],e.forEach(function(e){var t=e[0],n=e[1];t.call(n)})}),u=document.createElement("div");o.observe(u,{attributes:!0}),window.addEventListener("unload",function(){o.disconnect(),o=null}),i=function(e,t){s.push([e,t]),u.setAttribute("drainQueue","drainQueue")}}else i=function(e,t){setTimeout(function(){e.call(t)},1)};var a=function(e,t){this.type=e;for(var n in t){if(!t.hasOwnProperty(n))continue;this[n]=t[n]}},f=function(e,t){for(var n=0,r=e.length;n<r;n++)if(e[n][0]===t)return n;return-1},l=function(e){var t=e._promiseCallbacks;return t||(t=e._promiseCallbacks={}),t},c={mixin:function(e){return e.on=this.on,e.off=this.off,e.trigger=this.trigger,e},on:function(e,t,n){var r=l(this),i,s;e=e.split(/\s+/),n=n||this;while(s=e.shift())i=r[s],i||(i=r[s]=[]),f(i,t)===-1&&i.push([t,n])},off:function(e,t){var n=l(this),r,i,s;e=e.split(/\s+/);while(i=e.shift()){if(!t){n[i]=[];continue}r=n[i],s=f(r,t),s!==-1&&r.splice(s,1)}},trigger:function(e,t){var n=l(this),r,i,s,o,u;if(r=n[e])for(var f=0,c=r.length;f<c;f++)i=r[f],s=i[0],o=i[1],typeof t!="object"&&(t={detail:t}),u=new a(e,t),s.call(o,u)}},h=function(){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)},p=function(){},d=function(e,t,n,r){var i=typeof n=="function",s,o,u,a;if(i)try{s=n(r.detail),u=!0}catch(f){a=!0,o=f}else s=r.detail,u=!0;s&&typeof s.then=="function"?s.then(function(e){t.resolve(e)},function(e){t.reject(e)}):i&&u?t.resolve(s):a?t.reject(o):t[e](s)};h.prototype={then:function(e,t){var n=new h;return this.isResolved&&r.async(function(){d("resolve",n,e,{detail:this.resolvedValue})},this),this.isRejected&&r.async(function(){d("reject",n,t,{detail:this.rejectedValue})},this),this.on("promise:resolved",function(t){d("resolve",n,e,t)}),this.on("promise:failed",function(e){d("reject",n,t,e)}),n},resolve:function(e){v(this,e),this.resolve=p,this.reject=p},reject:function(e){m(this,e),this.resolve=p,this.reject=p}},c.mixin(h.prototype),r={async:i,Promise:h,Event:a,EventTarget:c,all:g,raiseOnUncaughtExceptions:!0},e.RSVP=r})(window);
...@@ -244,17 +244,23 @@ function all(promises) { ...@@ -244,17 +244,23 @@ function all(promises) {
var allPromise = new Promise(); var allPromise = new Promise();
var remaining = promises.length; var remaining = promises.length;
if (remaining === 0) {
allPromise.resolve([]);
}
var resolver = function(index) { var resolver = function(index) {
return function(value) { return function(value) {
resolve(index, value); resolve(index, value);
}; };
}; };
var resolve = function(index, value) { var resolve = function(index, value) {
results[index] = value; results[index] = value;
if (--remaining === 0) { if (--remaining === 0) {
allPromise.resolve(results); allPromise.resolve(results);
} }
}; };
var reject = function(error) { var reject = function(error) {
allPromise.reject(error); allPromise.reject(error);
}; };
......
...@@ -240,7 +240,39 @@ function reject(promise, value) { ...@@ -240,7 +240,39 @@ function reject(promise, value) {
}); });
} }
function all(promises) {
var i, results = [];
var allPromise = new Promise();
var remaining = promises.length;
if (remaining === 0) {
allPromise.resolve([]);
}
var resolver = function(index) {
return function(value) {
resolve(index, value);
};
};
var resolve = function(index, value) {
results[index] = value;
if (--remaining === 0) {
allPromise.resolve(results);
}
};
var reject = function(error) {
allPromise.reject(error);
};
for (i = 0; i < remaining; i++) {
promises[i].then(resolver(i), reject);
}
return allPromise;
}
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, raiseOnUncaughtExceptions: true }; RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, all: all, raiseOnUncaughtExceptions: true };
module.exports = RSVP; module.exports = RSVP;
...@@ -62,5 +62,12 @@ describe("RSVP extensions", function() { ...@@ -62,5 +62,12 @@ describe("RSVP extensions", function() {
done(); done();
}); });
}); });
specify('resolves an empty array passed to RSVP.all()', function(done) {
RSVP.all([]).then(function(results) {
assert(results.length === 0);
done();
});
});
}); });
}); });
...@@ -240,7 +240,33 @@ function reject(promise, value) { ...@@ -240,7 +240,33 @@ function reject(promise, value) {
}); });
} }
function all(promises) {
var i, results = [];
var allPromise = new Promise();
var remaining = promises.length;
var resolver = function(index) {
return function(value) {
resolve(index, value);
};
};
var resolve = function(index, value) {
results[index] = value;
if (--remaining === 0) {
allPromise.resolve(results);
}
};
var reject = function(error) {
allPromise.reject(error);
};
for (i = 0; i < remaining; i++) {
promises[i].then(resolver(i), reject);
}
return allPromise;
}
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget }; RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, all: all, raiseOnUncaughtExceptions: true };
module.exports = RSVP; module.exports = RSVP;
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