diff options
Diffstat (limited to 'js/src/Ice/Promise.js')
-rw-r--r-- | js/src/Ice/Promise.js | 494 |
1 files changed, 245 insertions, 249 deletions
diff --git a/js/src/Ice/Promise.js b/js/src/Ice/Promise.js index 91608d13873..de0a2ce2bfe 100644 --- a/js/src/Ice/Promise.js +++ b/js/src/Ice/Promise.js @@ -7,291 +7,287 @@ // // ********************************************************************** -(function(global){ - require("Ice/Class"); +var Ice = require("../Ice/Class").Ice; - var Ice = global.Ice || {}; - - // - // Promise State - // - var State = {Pending: 0, Success: 1, Failed: 2}; - - var resolveImp = function(self, listener) +// +// Promise State +// +var State = {Pending: 0, Success: 1, Failed: 2}; + +var resolveImp = function(self, listener) +{ + var callback = self.__state === State.Success ? listener.onResponse : listener.onException; + try { - var callback = self.__state === State.Success ? listener.onResponse : listener.onException; - try + if(typeof callback !== "function") + { + listener.promise.setState(self.__state, self._args); + } + else { - if(typeof callback !== "function") + var result = callback.apply(null, self._args); + + // + // Callback can return a new promise. + // + if(result && typeof result.then == "function") { - listener.promise.setState(self.__state, self._args); + result.then( + function() + { + var args = arguments; + listener.promise.succeed.apply(listener.promise, args); + }, + function() + { + var args = arguments; + listener.promise.fail.apply(listener.promise, args); + }); } else { - var result = callback.apply(null, self._args); - - // - // Callback can return a new promise. - // - if(result && typeof result.then == "function") - { - result.then( - function() - { - var args = arguments; - listener.promise.succeed.apply(listener.promise, args); - }, - function() - { - var args = arguments; - listener.promise.fail.apply(listener.promise, args); - }); - } - else - { - listener.promise.succeed(result); - } + listener.promise.succeed(result); } } - catch(e) - { - listener.promise.fail.call(listener.promise, e); - } - }; + } + catch(e) + { + listener.promise.fail.call(listener.promise, e); + } +}; - var Promise = Ice.Class({ - __init__: function() - { - this.__state = State.Pending; - this.__listeners = []; - }, - then: function(onResponse, onException) - { - var promise = new Promise(); - var self = this; - // - // Use setTimeout so the listeners are not resolved until the call stack is empty. - // - setTimeout( - function() - { - self.__listeners.push( - { - promise:promise, - onResponse:onResponse, - onException:onException - }); - self.resolve(); - }, 0); - return promise; - }, - exception: function(onException) - { - return this.then(null, onException); - }, - finally: function(cb) +var Promise = Ice.Class({ + __init__: function() + { + this.__state = State.Pending; + this.__listeners = []; + }, + then: function(onResponse, onException) + { + var promise = new Promise(); + var self = this; + // + // Use setTimeout so the listeners are not resolved until the call stack is empty. + // + setTimeout( + function() + { + self.__listeners.push( + { + promise:promise, + onResponse:onResponse, + onException:onException + }); + self.resolve(); + }, 0); + return promise; + }, + exception: function(onException) + { + return this.then(null, onException); + }, + finally: function(cb) + { + var p = new Promise(); + var self = this; + + var finallyHandler = function(method) { - var p = new Promise(); - var self = this; - - var finallyHandler = function(method) + return function() { - return function() + var args = arguments; + try { - var args = arguments; - try + var result = cb.apply(null, args); + if(result && typeof result.then == "function") { - var result = cb.apply(null, args); - if(result && typeof result.then == "function") - { - var handler = function(){ method.apply(p, args); }; - result.then(handler).exception(handler); - } - else - { - method.apply(p, args); - } + var handler = function(){ method.apply(p, args); }; + result.then(handler).exception(handler); } - catch(e) + else { method.apply(p, args); } - }; - }; - - setTimeout( - function(){ - self.then(finallyHandler(p.succeed), finallyHandler(p.fail)); - }); - return p; - }, - delay: function(ms) - { - var p = new Promise(); - - var self = this; - - var delayHandler = function(promise, method) - { - return function() + } + catch(e) { - var args = arguments; - setTimeout( - function() - { - method.apply(promise, args); - }, - ms); - }; + method.apply(p, args); + } }; - - setTimeout( - function() - { - self.then(delayHandler(p, p.succeed), - delayHandler(p, p.fail)); - }); - - return p; - }, - resolve: function() + }; + + setTimeout( + function(){ + self.then(finallyHandler(p.succeed), finallyHandler(p.fail)); + }); + return p; + }, + delay: function(ms) + { + var p = new Promise(); + + var self = this; + + var delayHandler = function(promise, method) { - if(this.__state === State.Pending) - { - return; - } - - var obj; - while((obj = this.__listeners.pop())) + return function() { - // - // We use a separate function here to capture the listeners - // in the loop. - // - resolveImp(this, obj); - } - }, - setState: function(state, args) - { - if(this.__state === State.Pending && state !== State.Pending) + var args = arguments; + setTimeout( + function() + { + method.apply(promise, args); + }, + ms); + }; + }; + + setTimeout( + function() { - this.__state = state; - this._args = args; - // - // Use setTimeout so the listeners are not resolved until the call stack is empty. - // - var self = this; - setTimeout(function(){ self.resolve(); }, 0); - } - }, - succeed: function() - { - var args = arguments; - this.setState(State.Success, args); - return this; - }, - fail: function() - { - var args = arguments; - this.setState(State.Failed, args); - return this; - }, - succeeded: function() - { - return this.__state === State.Success; - }, - failed: function() - { - return this.__state === State.Failed; - }, - completed: function() - { - return this.__state !== State.Pending; - } - }); - - // - // Create a new promise object that is fulfilled when all the promise arguments - // are fulfilled or is rejected when one of the promises is rejected. - // - Promise.all = function() + self.then(delayHandler(p, p.succeed), + delayHandler(p, p.fail)); + }); + + return p; + }, + resolve: function() { - // If only one argument is provided, check if the argument is an array - if(arguments.length === 1 && arguments[0] instanceof Array) + if(this.__state === State.Pending) { - return Promise.all.apply(this, arguments[0]); + return; } - var promise = new Promise(); - var promises = Array.prototype.slice.call(arguments); - var results = new Array(arguments.length); - - var pending = promises.length; - if(pending === 0) + var obj; + while((obj = this.__listeners.pop())) { - promise.succeed.apply(promise, results); + // + // We use a separate function here to capture the listeners + // in the loop. + // + resolveImp(this, obj); } - for(var i = 0; i < promises.length; ++i) + }, + setState: function(state, args) + { + if(this.__state === State.Pending && state !== State.Pending) { + this.__state = state; + this._args = args; // - // Create an anonymous function to capture the loop index + // Use setTimeout so the listeners are not resolved until the call stack is empty. // - - /*jshint -W083 */ - (function(j) + var self = this; + setTimeout(function(){ self.resolve(); }, 0); + } + }, + succeed: function() + { + var args = arguments; + this.setState(State.Success, args); + return this; + }, + fail: function() + { + var args = arguments; + this.setState(State.Failed, args); + return this; + }, + succeeded: function() + { + return this.__state === State.Success; + }, + failed: function() + { + return this.__state === State.Failed; + }, + completed: function() + { + return this.__state !== State.Pending; + } +}); + +// +// Create a new promise object that is fulfilled when all the promise arguments +// are fulfilled or is rejected when one of the promises is rejected. +// +Promise.all = function() +{ + // If only one argument is provided, check if the argument is an array + if(arguments.length === 1 && arguments[0] instanceof Array) + { + return Promise.all.apply(this, arguments[0]); + } + + var promise = new Promise(); + var promises = Array.prototype.slice.call(arguments); + var results = new Array(arguments.length); + + var pending = promises.length; + if(pending === 0) + { + promise.succeed.apply(promise, results); + } + for(var i = 0; i < promises.length; ++i) + { + // + // Create an anonymous function to capture the loop index + // + + /*jshint -W083 */ + (function(j) + { + if(promises[j] && typeof promises[j].then == "function") { - if(promises[j] && typeof promises[j].then == "function") - { - promises[j].then( - function() - { - results[j] = arguments; - pending--; - if(pending === 0) - { - promise.succeed.apply(promise, results); - } - }, - function() + promises[j].then( + function() + { + results[j] = arguments; + pending--; + if(pending === 0) { - promise.fail.apply(promise, arguments); - }); - } - else - { - results[j] = promises[j]; - pending--; - if(pending === 0) + promise.succeed.apply(promise, results); + } + }, + function() { - promise.succeed.apply(promise, results); - } + promise.fail.apply(promise, arguments); + }); + } + else + { + results[j] = promises[j]; + pending--; + if(pending === 0) + { + promise.succeed.apply(promise, results); } - }(i)); - /*jshint +W083 */ - } - return promise; - }; - - Promise.try = function(onResponse) + } + }(i)); + /*jshint +W083 */ + } + return promise; +}; + +Promise.try = function(onResponse) +{ + return new Promise().succeed().then(onResponse); +}; + +Promise.delay = function(ms) +{ + if(arguments.length > 1) { - return new Promise().succeed().then(onResponse); - }; - - Promise.delay = function(ms) + var p = new Promise(); + var args = Array.prototype.slice.call(arguments); + ms = args.pop(); + return p.succeed.apply(p, args).delay(ms); + } + else { - if(arguments.length > 1) - { - var p = new Promise(); - var args = Array.prototype.slice.call(arguments); - ms = args.pop(); - return p.succeed.apply(p, args).delay(ms); - } - else - { - return new Promise().succeed().delay(ms); - } - }; + return new Promise().succeed().delay(ms); + } +}; - Ice.Promise = Promise; - global.Ice = Ice; -}(typeof (global) === "undefined" ? window : global)); +Ice.Promise = Promise; +module.exports.Ice = Ice; |