diff options
author | Richard Beales <[email protected]> | 2013-03-18 07:32:01 +0000 |
---|---|---|
committer | Richard Beales <[email protected]> | 2013-03-18 07:32:01 +0000 |
commit | 7c97d17aaf373339a8bcd917ad59ca6018148f0d (patch) | |
tree | 5a3c04f0f9529be392c1263d3feb75806eb43797 /lib/dojo/promise | |
parent | 70db7424e7068701e60cc5bcdfe8f858be508179 (diff) | |
parent | c670a80ddd9b03bd4ea6d940a9ed682fd26248d7 (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'lib/dojo/promise')
-rw-r--r-- | lib/dojo/promise/Promise.js | 8 | ||||
-rw-r--r-- | lib/dojo/promise/Promise.js.uncompressed.js | 133 | ||||
-rw-r--r-- | lib/dojo/promise/all.js | 8 | ||||
-rw-r--r-- | lib/dojo/promise/all.js.uncompressed.js | 76 | ||||
-rw-r--r-- | lib/dojo/promise/first.js | 8 | ||||
-rw-r--r-- | lib/dojo/promise/first.js.uncompressed.js | 49 | ||||
-rw-r--r-- | lib/dojo/promise/instrumentation.js | 8 | ||||
-rw-r--r-- | lib/dojo/promise/instrumentation.js.uncompressed.js | 105 | ||||
-rw-r--r-- | lib/dojo/promise/tracer.js | 8 | ||||
-rw-r--r-- | lib/dojo/promise/tracer.js.uncompressed.js | 85 |
10 files changed, 488 insertions, 0 deletions
diff --git a/lib/dojo/promise/Promise.js b/lib/dojo/promise/Promise.js new file mode 100644 index 000000000..b29fef360 --- /dev/null +++ b/lib/dojo/promise/Promise.js @@ -0,0 +1,8 @@ +/* + Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved. + Available via Academic Free License >= 2.1 OR the modified BSD license. + see: http://dojotoolkit.org/license for details +*/ + +//>>built +define("dojo/promise/Promise",["../_base/lang"],function(_1){"use strict";function _2(){throw new TypeError("abstract");};return _1.extend(function Promise(){},{then:function(_3,_4,_5){_2();},cancel:function(_6,_7){_2();},isResolved:function(){_2();},isRejected:function(){_2();},isFulfilled:function(){_2();},isCanceled:function(){_2();},always:function(_8){return this.then(_8,_8);},otherwise:function(_9){return this.then(null,_9);},trace:function(){return this;},traceRejected:function(){return this;},toString:function(){return "[object Promise]";}});});
\ No newline at end of file diff --git a/lib/dojo/promise/Promise.js.uncompressed.js b/lib/dojo/promise/Promise.js.uncompressed.js new file mode 100644 index 000000000..eba0080fd --- /dev/null +++ b/lib/dojo/promise/Promise.js.uncompressed.js @@ -0,0 +1,133 @@ +define("dojo/promise/Promise", [ + "../_base/lang" +], function(lang){ + "use strict"; + + // module: + // dojo/promise/Promise + + function throwAbstract(){ + throw new TypeError("abstract"); + } + + return lang.extend(function Promise(){ + // summary: + // The public interface to a deferred. + // description: + // The public interface to a deferred. All promises in Dojo are + // instances of this class. + }, { + then: function(callback, errback, progback){ + // summary: + // Add new callbacks to the promise. + // description: + // Add new callbacks to the deferred. Callbacks can be added + // before or after the deferred is fulfilled. + // callback: Function? + // Callback to be invoked when the promise is resolved. + // Receives the resolution value. + // errback: Function? + // Callback to be invoked when the promise is rejected. + // Receives the rejection error. + // progback: Function? + // Callback to be invoked when the promise emits a progress + // update. Receives the progress update. + // returns: dojo/promise/Promise + // Returns a new promise for the result of the callback(s). + // This can be used for chaining many asynchronous operations. + + throwAbstract(); + }, + + cancel: function(reason, strict){ + // summary: + // Inform the deferred it may cancel its asynchronous operation. + // description: + // Inform the deferred it may cancel its asynchronous operation. + // The deferred's (optional) canceler is invoked and the + // deferred will be left in a rejected state. Can affect other + // promises that originate with the same deferred. + // reason: any + // A message that may be sent to the deferred's canceler, + // explaining why it's being canceled. + // strict: Boolean? + // If strict, will throw an error if the deferred has already + // been fulfilled and consequently cannot be canceled. + // returns: any + // Returns the rejection reason if the deferred was canceled + // normally. + + throwAbstract(); + }, + + isResolved: function(){ + // summary: + // Checks whether the promise has been resolved. + // returns: Boolean + + throwAbstract(); + }, + + isRejected: function(){ + // summary: + // Checks whether the promise has been rejected. + // returns: Boolean + + throwAbstract(); + }, + + isFulfilled: function(){ + // summary: + // Checks whether the promise has been resolved or rejected. + // returns: Boolean + + throwAbstract(); + }, + + isCanceled: function(){ + // summary: + // Checks whether the promise has been canceled. + // returns: Boolean + + throwAbstract(); + }, + + always: function(callbackOrErrback){ + // summary: + // Add a callback to be invoked when the promise is resolved + // or rejected. + // callbackOrErrback: Function? + // A function that is used both as a callback and errback. + // returns: dojo/promise/Promise + // Returns a new promise for the result of the callback/errback. + + return this.then(callbackOrErrback, callbackOrErrback); + }, + + otherwise: function(errback){ + // summary: + // Add new errbacks to the promise. + // errback: Function? + // Callback to be invoked when the promise is rejected. + // returns: dojo/promise/Promise + // Returns a new promise for the result of the errback. + + return this.then(null, errback); + }, + + trace: function(){ + return this; + }, + + traceRejected: function(){ + return this; + }, + + toString: function(){ + // returns: string + // Returns `[object Promise]`. + + return "[object Promise]"; + } + }); +}); diff --git a/lib/dojo/promise/all.js b/lib/dojo/promise/all.js new file mode 100644 index 000000000..928c63bec --- /dev/null +++ b/lib/dojo/promise/all.js @@ -0,0 +1,8 @@ +/* + Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved. + Available via Academic Free License >= 2.1 OR the modified BSD license. + see: http://dojotoolkit.org/license for details +*/ + +//>>built +define("dojo/promise/all",["../_base/array","../Deferred","../when"],function(_1,_2,_3){"use strict";var _4=_1.some;return function all(_5){var _6,_1;if(_5 instanceof Array){_1=_5;}else{if(_5&&typeof _5==="object"){_6=_5;}}var _7;var _8=[];if(_6){_1=[];for(var _9 in _6){if(Object.hasOwnProperty.call(_6,_9)){_8.push(_9);_1.push(_6[_9]);}}_7={};}else{if(_1){_7=[];}}if(!_1||!_1.length){return new _2().resolve(_7);}var _a=new _2();_a.promise.always(function(){_7=_8=null;});var _b=_1.length;_4(_1,function(_c,_d){if(!_6){_8.push(_d);}_3(_c,function(_e){if(!_a.isFulfilled()){_7[_8[_d]]=_e;if(--_b===0){_a.resolve(_7);}}},_a.reject);return _a.isFulfilled();});return _a.promise;};});
\ No newline at end of file diff --git a/lib/dojo/promise/all.js.uncompressed.js b/lib/dojo/promise/all.js.uncompressed.js new file mode 100644 index 000000000..278f604a6 --- /dev/null +++ b/lib/dojo/promise/all.js.uncompressed.js @@ -0,0 +1,76 @@ +define("dojo/promise/all", [ + "../_base/array", + "../Deferred", + "../when" +], function(array, Deferred, when){ + "use strict"; + + // module: + // dojo/promise/all + + var some = array.some; + + return function all(objectOrArray){ + // summary: + // Takes multiple promises and returns a new promise that is fulfilled + // when all promises have been fulfilled. + // description: + // Takes multiple promises and returns a new promise that is fulfilled + // when all promises have been fulfilled. If one of the promises is rejected, + // the returned promise is also rejected. Canceling the returned promise will + // *not* cancel any passed promises. + // objectOrArray: Object|Array? + // The promise will be fulfilled with a list of results if invoked with an + // array, or an object of results when passed an object (using the same + // keys). If passed neither an object or array it is resolved with an + // undefined value. + // returns: dojo/promise/Promise + + var object, array; + if(objectOrArray instanceof Array){ + array = objectOrArray; + }else if(objectOrArray && typeof objectOrArray === "object"){ + object = objectOrArray; + } + + var results; + var keyLookup = []; + if(object){ + array = []; + for(var key in object){ + if(Object.hasOwnProperty.call(object, key)){ + keyLookup.push(key); + array.push(object[key]); + } + } + results = {}; + }else if(array){ + results = []; + } + + if(!array || !array.length){ + return new Deferred().resolve(results); + } + + var deferred = new Deferred(); + deferred.promise.always(function(){ + results = keyLookup = null; + }); + var waiting = array.length; + some(array, function(valueOrPromise, index){ + if(!object){ + keyLookup.push(index); + } + when(valueOrPromise, function(value){ + if(!deferred.isFulfilled()){ + results[keyLookup[index]] = value; + if(--waiting === 0){ + deferred.resolve(results); + } + } + }, deferred.reject); + return deferred.isFulfilled(); + }); + return deferred.promise; // dojo/promise/Promise + }; +}); diff --git a/lib/dojo/promise/first.js b/lib/dojo/promise/first.js new file mode 100644 index 000000000..9a06ac2fc --- /dev/null +++ b/lib/dojo/promise/first.js @@ -0,0 +1,8 @@ +/* + Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved. + Available via Academic Free License >= 2.1 OR the modified BSD license. + see: http://dojotoolkit.org/license for details +*/ + +//>>built +define("dojo/promise/first",["../_base/array","../Deferred","../when"],function(_1,_2,_3){"use strict";var _4=_1.forEach;return function first(_5){var _6;if(_5 instanceof Array){_6=_5;}else{if(_5&&typeof _5==="object"){_6=[];for(var _7 in _5){if(Object.hasOwnProperty.call(_5,_7)){_6.push(_5[_7]);}}}}if(!_6||!_6.length){return new _2().resolve();}var _8=new _2();_4(_6,function(_9){_3(_9,_8.resolve,_8.reject);});return _8.promise;};});
\ No newline at end of file diff --git a/lib/dojo/promise/first.js.uncompressed.js b/lib/dojo/promise/first.js.uncompressed.js new file mode 100644 index 000000000..863573fa1 --- /dev/null +++ b/lib/dojo/promise/first.js.uncompressed.js @@ -0,0 +1,49 @@ +define("dojo/promise/first", [ + "../_base/array", + "../Deferred", + "../when" +], function(array, Deferred, when){ + "use strict"; + + // module: + // dojo/promise/first + + var forEach = array.forEach; + + return function first(objectOrArray){ + // summary: + // Takes multiple promises and returns a new promise that is fulfilled + // when the first of these promises is fulfilled. + // description: + // Takes multiple promises and returns a new promise that is fulfilled + // when the first of these promises is fulfilled. Canceling the returned + // promise will *not* cancel any passed promises. The promise will be + // fulfilled with the value of the first fulfilled promise. + // objectOrArray: Object|Array? + // The promises are taken from the array or object values. If no value + // is passed, the returned promise is resolved with an undefined value. + // returns: dojo/promise/Promise + + var array; + if(objectOrArray instanceof Array){ + array = objectOrArray; + }else if(objectOrArray && typeof objectOrArray === "object"){ + array = []; + for(var key in objectOrArray){ + if(Object.hasOwnProperty.call(objectOrArray, key)){ + array.push(objectOrArray[key]); + } + } + } + + if(!array || !array.length){ + return new Deferred().resolve(); + } + + var deferred = new Deferred(); + forEach(array, function(valueOrPromise){ + when(valueOrPromise, deferred.resolve, deferred.reject); + }); + return deferred.promise; // dojo/promise/Promise + }; +}); diff --git a/lib/dojo/promise/instrumentation.js b/lib/dojo/promise/instrumentation.js new file mode 100644 index 000000000..086b49a25 --- /dev/null +++ b/lib/dojo/promise/instrumentation.js @@ -0,0 +1,8 @@ +/* + Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved. + Available via Academic Free License >= 2.1 OR the modified BSD license. + see: http://dojotoolkit.org/license for details +*/ + +//>>built +define("dojo/promise/instrumentation",["./tracer","../has","../_base/lang","../_base/array"],function(_1,_2,_3,_4){function _5(_6,_7,_8){var _9="";if(_6&&_6.stack){_9+=_6.stack;}if(_7&&_7.stack){_9+="\n ----------------------------------------\n rejected"+_7.stack.split("\n").slice(1).join("\n").replace(/^\s+/," ");}if(_8&&_8.stack){_9+="\n ----------------------------------------\n"+_8.stack;}console.error(_6,_9);};function _a(_b,_c,_d,_e){if(!_c){_5(_b,_d,_e);}};var _f=[];var _10=false;var _11=1000;function _12(_13,_14,_15,_16){if(_14){_4.some(_f,function(obj,ix){if(obj.error===_13){_f.splice(ix,1);return true;}});}else{if(!_4.some(_f,function(obj){return obj.error===_13;})){_f.push({error:_13,rejection:_15,deferred:_16,timestamp:new Date().getTime()});}}if(!_10){_10=setTimeout(_17,_11);}};function _17(){var now=new Date().getTime();var _18=now-_11;_f=_4.filter(_f,function(obj){if(obj.timestamp<_18){_5(obj.error,obj.rejection,obj.deferred);return false;}return true;});if(_f.length){_10=setTimeout(_17,_f[0].timestamp+_11-now);}else{_10=false;}};return function(_19){var _1a=_2("config-useDeferredInstrumentation");if(_1a){_1.on("resolved",_3.hitch(console,"log","resolved"));_1.on("rejected",_3.hitch(console,"log","rejected"));_1.on("progress",_3.hitch(console,"log","progress"));var _1b=[];if(typeof _1a==="string"){_1b=_1a.split(",");_1a=_1b.shift();}if(_1a==="report-rejections"){_19.instrumentRejected=_a;}else{if(_1a==="report-unhandled-rejections"||_1a===true||_1a===1){_19.instrumentRejected=_12;_11=parseInt(_1b[0],10)||_11;}else{throw new Error("Unsupported instrumentation usage <"+_1a+">");}}}};});
\ No newline at end of file diff --git a/lib/dojo/promise/instrumentation.js.uncompressed.js b/lib/dojo/promise/instrumentation.js.uncompressed.js new file mode 100644 index 000000000..b32bb9841 --- /dev/null +++ b/lib/dojo/promise/instrumentation.js.uncompressed.js @@ -0,0 +1,105 @@ +define("dojo/promise/instrumentation", [ + "./tracer", + "../has", + "../_base/lang", + "../_base/array" +], function(tracer, has, lang, arrayUtil){ + function logError(error, rejection, deferred){ + var stack = ""; + if(error && error.stack){ + stack += error.stack; + } + if(rejection && rejection.stack){ + stack += "\n ----------------------------------------\n rejected" + rejection.stack.split("\n").slice(1).join("\n").replace(/^\s+/, " "); + } + if(deferred && deferred.stack){ + stack += "\n ----------------------------------------\n" + deferred.stack; + } + console.error(error, stack); + } + + function reportRejections(error, handled, rejection, deferred){ + if(!handled){ + logError(error, rejection, deferred); + } + } + + var errors = []; + var activeTimeout = false; + var unhandledWait = 1000; + function trackUnhandledRejections(error, handled, rejection, deferred){ + if(handled){ + arrayUtil.some(errors, function(obj, ix){ + if(obj.error === error){ + errors.splice(ix, 1); + return true; + } + }); + }else if(!arrayUtil.some(errors, function(obj){ return obj.error === error; })){ + errors.push({ + error: error, + rejection: rejection, + deferred: deferred, + timestamp: new Date().getTime() + }); + } + + if(!activeTimeout){ + activeTimeout = setTimeout(logRejected, unhandledWait); + } + } + + function logRejected(){ + var now = new Date().getTime(); + var reportBefore = now - unhandledWait; + errors = arrayUtil.filter(errors, function(obj){ + if(obj.timestamp < reportBefore){ + logError(obj.error, obj.rejection, obj.deferred); + return false; + } + return true; + }); + + if(errors.length){ + activeTimeout = setTimeout(logRejected, errors[0].timestamp + unhandledWait - now); + }else{ + activeTimeout = false; + } + } + + return function(Deferred){ + // summary: + // Initialize instrumentation for the Deferred class. + // description: + // Initialize instrumentation for the Deferred class. + // Done automatically by `dojo/Deferred` if the + // `deferredInstrumentation` and `useDeferredInstrumentation` + // config options are set. + // + // Sets up `dojo/promise/tracer` to log to the console. + // + // Sets up instrumentation of rejected deferreds so unhandled + // errors are logged to the console. + + var usage = has("config-useDeferredInstrumentation"); + if(usage){ + tracer.on("resolved", lang.hitch(console, "log", "resolved")); + tracer.on("rejected", lang.hitch(console, "log", "rejected")); + tracer.on("progress", lang.hitch(console, "log", "progress")); + + var args = []; + if(typeof usage === "string"){ + args = usage.split(","); + usage = args.shift(); + } + if(usage === "report-rejections"){ + Deferred.instrumentRejected = reportRejections; + }else if(usage === "report-unhandled-rejections" || usage === true || usage === 1){ + Deferred.instrumentRejected = trackUnhandledRejections; + unhandledWait = parseInt(args[0], 10) || unhandledWait; + }else{ + throw new Error("Unsupported instrumentation usage <" + usage + ">"); + } + } + }; +}); diff --git a/lib/dojo/promise/tracer.js b/lib/dojo/promise/tracer.js new file mode 100644 index 000000000..906b9452d --- /dev/null +++ b/lib/dojo/promise/tracer.js @@ -0,0 +1,8 @@ +/* + Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved. + Available via Academic Free License >= 2.1 OR the modified BSD license. + see: http://dojotoolkit.org/license for details +*/ + +//>>built +define("dojo/promise/tracer",["../_base/lang","./Promise","../Evented"],function(_1,_2,_3){"use strict";var _4=new _3;var _5=_4.emit;_4.emit=null;function _6(_7){setTimeout(function(){_5.apply(_4,_7);},0);};_2.prototype.trace=function(){var _8=_1._toArray(arguments);this.then(function(_9){_6(["resolved",_9].concat(_8));},function(_a){_6(["rejected",_a].concat(_8));},function(_b){_6(["progress",_b].concat(_8));});return this;};_2.prototype.traceRejected=function(){var _c=_1._toArray(arguments);this.otherwise(function(_d){_6(["rejected",_d].concat(_c));});return this;};return _4;});
\ No newline at end of file diff --git a/lib/dojo/promise/tracer.js.uncompressed.js b/lib/dojo/promise/tracer.js.uncompressed.js new file mode 100644 index 000000000..b5380cc32 --- /dev/null +++ b/lib/dojo/promise/tracer.js.uncompressed.js @@ -0,0 +1,85 @@ +define("dojo/promise/tracer", [ + "../_base/lang", + "./Promise", + "../Evented" +], function(lang, Promise, Evented){ + "use strict"; + + // module: + // dojo/promise/tracer + + /*===== + return { + // summary: + // Trace promise fulfillment. + // description: + // Trace promise fulfillment. Calling `.trace()` or `.traceError()` on a + // promise enables tracing. Will emit `resolved`, `rejected` or `progress` + // events. + + on: function(type, listener){ + // summary: + // Subscribe to traces. + // description: + // See `dojo/Evented#on()`. + // type: String + // `resolved`, `rejected`, or `progress` + // listener: Function + // The listener is passed the traced value and any arguments + // that were used with the `.trace()` call. + } + }; + =====*/ + + var evented = new Evented; + var emit = evented.emit; + evented.emit = null; + // Emit events asynchronously since they should not change the promise state. + function emitAsync(args){ + setTimeout(function(){ + emit.apply(evented, args); + }, 0); + } + + Promise.prototype.trace = function(){ + // summary: + // Trace the promise. + // description: + // Tracing allows you to transparently log progress, + // resolution and rejection of promises, without affecting the + // promise itself. Any arguments passed to `trace()` are + // emitted in trace events. See `dojo/promise/tracer` on how + // to handle traces. + // returns: dojo/promise/Promise + // The promise instance `trace()` is called on. + + var args = lang._toArray(arguments); + this.then( + function(value){ emitAsync(["resolved", value].concat(args)); }, + function(error){ emitAsync(["rejected", error].concat(args)); }, + function(update){ emitAsync(["progress", update].concat(args)); } + ); + return this; + }; + + Promise.prototype.traceRejected = function(){ + // summary: + // Trace rejection of the promise. + // description: + // Tracing allows you to transparently log progress, + // resolution and rejection of promises, without affecting the + // promise itself. Any arguments passed to `trace()` are + // emitted in trace events. See `dojo/promise/tracer` on how + // to handle traces. + // returns: dojo/promise/Promise + // The promise instance `traceRejected()` is called on. + + var args = lang._toArray(arguments); + this.otherwise(function(error){ + emitAsync(["rejected", error].concat(args)); + }); + return this; + }; + + return evented; +}); |