Commit 55398c39 authored by Stefan Penner's avatar Stefan Penner

short-circuit RSVP.resolve is resolving RSVP.Promise

parent 889827fd
...@@ -566,12 +566,15 @@ define("rsvp/resolve", ...@@ -566,12 +566,15 @@ define("rsvp/resolve",
"use strict"; "use strict";
var Promise = __dependency1__.Promise; var Promise = __dependency1__.Promise;
function objectOrFunction(x) { function objectOrFunction(x) {
return typeof x === "function" || (typeof x === "object" && x !== null); return typeof x === "function" || (typeof x === "object" && x !== null);
} }
function resolve(thenable){ function resolve(thenable){
if (thenable instanceof Promise) {
return thenable;
}
var promise = new Promise(function(resolve, reject){ var promise = new Promise(function(resolve, reject){
var then; var then;
......
(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")})(); (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){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")})();
...@@ -4,12 +4,15 @@ define( ...@@ -4,12 +4,15 @@ define(
"use strict"; "use strict";
var Promise = __dependency1__.Promise; var Promise = __dependency1__.Promise;
function objectOrFunction(x) { function objectOrFunction(x) {
return typeof x === "function" || (typeof x === "object" && x !== null); return typeof x === "function" || (typeof x === "object" && x !== null);
} }
function resolve(thenable){ function resolve(thenable){
if (thenable instanceof Promise) {
return thenable;
}
var promise = new Promise(function(resolve, reject){ var promise = new Promise(function(resolve, reject){
var then; var then;
......
import { Promise } from "rsvp/promise"; import { Promise } from "rsvp/promise";
function objectOrFunction(x) { function objectOrFunction(x) {
return typeof x === "function" || (typeof x === "object" && x !== null); return typeof x === "function" || (typeof x === "object" && x !== null);
} }
function resolve(thenable){ function resolve(thenable){
if (thenable instanceof Promise) {
return thenable;
}
var promise = new Promise(function(resolve, reject){ var promise = new Promise(function(resolve, reject){
var then; var then;
......
...@@ -613,6 +613,12 @@ describe("RSVP extensions", function() { ...@@ -613,6 +613,12 @@ describe("RSVP extensions", function() {
assert(RSVP.resolve); assert(RSVP.resolve);
}); });
specify("it short circuits if RSVP.promise", function(){
var deferred = new RSVP.defer();
assert.equal(RSVP.resolve(deferred.promise), deferred.promise);
});
describe("1. If x is a promise, adopt its state ", function(){ describe("1. If x is a promise, adopt its state ", function(){
specify("1.1 If x is pending, promise must remain pending until x is fulfilled or rejected.", function(done){ specify("1.1 If x is pending, promise must remain pending until x is fulfilled or rejected.", function(done){
var expectedValue, resolver, thenable, wrapped; var expectedValue, resolver, thenable, wrapped;
......
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