Commit ab31b960 authored by tomhuda's avatar tomhuda

Improve exports and configuration

Fixes #31
parent 94aeaf66
...@@ -17,7 +17,7 @@ file "browser/rsvp.amd.js" => ["browser", "lib/rsvp.js"] do ...@@ -17,7 +17,7 @@ file "browser/rsvp.amd.js" => ["browser", "lib/rsvp.js"] do
open "browser/rsvp.amd.js", "w" do |file| open "browser/rsvp.amd.js", "w" do |file|
require "js_module_transpiler" require "js_module_transpiler"
converter = JsModuleTranspiler::Compiler.new(File.read("./lib/rsvp.js"), "rsvp") converter = JsModuleTranspiler::Compiler.new(File.read("./lib/rsvp.js"))
file.puts converter.to_amd file.puts converter.to_amd
end end
end end
......
define("rsvp", define(
[], ["exports"],
function() { function(__exports__) {
"use strict"; "use strict";
var config = {};
var browserGlobal = (typeof window !== 'undefined') ? window : {}; var browserGlobal = (typeof window !== 'undefined') ? window : {};
var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var RSVP, async; var RSVP;
if (typeof process !== 'undefined' && if (typeof process !== 'undefined' &&
{}.toString.call(process) === '[object process]') { {}.toString.call(process) === '[object process]') {
async = function(callback, binding) { config.async = function(callback, binding) {
process.nextTick(function() { process.nextTick(function() {
callback.call(binding); callback.call(binding);
}); });
...@@ -36,12 +38,12 @@ define("rsvp", ...@@ -36,12 +38,12 @@ define("rsvp",
observer = null; observer = null;
}); });
async = function(callback, binding) { config.async = function(callback, binding) {
queue.push([callback, binding]); queue.push([callback, binding]);
element.setAttribute('drainQueue', 'drainQueue'); element.setAttribute('drainQueue', 'drainQueue');
}; };
} else { } else {
async = function(callback, binding) { config.async = function(callback, binding) {
setTimeout(function() { setTimeout(function() {
callback.call(binding); callback.call(binding);
}, 1); }, 1);
...@@ -190,13 +192,13 @@ define("rsvp", ...@@ -190,13 +192,13 @@ define("rsvp",
var thenPromise = new Promise(); var thenPromise = new Promise();
if (this.isResolved) { if (this.isResolved) {
RSVP.async(function() { config.async(function() {
invokeCallback('resolve', thenPromise, done, { detail: this.resolvedValue }); invokeCallback('resolve', thenPromise, done, { detail: this.resolvedValue });
}, this); }, this);
} }
if (this.isRejected) { if (this.isRejected) {
RSVP.async(function() { config.async(function() {
invokeCallback('reject', thenPromise, fail, { detail: this.rejectedValue }); invokeCallback('reject', thenPromise, fail, { detail: this.rejectedValue });
}, this); }, this);
} }
...@@ -228,7 +230,7 @@ define("rsvp", ...@@ -228,7 +230,7 @@ define("rsvp",
}; };
function resolve(promise, value) { function resolve(promise, value) {
RSVP.async(function() { config.async(function() {
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
promise.isResolved = true; promise.isResolved = true;
promise.resolvedValue = value; promise.resolvedValue = value;
...@@ -236,7 +238,7 @@ define("rsvp", ...@@ -236,7 +238,7 @@ define("rsvp",
} }
function reject(promise, value) { function reject(promise, value) {
RSVP.async(function() { config.async(function() {
promise.trigger('promise:failed', { detail: value }); promise.trigger('promise:failed', { detail: value });
promise.isRejected = true; promise.isRejected = true;
promise.rejectedValue = value; promise.rejectedValue = value;
...@@ -244,39 +246,46 @@ define("rsvp", ...@@ -244,39 +246,46 @@ define("rsvp",
} }
function all(promises) { function all(promises) {
var i, results = []; var i, results = [];
var allPromise = new Promise(); var allPromise = new Promise();
var remaining = promises.length; var remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
allPromise.resolve([]); 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);
}; };
for (i = 0; i < remaining; i++) { for (i = 0; i < remaining; i++) {
promises[i].then(resolver(i), reject); promises[i].then(resolver(i), reject);
} }
return allPromise; return allPromise;
} }
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, all: all, raiseOnUncaughtExceptions: true }; function configure(name, value) {
return RSVP; config[name] = value;
}
__exports__.Promise = Promise;
__exports__.Event = Event;
__exports__.EventTarget = EventTarget;
__exports__.all = all;
__exports__.configure = configure;
}); });
(function(exports) { (function(exports) {
"use strict"; "use strict";
var config = {};
var browserGlobal = (typeof window !== 'undefined') ? window : {}; var browserGlobal = (typeof window !== 'undefined') ? window : {};
var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var RSVP, async; var RSVP;
if (typeof process !== 'undefined' && if (typeof process !== 'undefined' &&
{}.toString.call(process) === '[object process]') { {}.toString.call(process) === '[object process]') {
async = function(callback, binding) { config.async = function(callback, binding) {
process.nextTick(function() { process.nextTick(function() {
callback.call(binding); callback.call(binding);
}); });
...@@ -34,12 +36,12 @@ ...@@ -34,12 +36,12 @@
observer = null; observer = null;
}); });
async = function(callback, binding) { config.async = function(callback, binding) {
queue.push([callback, binding]); queue.push([callback, binding]);
element.setAttribute('drainQueue', 'drainQueue'); element.setAttribute('drainQueue', 'drainQueue');
}; };
} else { } else {
async = function(callback, binding) { config.async = function(callback, binding) {
setTimeout(function() { setTimeout(function() {
callback.call(binding); callback.call(binding);
}, 1); }, 1);
...@@ -188,13 +190,13 @@ ...@@ -188,13 +190,13 @@
var thenPromise = new Promise(); var thenPromise = new Promise();
if (this.isResolved) { if (this.isResolved) {
RSVP.async(function() { config.async(function() {
invokeCallback('resolve', thenPromise, done, { detail: this.resolvedValue }); invokeCallback('resolve', thenPromise, done, { detail: this.resolvedValue });
}, this); }, this);
} }
if (this.isRejected) { if (this.isRejected) {
RSVP.async(function() { config.async(function() {
invokeCallback('reject', thenPromise, fail, { detail: this.rejectedValue }); invokeCallback('reject', thenPromise, fail, { detail: this.rejectedValue });
}, this); }, this);
} }
...@@ -226,7 +228,7 @@ ...@@ -226,7 +228,7 @@
}; };
function resolve(promise, value) { function resolve(promise, value) {
RSVP.async(function() { config.async(function() {
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
promise.isResolved = true; promise.isResolved = true;
promise.resolvedValue = value; promise.resolvedValue = value;
...@@ -234,7 +236,7 @@ ...@@ -234,7 +236,7 @@
} }
function reject(promise, value) { function reject(promise, value) {
RSVP.async(function() { config.async(function() {
promise.trigger('promise:failed', { detail: value }); promise.trigger('promise:failed', { detail: value });
promise.isRejected = true; promise.isRejected = true;
promise.rejectedValue = value; promise.rejectedValue = value;
...@@ -242,39 +244,46 @@ ...@@ -242,39 +244,46 @@
} }
function all(promises) { function all(promises) {
var i, results = []; var i, results = [];
var allPromise = new Promise(); var allPromise = new Promise();
var remaining = promises.length; var remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
allPromise.resolve([]); 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);
}; };
for (i = 0; i < remaining; i++) { for (i = 0; i < remaining; i++) {
promises[i].then(resolver(i), reject); promises[i].then(resolver(i), reject);
} }
return allPromise; return allPromise;
} }
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, all: all, raiseOnUncaughtExceptions: true }; function configure(name, value) {
exports.RSVP = RSVP; config[name] = value;
})(window); }
exports.Promise = Promise;
exports.Event = Event;
exports.EventTarget = EventTarget;
exports.all = all;
exports.configure = configure;
})(window.RSVP = {});
(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); (function(e){"use strict";function v(e,n){t.async(function(){e.trigger("promise:resolved",{detail:n}),e.isResolved=!0,e.resolvedValue=n})}function m(e,n){t.async(function(){e.trigger("promise:failed",{detail:n}),e.isRejected=!0,e.rejectedValue=n})}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}function y(e,n){t[e]=n}var t={},n=typeof window!="undefined"?window:{},r=n.MutationObserver||n.WebKitMutationObserver,i;if(typeof process!="undefined"&&{}.toString.call(process)==="[object process]")t.async=function(e,t){process.nextTick(function(){e.call(t)})};else if(r){var s=[],o=new r(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}),t.async=function(e,t){s.push([e,t]),u.setAttribute("drainQueue","drainQueue")}}else t.async=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,n){var r=new h;return this.isResolved&&t.async(function(){d("resolve",r,e,{detail:this.resolvedValue})},this),this.isRejected&&t.async(function(){d("reject",r,n,{detail:this.rejectedValue})},this),this.on("promise:resolved",function(t){d("resolve",r,e,t)}),this.on("promise:failed",function(e){d("reject",r,n,e)}),r},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),e.Promise=h,e.Event=a,e.EventTarget=c,e.all=g,e.configure=y})(window.RSVP={});
var config = {};
var browserGlobal = (typeof window !== 'undefined') ? window : {}; var browserGlobal = (typeof window !== 'undefined') ? window : {};
var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var RSVP, async; var RSVP;
if (typeof process !== 'undefined' && if (typeof process !== 'undefined' &&
{}.toString.call(process) === '[object process]') { {}.toString.call(process) === '[object process]') {
async = function(callback, binding) { config.async = function(callback, binding) {
process.nextTick(function() { process.nextTick(function() {
callback.call(binding); callback.call(binding);
}); });
...@@ -32,12 +34,12 @@ if (typeof process !== 'undefined' && ...@@ -32,12 +34,12 @@ if (typeof process !== 'undefined' &&
observer = null; observer = null;
}); });
async = function(callback, binding) { config.async = function(callback, binding) {
queue.push([callback, binding]); queue.push([callback, binding]);
element.setAttribute('drainQueue', 'drainQueue'); element.setAttribute('drainQueue', 'drainQueue');
}; };
} else { } else {
async = function(callback, binding) { config.async = function(callback, binding) {
setTimeout(function() { setTimeout(function() {
callback.call(binding); callback.call(binding);
}, 1); }, 1);
...@@ -186,13 +188,13 @@ Promise.prototype = { ...@@ -186,13 +188,13 @@ Promise.prototype = {
var thenPromise = new Promise(); var thenPromise = new Promise();
if (this.isResolved) { if (this.isResolved) {
RSVP.async(function() { config.async(function() {
invokeCallback('resolve', thenPromise, done, { detail: this.resolvedValue }); invokeCallback('resolve', thenPromise, done, { detail: this.resolvedValue });
}, this); }, this);
} }
if (this.isRejected) { if (this.isRejected) {
RSVP.async(function() { config.async(function() {
invokeCallback('reject', thenPromise, fail, { detail: this.rejectedValue }); invokeCallback('reject', thenPromise, fail, { detail: this.rejectedValue });
}, this); }, this);
} }
...@@ -224,7 +226,7 @@ Promise.prototype = { ...@@ -224,7 +226,7 @@ Promise.prototype = {
}; };
function resolve(promise, value) { function resolve(promise, value) {
RSVP.async(function() { config.async(function() {
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
promise.isResolved = true; promise.isResolved = true;
promise.resolvedValue = value; promise.resolvedValue = value;
...@@ -232,7 +234,7 @@ function resolve(promise, value) { ...@@ -232,7 +234,7 @@ function resolve(promise, value) {
} }
function reject(promise, value) { function reject(promise, value) {
RSVP.async(function() { config.async(function() {
promise.trigger('promise:failed', { detail: value }); promise.trigger('promise:failed', { detail: value });
promise.isRejected = true; promise.isRejected = true;
promise.rejectedValue = value; promise.rejectedValue = value;
...@@ -240,38 +242,41 @@ function reject(promise, value) { ...@@ -240,38 +242,41 @@ function reject(promise, value) {
} }
function all(promises) { function all(promises) {
var i, results = []; var i, results = [];
var allPromise = new Promise(); var allPromise = new Promise();
var remaining = promises.length; var remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
allPromise.resolve([]); 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);
}; };
for (i = 0; i < remaining; i++) { for (i = 0; i < remaining; i++) {
promises[i].then(resolver(i), reject); promises[i].then(resolver(i), reject);
} }
return allPromise; return allPromise;
} }
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, all: all, raiseOnUncaughtExceptions: true }; function configure(name, value) {
export = RSVP; config[name] = value;
}
export { Promise, Event, EventTarget, all, configure };
"use strict"; "use strict";
var config = {};
var browserGlobal = (typeof window !== 'undefined') ? window : {}; var browserGlobal = (typeof window !== 'undefined') ? window : {};
var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var RSVP, async; var RSVP;
if (typeof process !== 'undefined' && if (typeof process !== 'undefined' &&
{}.toString.call(process) === '[object process]') { {}.toString.call(process) === '[object process]') {
async = function(callback, binding) { config.async = function(callback, binding) {
process.nextTick(function() { process.nextTick(function() {
callback.call(binding); callback.call(binding);
}); });
...@@ -33,12 +35,12 @@ if (typeof process !== 'undefined' && ...@@ -33,12 +35,12 @@ if (typeof process !== 'undefined' &&
observer = null; observer = null;
}); });
async = function(callback, binding) { config.async = function(callback, binding) {
queue.push([callback, binding]); queue.push([callback, binding]);
element.setAttribute('drainQueue', 'drainQueue'); element.setAttribute('drainQueue', 'drainQueue');
}; };
} else { } else {
async = function(callback, binding) { config.async = function(callback, binding) {
setTimeout(function() { setTimeout(function() {
callback.call(binding); callback.call(binding);
}, 1); }, 1);
...@@ -187,13 +189,13 @@ Promise.prototype = { ...@@ -187,13 +189,13 @@ Promise.prototype = {
var thenPromise = new Promise(); var thenPromise = new Promise();
if (this.isResolved) { if (this.isResolved) {
RSVP.async(function() { config.async(function() {
invokeCallback('resolve', thenPromise, done, { detail: this.resolvedValue }); invokeCallback('resolve', thenPromise, done, { detail: this.resolvedValue });
}, this); }, this);
} }
if (this.isRejected) { if (this.isRejected) {
RSVP.async(function() { config.async(function() {
invokeCallback('reject', thenPromise, fail, { detail: this.rejectedValue }); invokeCallback('reject', thenPromise, fail, { detail: this.rejectedValue });
}, this); }, this);
} }
...@@ -225,7 +227,7 @@ Promise.prototype = { ...@@ -225,7 +227,7 @@ Promise.prototype = {
}; };
function resolve(promise, value) { function resolve(promise, value) {
RSVP.async(function() { config.async(function() {
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
promise.isResolved = true; promise.isResolved = true;
promise.resolvedValue = value; promise.resolvedValue = value;
...@@ -233,7 +235,7 @@ function resolve(promise, value) { ...@@ -233,7 +235,7 @@ function resolve(promise, value) {
} }
function reject(promise, value) { function reject(promise, value) {
RSVP.async(function() { config.async(function() {
promise.trigger('promise:failed', { detail: value }); promise.trigger('promise:failed', { detail: value });
promise.isRejected = true; promise.isRejected = true;
promise.rejectedValue = value; promise.rejectedValue = value;
...@@ -241,38 +243,45 @@ function reject(promise, value) { ...@@ -241,38 +243,45 @@ function reject(promise, value) {
} }
function all(promises) { function all(promises) {
var i, results = []; var i, results = [];
var allPromise = new Promise(); var allPromise = new Promise();
var remaining = promises.length; var remaining = promises.length;
if (remaining === 0) { if (remaining === 0) {
allPromise.resolve([]); 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);
}; };
for (i = 0; i < remaining; i++) { for (i = 0; i < remaining; i++) {
promises[i].then(resolver(i), reject); promises[i].then(resolver(i), reject);
} }
return allPromise; return allPromise;
} }
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
RSVP = { async: async, Promise: Promise, Event: Event, EventTarget: EventTarget, all: all, raiseOnUncaughtExceptions: true }; function configure(name, value) {
module.exports = RSVP; config[name] = value;
}
exports.Promise = Promise;
exports.Event = Event;
exports.EventTarget = EventTarget;
exports.all = all;
exports.configure = configure;
"use strict"; "use strict";
var config = {};
var browserGlobal = (typeof window !== 'undefined') ? window : {}; var browserGlobal = (typeof window !== 'undefined') ? window : {};
var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; var MutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var RSVP, async; var RSVP;
if (typeof process !== 'undefined' && if (typeof process !== 'undefined' &&
{}.toString.call(process) === '[object process]') { {}.toString.call(process) === '[object process]') {
async = function(callback, binding) { config.async = function(callback, binding) {
process.nextTick(function() { process.nextTick(function() {
callback.call(binding); callback.call(binding);
}); });
...@@ -33,12 +35,12 @@ if (typeof process !== 'undefined' && ...@@ -33,12 +35,12 @@ if (typeof process !== 'undefined' &&
observer = null; observer = null;
}); });
async = function(callback, binding) { config.async = function(callback, binding) {
queue.push([callback, binding]); queue.push([callback, binding]);
element.setAttribute('drainQueue', 'drainQueue'); element.setAttribute('drainQueue', 'drainQueue');
}; };
} else { } else {
async = function(callback, binding) { config.async = function(callback, binding) {
setTimeout(function() { setTimeout(function() {
callback.call(binding); callback.call(binding);
}, 1); }, 1);
...@@ -187,13 +189,13 @@ Promise.prototype = { ...@@ -187,13 +189,13 @@ Promise.prototype = {
var thenPromise = new Promise(); var thenPromise = new Promise();
if (this.isResolved) { if (this.isResolved) {
RSVP.async(function() { config.async(function() {
invokeCallback('resolve', thenPromise, done, { detail: this.resolvedValue }); invokeCallback('resolve', thenPromise, done, { detail: this.resolvedValue });
}, this); }, this);
} }
if (this.isRejected) { if (this.isRejected) {
RSVP.async(function() { config.async(function() {
invokeCallback('reject', thenPromise, fail, { detail: this.rejectedValue }); invokeCallback('reject', thenPromise, fail, { detail: this.rejectedValue });
}, this); }, this);
} }
...@@ -225,7 +227,7 @@ Promise.prototype = { ...@@ -225,7 +227,7 @@ Promise.prototype = {
}; };
function resolve(promise, value) { function resolve(promise, value) {
RSVP.async(function() { config.async(function() {
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
promise.isResolved = true; promise.isResolved = true;
promise.resolvedValue = value; promise.resolvedValue = value;
...@@ -233,7 +235,7 @@ function resolve(promise, value) { ...@@ -233,7 +235,7 @@ function resolve(promise, value) {
} }
function reject(promise, value) { function reject(promise, value) {
RSVP.async(function() { config.async(function() {
promise.trigger('promise:failed', { detail: value }); promise.trigger('promise:failed', { detail: value });
promise.isRejected = true; promise.isRejected = true;
promise.rejectedValue = value; promise.rejectedValue = value;
...@@ -241,32 +243,44 @@ function reject(promise, value) { ...@@ -241,32 +243,44 @@ function reject(promise, value) {
} }
function all(promises) { function all(promises) {
var i, results = []; var i, results = [];
var allPromise = new Promise(); var allPromise = new Promise();
var remaining = promises.length; var remaining = promises.length;
var resolver = function(index) { if (remaining === 0) {
return function(value) { allPromise.resolve([]);
resolve(index, value); }
};
}; var resolver = function(index) {
var resolve = function(index, value) { return function(value) {
results[index] = value; resolve(index, value);
if (--remaining === 0) { };
allPromise.resolve(results); };
}
}; var resolve = function(index, value) {
var reject = function(error) { results[index] = value;
allPromise.reject(error); if (--remaining === 0) {
}; allPromise.resolve(results);
}
for (i = 0; i < remaining; i++) { };
promises[i].then(resolver(i), reject);
} var reject = function(error) {
return allPromise; 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, all: all, raiseOnUncaughtExceptions: true }; function configure(name, value) {
module.exports = RSVP; config[name] = value;
}
exports.Promise = Promise;
exports.Event = Event;
exports.EventTarget = EventTarget;
exports.configure = configure;
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