Commit ec8dce36 authored by Yehuda Katz's avatar Yehuda Katz

Add RSVP.configure('onerror')

This function is invoked when an unhandled exception is thrown.
parent da484cfd
...@@ -38,6 +38,9 @@ define("rsvp/all", ...@@ -38,6 +38,9 @@ define("rsvp/all",
var Promise = __dependency1__.Promise; var Promise = __dependency1__.Promise;
function all(promises) { function all(promises) {
if(toString.call(promises) !== "[object Array]") {
throw new TypeError('You must pass an array to all.');
}
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var results = [], remaining = promises.length, var results = [], remaining = promises.length,
promise; promise;
...@@ -318,7 +321,51 @@ define("rsvp/hash", ...@@ -318,7 +321,51 @@ define("rsvp/hash",
__exports__.hash = hash; __exports__.hash = hash;
}); });
define("rsvp/node",
["rsvp/promise","rsvp/all","exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var Promise = __dependency1__.Promise;
var all = __dependency2__.all;
function makeNodeCallbackFor(resolve, reject) {
return function (error, value) {
if (error) {
reject(error);
} else if (arguments.length > 2) {
resolve(Array.prototype.slice.call(arguments, 1));
} else {
resolve(value);
}
};
}
function denodeify(nodeFunc) {
return function() {
var nodeArgs = Array.prototype.slice.call(arguments), resolve, reject;
var thisArg = this;
var promise = new Promise(function(nodeResolve, nodeReject) {
resolve = nodeResolve;
reject = nodeReject;
});
all(nodeArgs).then(function(nodeArgs) {
nodeArgs.push(makeNodeCallbackFor(resolve, reject));
try {
nodeFunc.apply(thisArg, nodeArgs);
} catch(e) {
reject(e);
}
});
return promise;
};
}
__exports__.denodeify = denodeify;
});
define("rsvp/promise", define("rsvp/promise",
["rsvp/config","rsvp/events","exports"], ["rsvp/config","rsvp/events","exports"],
...@@ -367,6 +414,8 @@ define("rsvp/promise", ...@@ -367,6 +414,8 @@ define("rsvp/promise",
this.trigger('error', { detail: event.detail }); this.trigger('error', { detail: event.detail });
}, this); }, this);
this.on('error', onerror);
try { try {
resolver(resolvePromise, rejectPromise); resolver(resolvePromise, rejectPromise);
} catch(e) { } catch(e) {
...@@ -374,6 +423,12 @@ define("rsvp/promise", ...@@ -374,6 +423,12 @@ define("rsvp/promise",
} }
}; };
function onerror(event) {
if (config.onerror) {
config.onerror(event.detail);
}
}
var invokeCallback = function(type, promise, callback, event) { var invokeCallback = function(type, promise, callback, event) {
var hasCallback = isFunction(callback), var hasCallback = isFunction(callback),
value, error, succeeded, failed; value, error, succeeded, failed;
...@@ -408,6 +463,8 @@ define("rsvp/promise", ...@@ -408,6 +463,8 @@ define("rsvp/promise",
constructor: Promise, constructor: Promise,
then: function(done, fail) { then: function(done, fail) {
this.off('error', onerror);
var thenPromise = new Promise(function() {}); var thenPromise = new Promise(function() {});
if (this.isFulfilled) { if (this.isFulfilled) {
......
(function(){var e,t;(function(){var r={},n={};e=function(e,t,n){r[e]={deps:t,callback:n}};t=function(e){if(n[e]){return n[e]}n[e]={};var i=r[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 v=s.apply(this,f);return n[e]=c||v}})();e("rsvp/all",["rsvp/promise","exports"],function(e,t){"use strict";var r=e.Promise;function n(e){return new r(function(t,r){var n=[],i=e.length,o;if(i===0){t([])}function s(e){return function(t){f(e,t)}}function f(e,r){n[e]=r;if(--i===0){t(n)}}for(var c=0;c<e.length;c++){o=e[c];if(o&&typeof o.then==="function"){o.then(s(c),r)}else{f(c,o)}}})}t.all=n});e("rsvp/async",["exports"],function(e){"use strict";var t=typeof window!=="undefined"?window:{};var r=t.MutationObserver||t.WebKitMutationObserver;var n;if(typeof process!=="undefined"&&{}.toString.call(process)==="[object process]"){n=function(e,t){process.nextTick(function(){e.call(t)})}}else if(r){var i=[];var o=new r(function(){var e=i.slice();i=[];e.forEach(function(e){var t=e[0],r=e[1];t.call(r)})});var s=document.createElement("div");o.observe(s,{attributes:true});window.addEventListener("unload",function(){o.disconnect();o=null});n=function(e,t){i.push([e,t]);s.setAttribute("drainQueue","drainQueue")}}else{n=function(e,t){setTimeout(function(){e.call(t)},1)}}e.async=n});e("rsvp/config",["rsvp/async","exports"],function(e,t){"use strict";var r=e.async;var n={};n.async=r;t.config=n});e("rsvp/defer",["rsvp/promise","exports"],function(e,t){"use strict";var r=e.Promise;function n(){var e={};var t=new r(function(t,r){e.resolve=t;e.reject=r});e.promise=t;return e}t.defer=n});e("rsvp/events",["exports"],function(e){"use strict";var t=function(e,t){this.type=e;for(var r in t){if(!t.hasOwnProperty(r)){continue}this[r]=t[r]}};var r=function(e,t){for(var r=0,n=e.length;r<n;r++){if(e[r][0]===t){return r}}return-1};var n=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=n(this),s,f;e=e.split(/\s+/);i=i||this;while(f=e.shift()){s=o[f];if(!s){s=o[f]=[]}if(r(s,t)===-1){s.push([t,i])}}},off:function(e,t){var i=n(this),o,s,f;e=e.split(/\s+/);while(s=e.shift()){if(!t){i[s]=[];continue}o=i[s];f=r(o,t);if(f!==-1){o.splice(f,1)}}},trigger:function(e,r){var i=n(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 r!=="object"){r={detail:r}}u=new t(e,r);f.call(c,u)}}}};e.EventTarget=i});e("rsvp/hash",["rsvp/defer","exports"],function(e,t){"use strict";var r=e.defer;function n(e){var t=0;for(var r in e){t++}return t}function i(e){var t={},i=r(),o=n(e);if(o===0){i.resolve({})}var s=function(e){return function(t){f(e,t)}};var f=function(e,r){t[e]=r;if(--o===0){i.resolve(t)}};var c=function(e){i.reject(e)};for(var u in e){if(e[u]&&typeof e[u].then==="function"){e[u].then(s(u),c)}else{f(u,e[u])}}return i.promise}t.hash=i});e("rsvp/promise",["rsvp/config","rsvp/events","exports"],function(e,t,r){"use strict";var n=e.config;var i=t.EventTarget;function o(e){return s(e)||typeof e==="object"&&e!==null}function s(e){return typeof e==="function"}var f=function(e){var t=this,r=false;if(typeof e!=="function"){throw new TypeError("You must pass a resolver function as the sole argument to the promise constructor")}if(!(t instanceof f)){return new f(e)}var n=function(e){if(r){return}r=true;u(t,e)};var i=function(e){if(r){return}r=true;l(t,e)};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);try{e(n,i)}catch(o){i(o)}};var c=function(e,t,r,n){var i=s(r),o,f,c,v;if(i){try{o=r(n.detail);c=true}catch(p){v=true;f=p}}else{o=n.detail;c=true}if(a(t,o)){return}else if(i&&c){u(t,o)}else if(v){l(t,f)}else if(e==="resolve"){u(t,o)}else if(e==="reject"){l(t,o)}};f.prototype={constructor:f,then:function(e,t){var r=new f(function(){});if(this.isFulfilled){n.async(function(){c("resolve",r,e,{detail:this.fulfillmentValue})},this)}if(this.isRejected){n.async(function(){c("reject",r,t,{detail:this.rejectedReason})},this)}this.on("promise:resolved",function(t){c("resolve",r,e,t)});this.on("promise:failed",function(e){c("reject",r,t,e)});return r}};i.mixin(f.prototype);function u(e,t){if(e===t){v(e,t)}else if(!a(e,t)){v(e,t)}}function a(e,t){var r=null;if(o(t)){try{r=t.then}catch(n){l(e,n);return true}if(s(r)){try{r.call(t,function(r){if(t!==r){u(e,r)}else{v(e,r)}},function(t){l(e,t)})}catch(n){l(e,n)}return true}}return false}function v(e,t){n.async(function(){e.trigger("promise:resolved",{detail:t});e.isFulfilled=true;e.fulfillmentValue=t})}function l(e,t){n.async(function(){e.trigger("promise:failed",{detail:t});e.isRejected=true;e.rejectedReason=t})}r.Promise=f});e("rsvp/reject",["rsvp/promise","exports"],function(e,t){"use strict";var r=e.Promise;function n(e){return typeof e==="function"||typeof e==="object"&&e!==null}function i(e){return new r(function(t,r){r(e)})}t.reject=i});e("rsvp/resolve",["rsvp/promise","exports"],function(e,t){"use strict";var r=e.Promise;function n(e){return typeof e==="function"||typeof e==="object"&&e!==null}function i(e){if(e instanceof r){return e}var t=new r(function(t,r){var i;try{if(n(e)){i=e.then;if(typeof i==="function"){i.call(e,t,r)}else{t(e)}}else{t(e)}}catch(o){r(o)}});return t}t.resolve=i});e("rsvp",["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","rsvp/resolve","rsvp/reject","exports"],function(e,t,r,n,i,o,s,f,c,u){"use strict";var a=e.EventTarget;var v=t.Promise;var l=r.denodeify;var p=n.all;var h=i.hash;var d=o.defer;var y=s.config;var m=f.resolve;var g=c.reject;function w(e,t){y[e]=t}u.Promise=v;u.EventTarget=a;u.all=p;u.hash=h;u.defer=d;u.denodeify=l;u.configure=w;u.resolve=m;u.reject=g});window.RSVP=t("rsvp")})(); !function(){var e,r;!function(){var t={},n={};e=function(e,r,n){t[e]={deps:r,callback:n}};r=function(e){if(n[e]){return n[e]}n[e]={};var i=t[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(r(o[u]))}}var l=s.apply(this,f);return n[e]=c||l}}();e("rsvp/all",["rsvp/promise","exports"],function(e,r){"use strict";var t=e.Promise;function n(e){if(toString.call(e)!=="[object Array]"){throw new TypeError("You must pass an array to all.")}return new t(function(r,t){var n=[],i=e.length,o;if(i===0){r([])}function s(e){return function(r){f(e,r)}}function f(e,t){n[e]=t;if(--i===0){r(n)}}for(var c=0;c<e.length;c++){o=e[c];if(o&&typeof o.then==="function"){o.then(s(c),t)}else{f(c,o)}}})}r.all=n});e("rsvp/async",["exports"],function(e){"use strict";var r=typeof window!=="undefined"?window:{};var t=r.MutationObserver||r.WebKitMutationObserver;var n;if(typeof process!=="undefined"&&{}.toString.call(process)==="[object process]"){n=function(e,r){process.nextTick(function(){e.call(r)})}}else if(t){var i=[];var o=new t(function(){var e=i.slice();i=[];e.forEach(function(e){var r=e[0],t=e[1];r.call(t)})});var s=document.createElement("div");o.observe(s,{attributes:true});window.addEventListener("unload",function(){o.disconnect();o=null});n=function(e,r){i.push([e,r]);s.setAttribute("drainQueue","drainQueue")}}else{n=function(e,r){setTimeout(function(){e.call(r)},1)}}e.async=n});e("rsvp/config",["rsvp/async","exports"],function(e,r){"use strict";var t=e.async;var n={};n.async=t;r.config=n});e("rsvp/defer",["rsvp/promise","exports"],function(e,r){"use strict";var t=e.Promise;function n(){var e={};var r=new t(function(r,t){e.resolve=r;e.reject=t});e.promise=r;return e}r.defer=n});e("rsvp/events",["exports"],function(e){"use strict";var r=function(e,r){this.type=e;for(var t in r){if(!r.hasOwnProperty(t)){continue}this[t]=r[t]}};var t=function(e,r){for(var t=0,n=e.length;t<n;t++){if(e[t][0]===r){return t}}return-1};var n=function(e){var r=e._promiseCallbacks;if(!r){r=e._promiseCallbacks={}}return r};var i={mixin:function(e){e.on=this.on;e.off=this.off;e.trigger=this.trigger;return e},on:function(e,r,i){var o=n(this),s,f;e=e.split(/\s+/);i=i||this;while(f=e.shift()){s=o[f];if(!s){s=o[f]=[]}if(t(s,r)===-1){s.push([r,i])}}},off:function(e,r){var i=n(this),o,s,f;e=e.split(/\s+/);while(s=e.shift()){if(!r){i[s]=[];continue}o=i[s];f=t(o,r);if(f!==-1){o.splice(f,1)}}},trigger:function(e,t){var i=n(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 t!=="object"){t={detail:t}}u=new r(e,t);f.call(c,u)}}}};e.EventTarget=i});e("rsvp/hash",["rsvp/defer","exports"],function(e,r){"use strict";var t=e.defer;function n(e){var r=0;for(var t in e){r++}return r}function i(e){var r={},i=t(),o=n(e);if(o===0){i.resolve({})}var s=function(e){return function(r){f(e,r)}};var f=function(e,t){r[e]=t;if(--o===0){i.resolve(r)}};var c=function(e){i.reject(e)};for(var u in e){if(e[u]&&typeof e[u].then==="function"){e[u].then(s(u),c)}else{f(u,e[u])}}return i.promise}r.hash=i});e("rsvp/node",["rsvp/promise","rsvp/all","exports"],function(e,r,t){"use strict";var n=e.Promise;var i=r.all;function o(e,r){return function(t,n){if(t){r(t)}else if(arguments.length>2){e(Array.prototype.slice.call(arguments,1))}else{e(n)}}}function s(e){return function(){var r=Array.prototype.slice.call(arguments),t,s;var f=this;var c=new n(function(e,r){t=e;s=r});i(r).then(function(r){r.push(o(t,s));try{e.apply(f,r)}catch(n){s(n)}});return c}}t.denodeify=s});e("rsvp/promise",["rsvp/config","rsvp/events","exports"],function(e,r,t){"use strict";var n=e.config;var i=r.EventTarget;function o(e){return s(e)||typeof e==="object"&&e!==null}function s(e){return typeof e==="function"}var f=function(e){var r=this,t=false;if(typeof e!=="function"){throw new TypeError("You must pass a resolver function as the sole argument to the promise constructor")}if(!(r instanceof f)){return new f(e)}var n=function(e){if(t){return}t=true;a(r,e)};var i=function(e){if(t){return}t=true;p(r,e)};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);this.on("error",c);try{e(n,i)}catch(o){i(o)}};function c(e){if(n.onerror){n.onerror(e.detail)}}var u=function(e,r,t,n){var i=s(t),o,f,c,u;if(i){try{o=t(n.detail);c=true}catch(v){u=true;f=v}}else{o=n.detail;c=true}if(l(r,o)){return}else if(i&&c){a(r,o)}else if(u){p(r,f)}else if(e==="resolve"){a(r,o)}else if(e==="reject"){p(r,o)}};f.prototype={constructor:f,then:function(e,r){this.off("error",c);var t=new f(function(){});if(this.isFulfilled){n.async(function(){u("resolve",t,e,{detail:this.fulfillmentValue})},this)}if(this.isRejected){n.async(function(){u("reject",t,r,{detail:this.rejectedReason})},this)}this.on("promise:resolved",function(r){u("resolve",t,e,r)});this.on("promise:failed",function(e){u("reject",t,r,e)});return t}};i.mixin(f.prototype);function a(e,r){if(e===r){v(e,r)}else if(!l(e,r)){v(e,r)}}function l(e,r){var t=null;if(o(r)){try{t=r.then}catch(n){p(e,n);return true}if(s(t)){try{t.call(r,function(t){if(r!==t){a(e,t)}else{v(e,t)}},function(r){p(e,r)})}catch(n){p(e,n)}return true}}return false}function v(e,r){n.async(function(){e.trigger("promise:resolved",{detail:r});e.isFulfilled=true;e.fulfillmentValue=r})}function p(e,r){n.async(function(){e.trigger("promise:failed",{detail:r});e.isRejected=true;e.rejectedReason=r})}t.Promise=f});e("rsvp/reject",["rsvp/promise","exports"],function(e,r){"use strict";var t=e.Promise;function n(e){return typeof e==="function"||typeof e==="object"&&e!==null}function i(e){return new t(function(r,t){t(e)})}r.reject=i});e("rsvp/resolve",["rsvp/promise","exports"],function(e,r){"use strict";var t=e.Promise;function n(e){return typeof e==="function"||typeof e==="object"&&e!==null}function i(e){if(e instanceof t){return e}var r=new t(function(r,t){var i;try{if(n(e)){i=e.then;if(typeof i==="function"){i.call(e,r,t)}else{r(e)}}else{r(e)}}catch(o){t(o)}});return r}r.resolve=i});e("rsvp",["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","rsvp/resolve","rsvp/reject","exports"],function(e,r,t,n,i,o,s,f,c,u){"use strict";var a=e.EventTarget;var l=r.Promise;var v=t.denodeify;var p=n.all;var h=i.hash;var d=o.defer;var y=s.config;var m=f.resolve;var g=c.reject;function w(e,r){y[e]=r}u.Promise=l;u.EventTarget=a;u.all=p;u.hash=h;u.defer=d;u.denodeify=v;u.configure=w;u.resolve=m;u.reject=g});window.RSVP=r("rsvp")}();
...@@ -5,6 +5,9 @@ define( ...@@ -5,6 +5,9 @@ define(
var Promise = __dependency1__.Promise; var Promise = __dependency1__.Promise;
function all(promises) { function all(promises) {
if(toString.call(promises) !== "[object Array]") {
throw new TypeError('You must pass an array to all.');
}
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var results = [], remaining = promises.length, var results = [], remaining = promises.length,
promise; promise;
......
define(
["rsvp/promise","rsvp/all","exports"],
function(__dependency1__, __dependency2__, __exports__) {
"use strict";
var Promise = __dependency1__.Promise;
var all = __dependency2__.all;
function makeNodeCallbackFor(resolve, reject) {
return function (error, value) {
if (error) {
reject(error);
} else if (arguments.length > 2) {
resolve(Array.prototype.slice.call(arguments, 1));
} else {
resolve(value);
}
};
}
function denodeify(nodeFunc) {
return function() {
var nodeArgs = Array.prototype.slice.call(arguments), resolve, reject;
var thisArg = this;
var promise = new Promise(function(nodeResolve, nodeReject) {
resolve = nodeResolve;
reject = nodeReject;
});
all(nodeArgs).then(function(nodeArgs) {
nodeArgs.push(makeNodeCallbackFor(resolve, reject));
try {
nodeFunc.apply(thisArg, nodeArgs);
} catch(e) {
reject(e);
}
});
return promise;
};
}
__exports__.denodeify = denodeify;
});
...@@ -45,6 +45,8 @@ define( ...@@ -45,6 +45,8 @@ define(
this.trigger('error', { detail: event.detail }); this.trigger('error', { detail: event.detail });
}, this); }, this);
this.on('error', onerror);
try { try {
resolver(resolvePromise, rejectPromise); resolver(resolvePromise, rejectPromise);
} catch(e) { } catch(e) {
...@@ -52,6 +54,12 @@ define( ...@@ -52,6 +54,12 @@ define(
} }
}; };
function onerror(event) {
if (config.onerror) {
config.onerror(event.detail);
}
}
var invokeCallback = function(type, promise, callback, event) { var invokeCallback = function(type, promise, callback, event) {
var hasCallback = isFunction(callback), var hasCallback = isFunction(callback),
value, error, succeeded, failed; value, error, succeeded, failed;
...@@ -86,6 +94,8 @@ define( ...@@ -86,6 +94,8 @@ define(
constructor: Promise, constructor: Promise,
then: function(done, fail) { then: function(done, fail) {
this.off('error', onerror);
var thenPromise = new Promise(function() {}); var thenPromise = new Promise(function() {});
if (this.isFulfilled) { if (this.isFulfilled) {
......
...@@ -41,6 +41,8 @@ var Promise = function(resolver) { ...@@ -41,6 +41,8 @@ var Promise = function(resolver) {
this.trigger('error', { detail: event.detail }); this.trigger('error', { detail: event.detail });
}, this); }, this);
this.on('error', onerror);
try { try {
resolver(resolvePromise, rejectPromise); resolver(resolvePromise, rejectPromise);
} catch(e) { } catch(e) {
...@@ -48,6 +50,12 @@ var Promise = function(resolver) { ...@@ -48,6 +50,12 @@ var Promise = function(resolver) {
} }
}; };
function onerror(event) {
if (config.onerror) {
config.onerror(event.detail);
}
}
var invokeCallback = function(type, promise, callback, event) { var invokeCallback = function(type, promise, callback, event) {
var hasCallback = isFunction(callback), var hasCallback = isFunction(callback),
value, error, succeeded, failed; value, error, succeeded, failed;
...@@ -82,6 +90,8 @@ Promise.prototype = { ...@@ -82,6 +90,8 @@ Promise.prototype = {
constructor: Promise, constructor: Promise,
then: function(done, fail) { then: function(done, fail) {
this.off('error', onerror);
var thenPromise = new Promise(function() {}); var thenPromise = new Promise(function() {});
if (this.isFulfilled) { if (this.isFulfilled) {
......
...@@ -622,6 +622,44 @@ describe("RSVP extensions", function() { ...@@ -622,6 +622,44 @@ describe("RSVP extensions", function() {
}); });
}); });
describe("RSVP.onerror", function(){
var onerror;
after(function() {
RSVP.configure('onerror', null);
});
it("When provided, any unhandled exceptions are sent to it", function(done) {
var thrownError = new Error();
RSVP.configure('onerror', function(error) {
assert.equal(error, thrownError, "The thrown error is passed in");
done();
});
new RSVP.Promise(function(resolve, reject) {
reject(thrownError);
}).then(function() {
// doesn't get here
});
});
it("When provided, handled exceptions are not sent to it", function(done) {
var thrownError = new Error();
RSVP.configure('onerror', function(error) {
assert(false, "Should not get here");
});
new RSVP.Promise(function(resolve, reject) {
reject(thrownError);
}).then(null, function(error) {
assert.equal(error, thrownError, "The handler should handle the error");
done();
});
});
});
describe("RSVP.resolve", function(){ describe("RSVP.resolve", function(){
specify("it should exist", function(){ specify("it should exist", function(){
assert(RSVP.resolve); assert(RSVP.resolve);
......
This source diff could not be displayed because it is too large. You can view the blob instead.
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