From 0584797edfb95bd8c319d754a03686b187c6772e Mon Sep 17 00:00:00 2001 From: Michael Spencer Date: Sun, 18 Jan 2015 21:56:22 -0600 Subject: [PATCH] Support for joining promises --- modules/Material/Extras/js/promises.js | 50 +++++++++++++++++++++++++- modules/Material/Extras/qmldir | 2 +- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/modules/Material/Extras/js/promises.js b/modules/Material/Extras/js/promises.js index 6def4f5..ea5d419 100644 --- a/modules/Material/Extras/js/promises.js +++ b/modules/Material/Extras/js/promises.js @@ -19,7 +19,21 @@ .pragma library -var Promise = function (delayed) { +function subclass(constructor, superConstructor) { + function surrogateConstructor() {} + + surrogateConstructor.prototype = superConstructor.prototype; + + var prototypeObject = new surrogateConstructor(); + prototypeObject.constructor = constructor; + + constructor.prototype = prototypeObject; +} + + +// Promise class + +function Promise(delayed) { this.thenHandlers = [] this.onDone = [] this.onError = [] @@ -68,3 +82,37 @@ Promise.prototype.reject = function (error) { Promise.prototype.start = function(args) { this.code(args) } + +// JoinedPromise class + +subclass(JoinedPromise, Promise) + +function JoinedPromise() { + Promise.call(this); + + this.promiseCount = 0 +} + +JoinedPromise.prototype.add = function(promise) { + this.promiseCount++ + + var join = this + + promise.done(function(data) { + join.promiseCount-- + + if (join.promiseCount == 0) { + print("All joined promises done!") + join.resolve() + } + }) + + promise.error(function (error) { + join.promiseCount = -1 + + print("A joined promise failed, shortcutting to failure!") + join.reject(error) + }) + + return this +} diff --git a/modules/Material/Extras/qmldir b/modules/Material/Extras/qmldir index 2b28eee..2644ffe 100644 --- a/modules/Material/Extras/qmldir +++ b/modules/Material/Extras/qmldir @@ -6,7 +6,7 @@ Document 0.1 Document.qml Object 0.1 Object.qml DateUtils 0.1 js/dateutils.js -HttpLib 0.1 js/httplib.js +Http 0.1 js/httplib.js ListUtils 0.1 js/listutils.js Promises 0.1 js/promises.js Utils 0.1 js/utils.js