Commit 8c80ba5e authored by Stefan Penner's avatar Stefan Penner

add RSVP.reject

parent 84469f63
define( define(
["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","rsvp/resolve","exports"], ["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","rsvp/resolve","rsvp/reject","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __dependency8__, __exports__) { function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __dependency8__, __dependency9__, __exports__) {
"use strict"; "use strict";
var EventTarget = __dependency1__.EventTarget; var EventTarget = __dependency1__.EventTarget;
var Promise = __dependency2__.Promise; var Promise = __dependency2__.Promise;
...@@ -10,6 +10,7 @@ define( ...@@ -10,6 +10,7 @@ define(
var defer = __dependency6__.defer; var defer = __dependency6__.defer;
var config = __dependency7__.config; var config = __dependency7__.config;
var resolve = __dependency8__.resolve; var resolve = __dependency8__.resolve;
var reject = __dependency9__.reject;
function configure(name, value) { function configure(name, value) {
config[name] = value; config[name] = value;
...@@ -24,4 +25,5 @@ define( ...@@ -24,4 +25,5 @@ define(
__exports__.denodeify = denodeify; __exports__.denodeify = denodeify;
__exports__.configure = configure; __exports__.configure = configure;
__exports__.resolve = resolve; __exports__.resolve = resolve;
__exports__.reject = reject;
}); });
...@@ -31,32 +31,47 @@ var define, requireModule; ...@@ -31,32 +31,47 @@ var define, requireModule;
}; };
})(); })();
define("rsvp", define("rsvp/all",
["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","rsvp/resolve","exports"], ["rsvp/defer","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __dependency8__, __exports__) { function(__dependency1__, __exports__) {
"use strict"; "use strict";
var EventTarget = __dependency1__.EventTarget; var defer = __dependency1__.defer;
var Promise = __dependency2__.Promise;
var denodeify = __dependency3__.denodeify;
var all = __dependency4__.all;
var hash = __dependency5__.hash;
var defer = __dependency6__.defer;
var config = __dependency7__.config;
var resolve = __dependency8__.resolve;
function configure(name, value) { function all(promises) {
config[name] = value; var results = [], deferred = defer(), remaining = promises.length;
}
if (remaining === 0) {
deferred.resolve([]);
}
var resolver = function(index) {
return function(value) {
resolveAll(index, value);
};
};
var resolveAll = function(index, value) {
results[index] = value;
if (--remaining === 0) {
deferred.resolve(results);
}
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll);
} else {
resolveAll(i, promises[i]);
}
}
return deferred.promise;
}
__exports__.Promise = Promise;
__exports__.EventTarget = EventTarget;
__exports__.all = all; __exports__.all = all;
__exports__.hash = hash;
__exports__.defer = defer;
__exports__.denodeify = denodeify;
__exports__.configure = configure;
__exports__.resolve = resolve;
}); });
define("rsvp/async", define("rsvp/async",
...@@ -113,6 +128,141 @@ define("rsvp/async", ...@@ -113,6 +128,141 @@ define("rsvp/async",
__exports__.async = async; __exports__.async = async;
}); });
define("rsvp/config",
["rsvp/async","exports"],
function(__dependency1__, __exports__) {
"use strict";
var async = __dependency1__.async;
var config = {};
config.async = async;
__exports__.config = config;
});
define("rsvp/defer",
["rsvp/promise","exports"],
function(__dependency1__, __exports__) {
"use strict";
var Promise = __dependency1__.Promise;
function defer() {
var deferred = {};
var promise = new Promise(function(resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
deferred.promise = promise;
return deferred;
}
__exports__.defer = defer;
});
define("rsvp/events",
["exports"],
function(__exports__) {
"use strict";
var Event = function(type, options) {
this.type = type;
for (var option in options) {
if (!options.hasOwnProperty(option)) { continue; }
this[option] = options[option];
}
};
var indexOf = function(callbacks, callback) {
for (var i=0, l=callbacks.length; i<l; i++) {
if (callbacks[i][0] === callback) { return i; }
}
return -1;
};
var callbacksFor = function(object) {
var callbacks = object._promiseCallbacks;
if (!callbacks) {
callbacks = object._promiseCallbacks = {};
}
return callbacks;
};
var EventTarget = {
mixin: function(object) {
object.on = this.on;
object.off = this.off;
object.trigger = this.trigger;
return object;
},
on: function(eventNames, callback, binding) {
var allCallbacks = callbacksFor(this), callbacks, eventName;
eventNames = eventNames.split(/\s+/);
binding = binding || this;
while (eventName = eventNames.shift()) {
callbacks = allCallbacks[eventName];
if (!callbacks) {
callbacks = allCallbacks[eventName] = [];
}
if (indexOf(callbacks, callback) === -1) {
callbacks.push([callback, binding]);
}
}
},
off: function(eventNames, callback) {
var allCallbacks = callbacksFor(this), callbacks, eventName, index;
eventNames = eventNames.split(/\s+/);
while (eventName = eventNames.shift()) {
if (!callback) {
allCallbacks[eventName] = [];
continue;
}
callbacks = allCallbacks[eventName];
index = indexOf(callbacks, callback);
if (index !== -1) { callbacks.splice(index, 1); }
}
},
trigger: function(eventName, options) {
var allCallbacks = callbacksFor(this),
callbacks, callbackTuple, callback, binding, event;
if (callbacks = allCallbacks[eventName]) {
// Don't cache the callbacks.length since it may grow
for (var i=0; i<callbacks.length; i++) {
callbackTuple = callbacks[i];
callback = callbackTuple[0];
binding = callbackTuple[1];
if (typeof options !== 'object') {
options = { detail: options };
}
event = new Event(eventName, options);
callback.call(binding, event);
}
}
}
};
__exports__.EventTarget = EventTarget;
});
define("rsvp/hash", define("rsvp/hash",
["rsvp/defer","exports"], ["rsvp/defer","exports"],
function(__dependency1__, __exports__) { function(__dependency1__, __exports__) {
...@@ -212,123 +362,6 @@ define("rsvp/node", ...@@ -212,123 +362,6 @@ define("rsvp/node",
__exports__.denodeify = denodeify; __exports__.denodeify = denodeify;
}); });
define("rsvp/defer",
["rsvp/promise","exports"],
function(__dependency1__, __exports__) {
"use strict";
var Promise = __dependency1__.Promise;
function defer() {
var deferred = {};
var promise = new Promise(function(resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});
deferred.promise = promise;
return deferred;
}
__exports__.defer = defer;
});
define("rsvp/config",
["rsvp/async","exports"],
function(__dependency1__, __exports__) {
"use strict";
var async = __dependency1__.async;
var config = {};
config.async = async;
__exports__.config = config;
});
define("rsvp/all",
["rsvp/defer","exports"],
function(__dependency1__, __exports__) {
"use strict";
var defer = __dependency1__.defer;
function all(promises) {
var results = [], deferred = defer(), remaining = promises.length;
if (remaining === 0) {
deferred.resolve([]);
}
var resolver = function(index) {
return function(value) {
resolveAll(index, value);
};
};
var resolveAll = function(index, value) {
results[index] = value;
if (--remaining === 0) {
deferred.resolve(results);
}
};
var rejectAll = function(error) {
deferred.reject(error);
};
for (var i = 0; i < promises.length; i++) {
if (promises[i] && typeof promises[i].then === 'function') {
promises[i].then(resolver(i), rejectAll);
} else {
resolveAll(i, promises[i]);
}
}
return deferred.promise;
}
__exports__.all = all;
});
define("rsvp/resolve",
["rsvp/promise","exports"],
function(__dependency1__, __exports__) {
"use strict";
var Promise = __dependency1__.Promise;
function objectOrFunction(x) {
return typeof x === "function" || (typeof x === "object" && x !== null);
}
function resolve(thenable){
var promise = new Promise(function(resolve, reject){
var then;
try {
if ( objectOrFunction(thenable) ) {
then = thenable.then;
if (typeof then === "function") {
then.call(thenable, resolve, reject);
} else {
resolve(thenable);
}
} else {
resolve(thenable);
}
} catch(error) {
reject(error);
}
});
return promise;
}
__exports__.resolve = resolve;
});
define("rsvp/promise", define("rsvp/promise",
["rsvp/config","rsvp/events","exports"], ["rsvp/config","rsvp/events","exports"],
function(__dependency1__, __dependency2__, __exports__) { function(__dependency1__, __dependency2__, __exports__) {
...@@ -376,7 +409,11 @@ define("rsvp/promise", ...@@ -376,7 +409,11 @@ define("rsvp/promise",
this.trigger('error', { detail: event.detail }); this.trigger('error', { detail: event.detail });
}, this); }, this);
resolver(resolvePromise, rejectPromise); try {
resolver(resolvePromise, rejectPromise);
} catch(e) {
rejectPromise(e);
}
}; };
var invokeCallback = function(type, promise, callback, event) { var invokeCallback = function(type, promise, callback, event) {
...@@ -396,12 +433,8 @@ define("rsvp/promise", ...@@ -396,12 +433,8 @@ define("rsvp/promise",
succeeded = true; succeeded = true;
} }
if (objectOrFunction(value) && isFunction(value.then)) { if (handleThenable(promise, value)) {
value.then(function(value) { return;
resolve(promise, value);
}, function(error) {
reject(promise, error);
});
} else if (hasCallback && succeeded) { } else if (hasCallback && succeeded) {
resolve(promise, value); resolve(promise, value);
} else if (failed) { } else if (failed) {
...@@ -446,24 +479,45 @@ define("rsvp/promise", ...@@ -446,24 +479,45 @@ define("rsvp/promise",
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
function resolve(promise, value) { function resolve(promise, value) {
if (promise === value) { if (promise === value) {
fulfill(promise, value); fulfill(promise, value);
} else if (objectOrFunction(value) && isFunction(value.then)) { } else if (!handleThenable(promise, value)) {
value.then(function(val) {
if (value !== val) {
resolve(promise, val);
} else {
fulfill(promise, val);
}
}, function(val) {
reject(promise, val);
});
} else {
fulfill(promise, value); fulfill(promise, value);
} }
} }
function handleThenable(promise, value) {
var then = null;
if (objectOrFunction(value)) {
try {
then = value.then;
} catch(e) {
reject(promise, e);
return true;
}
if (isFunction(then)) {
try {
then.call(value, function(val) {
if (value !== val) {
resolve(promise, val);
} else {
fulfill(promise, val);
}
}, function(val) {
reject(promise, val);
});
} catch (e) {
reject(promise, e);
}
return true;
}
}
return false;
}
function fulfill(promise, value) { function fulfill(promise, value) {
config.async(function() { config.async(function() {
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
...@@ -484,106 +538,97 @@ define("rsvp/promise", ...@@ -484,106 +538,97 @@ define("rsvp/promise",
__exports__.Promise = Promise; __exports__.Promise = Promise;
}); });
define("rsvp/events", define("rsvp/reject",
["exports"], ["rsvp/promise","exports"],
function(__exports__) { function(__dependency1__, __exports__) {
"use strict"; "use strict";
var Event = function(type, options) { var Promise = __dependency1__.Promise;
this.type = type;
for (var option in options) {
if (!options.hasOwnProperty(option)) { continue; }
this[option] = options[option];
}
};
var indexOf = function(callbacks, callback) { function objectOrFunction(x) {
for (var i=0, l=callbacks.length; i<l; i++) { return typeof x === "function" || (typeof x === "object" && x !== null);
if (callbacks[i][0] === callback) { return i; } }
}
return -1;
};
var callbacksFor = function(object) { function reject(reason) {
var callbacks = object._promiseCallbacks; return new Promise(function (resolve, reject) {
reject(reason);
});
}
if (!callbacks) {
callbacks = object._promiseCallbacks = {};
}
return callbacks; __exports__.reject = reject;
}; });
var EventTarget = { define("rsvp/resolve",
mixin: function(object) { ["rsvp/promise","exports"],
object.on = this.on; function(__dependency1__, __exports__) {
object.off = this.off; "use strict";
object.trigger = this.trigger; var Promise = __dependency1__.Promise;
return object;
},
on: function(eventNames, callback, binding) {
var allCallbacks = callbacksFor(this), callbacks, eventName;
eventNames = eventNames.split(/\s+/);
binding = binding || this;
while (eventName = eventNames.shift()) { function objectOrFunction(x) {
callbacks = allCallbacks[eventName]; return typeof x === "function" || (typeof x === "object" && x !== null);
}
if (!callbacks) { function resolve(thenable){
callbacks = allCallbacks[eventName] = []; var promise = new Promise(function(resolve, reject){
} var then;
if (indexOf(callbacks, callback) === -1) { try {
callbacks.push([callback, binding]); if ( objectOrFunction(thenable) ) {
} then = thenable.then;
}
},
off: function(eventNames, callback) { if (typeof then === "function") {
var allCallbacks = callbacksFor(this), callbacks, eventName, index; then.call(thenable, resolve, reject);
eventNames = eventNames.split(/\s+/); } else {
resolve(thenable);
}
while (eventName = eventNames.shift()) { } else {
if (!callback) { resolve(thenable);
allCallbacks[eventName] = [];
continue;
} }
callbacks = allCallbacks[eventName]; } catch(error) {
reject(error);
index = indexOf(callbacks, callback);
if (index !== -1) { callbacks.splice(index, 1); }
} }
}, });
trigger: function(eventName, options) { return promise;
var allCallbacks = callbacksFor(this), }
callbacks, callbackTuple, callback, binding, event;
if (callbacks = allCallbacks[eventName]) {
// Don't cache the callbacks.length since it may grow
for (var i=0; i<callbacks.length; i++) {
callbackTuple = callbacks[i];
callback = callbackTuple[0];
binding = callbackTuple[1];
if (typeof options !== 'object') { __exports__.resolve = resolve;
options = { detail: options }; });
}
event = new Event(eventName, options); define("rsvp",
callback.call(binding, event); ["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","rsvp/resolve","rsvp/reject","exports"],
} function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __dependency8__, __dependency9__, __exports__) {
} "use strict";
} var EventTarget = __dependency1__.EventTarget;
}; var Promise = __dependency2__.Promise;
var denodeify = __dependency3__.denodeify;
var all = __dependency4__.all;
var hash = __dependency5__.hash;
var defer = __dependency6__.defer;
var config = __dependency7__.config;
var resolve = __dependency8__.resolve;
var reject = __dependency9__.reject;
function configure(name, value) {
config[name] = value;
}
__exports__.Promise = Promise;
__exports__.EventTarget = EventTarget; __exports__.EventTarget = EventTarget;
__exports__.all = all;
__exports__.hash = hash;
__exports__.defer = defer;
__exports__.denodeify = denodeify;
__exports__.configure = configure;
__exports__.resolve = resolve;
__exports__.reject = reject;
}); });
window.RSVP = requireModule('rsvp'); window.RSVP = requireModule('rsvp');
......
(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 l=s.apply(this,f);return n[e]=c||l}})();e("rsvp",["rsvp/events","rsvp/promise","rsvp/node","rsvp/all","rsvp/hash","rsvp/defer","rsvp/config","rsvp/resolve","exports"],function(e,t,r,n,i,o,s,f,c){"use strict";var u=e.EventTarget;var a=t.Promise;var l=r.denodeify;var v=n.all;var p=i.hash;var h=o.defer;var d=s.config;var y=f.resolve;function g(e,t){d[e]=t}c.Promise=a;c.EventTarget=u;c.all=v;c.hash=p;c.defer=h;c.denodeify=l;c.configure=g;c.resolve=y});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/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/node",["rsvp/promise","rsvp/all","exports"],function(e,t,r){"use strict";var n=e.Promise;var i=t.all;function o(e,t){return function(r,n){if(r){t(r)}else if(arguments.length>2){e(Array.prototype.slice.call(arguments,1))}else{e(n)}}}function s(e){return function(){var t=Array.prototype.slice.call(arguments),r,s;var f=new n(function(e,t){r=e;s=t});i(t).then(function(t){t.push(o(r,s));try{e.apply(this,t)}catch(n){s(n)}});return f}}r.denodeify=s});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/config",["rsvp/async","exports"],function(e,t){"use strict";var r=e.async;var n={};n.async=r;t.config=n});e("rsvp/all",["rsvp/defer","exports"],function(e,t){"use strict";var r=e.defer;function n(e){var t=[],n=r(),i=e.length;if(i===0){n.resolve([])}var o=function(e){return function(t){s(e,t)}};var s=function(e,r){t[e]=r;if(--i===0){n.resolve(t)}};var f=function(e){n.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 n.promise}t.all=n});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){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/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);e(n,i)};var c=function(e,t,r,n){var i=s(r),f,c,a,v;if(i){try{f=r(n.detail);a=true}catch(p){v=true;c=p}}else{f=n.detail;a=true}if(o(f)&&s(f.then)){f.then(function(e){u(t,e)},function(e){l(t,e)})}else if(i&&a){u(t,f)}else if(v){l(t,c)}else if(e==="resolve"){u(t,f)}else if(e==="reject"){l(t,f)}};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){a(e,t)}else if(o(t)&&s(t.then)){t.then(function(r){if(t!==r){u(e,r)}else{a(e,r)}},function(t){l(e,t)})}else{a(e,t)}}function a(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/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});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/defer","exports"],function(e,r){"use strict";var t=e.defer;function n(e){var r=[],n=t(),i=e.length;if(i===0){n.resolve([])}var o=function(e){return function(r){s(e,r)}};var s=function(e,t){r[e]=t;if(--i===0){n.resolve(r)}};var f=function(e){n.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 n.promise}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=new n(function(e,r){t=e;s=r});i(r).then(function(r){r.push(o(t,s));try{e.apply(this,r)}catch(n){s(n)}});return f}}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;u(r,e)};var i=function(e){if(t){return}t=true;v(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);try{e(n,i)}catch(o){i(o)}};var c=function(e,r,t,n){var i=s(t),o,f,c,l;if(i){try{o=t(n.detail);c=true}catch(p){l=true;f=p}}else{o=n.detail;c=true}if(a(r,o)){return}else if(i&&c){u(r,o)}else if(l){v(r,f)}else if(e==="resolve"){u(r,o)}else if(e==="reject"){v(r,o)}};f.prototype={constructor:f,then:function(e,r){var t=new f(function(){});if(this.isFulfilled){n.async(function(){c("resolve",t,e,{detail:this.fulfillmentValue})},this)}if(this.isRejected){n.async(function(){c("reject",t,r,{detail:this.rejectedReason})},this)}this.on("promise:resolved",function(r){c("resolve",t,e,r)});this.on("promise:failed",function(e){c("reject",t,r,e)});return t}};i.mixin(f.prototype);function u(e,r){if(e===r){l(e,r)}else if(!a(e,r)){l(e,r)}}function a(e,r){var t=null;if(o(r)){try{t=r.then}catch(n){v(e,n);return true}if(s(t)){try{t.call(r,function(t){if(r!==t){u(e,t)}else{l(e,t)}},function(r){v(e,r)})}catch(n){v(e,n)}return true}}return false}function l(e,r){n.async(function(){e.trigger("promise:resolved",{detail:r});e.isFulfilled=true;e.fulfillmentValue=r})}function v(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){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")})();
...@@ -45,7 +45,11 @@ define( ...@@ -45,7 +45,11 @@ define(
this.trigger('error', { detail: event.detail }); this.trigger('error', { detail: event.detail });
}, this); }, this);
resolver(resolvePromise, rejectPromise); try {
resolver(resolvePromise, rejectPromise);
} catch(e) {
rejectPromise(e);
}
}; };
var invokeCallback = function(type, promise, callback, event) { var invokeCallback = function(type, promise, callback, event) {
...@@ -65,12 +69,8 @@ define( ...@@ -65,12 +69,8 @@ define(
succeeded = true; succeeded = true;
} }
if (objectOrFunction(value) && isFunction(value.then)) { if (handleThenable(promise, value)) {
value.then(function(value) { return;
resolve(promise, value);
}, function(error) {
reject(promise, error);
});
} else if (hasCallback && succeeded) { } else if (hasCallback && succeeded) {
resolve(promise, value); resolve(promise, value);
} else if (failed) { } else if (failed) {
...@@ -115,24 +115,45 @@ define( ...@@ -115,24 +115,45 @@ define(
EventTarget.mixin(Promise.prototype); EventTarget.mixin(Promise.prototype);
function resolve(promise, value) { function resolve(promise, value) {
if (promise === value) { if (promise === value) {
fulfill(promise, value); fulfill(promise, value);
} else if (objectOrFunction(value) && isFunction(value.then)) { } else if (!handleThenable(promise, value)) {
value.then(function(val) {
if (value !== val) {
resolve(promise, val);
} else {
fulfill(promise, val);
}
}, function(val) {
reject(promise, val);
});
} else {
fulfill(promise, value); fulfill(promise, value);
} }
} }
function handleThenable(promise, value) {
var then = null;
if (objectOrFunction(value)) {
try {
then = value.then;
} catch(e) {
reject(promise, e);
return true;
}
if (isFunction(then)) {
try {
then.call(value, function(val) {
if (value !== val) {
resolve(promise, val);
} else {
fulfill(promise, val);
}
}, function(val) {
reject(promise, val);
});
} catch (e) {
reject(promise, e);
}
return true;
}
}
return false;
}
function fulfill(promise, value) { function fulfill(promise, value) {
config.async(function() { config.async(function() {
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
......
define(
["rsvp/promise","exports"],
function(__dependency1__, __exports__) {
"use strict";
var Promise = __dependency1__.Promise;
function objectOrFunction(x) {
return typeof x === "function" || (typeof x === "object" && x !== null);
}
function reject(reason) {
return new Promise(function (resolve, reject) {
reject(reason);
});
}
__exports__.reject = reject;
});
...@@ -6,9 +6,10 @@ import { hash } from "rsvp/hash"; ...@@ -6,9 +6,10 @@ 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";
import { resolve } from "rsvp/resolve"; import { resolve } from "rsvp/resolve";
import { reject } from "rsvp/reject";
function configure(name, value) { function configure(name, value) {
config[name] = value; config[name] = value;
} }
export { Promise, EventTarget, all, hash, defer, denodeify, configure, resolve }; export { Promise, EventTarget, all, hash, defer, denodeify, configure, resolve, reject };
import { Promise } from "rsvp/promise";
function objectOrFunction(x) {
return typeof x === "function" || (typeof x === "object" && x !== null);
}
function reject(reason) {
return new Promise(function (resolve, reject) {
reject(reason);
});
}
export { reject };
...@@ -574,6 +574,23 @@ describe("RSVP extensions", function() { ...@@ -574,6 +574,23 @@ describe("RSVP extensions", function() {
}); });
}); });
describe("RSVP.reject", function(){
specify("it should exist", function(){
assert(RSVP.reject);
});
describe('it rejects', function(){
var reason = 'the reason',
promise = RSVP.reject(reason);
promise.then(function(){
assert(false, 'should not fulfill');
}, function(actualReason){
assert.equal(reason, actualReason);
});
});
});
describe("RSVP.resolve", function(){ describe("RSVP.resolve", function(){
specify("it should exist", function(){ specify("it should exist", function(){
assert(RSVP.resolve); assert(RSVP.resolve);
......
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