Skip to content

Commit

Permalink
Support for joining promises
Browse files Browse the repository at this point in the history
  • Loading branch information
iBelieve committed Jan 19, 2015
1 parent 6e1c52a commit 0584797
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
50 changes: 49 additions & 1 deletion modules/Material/Extras/js/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion modules/Material/Extras/qmldir
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0584797

Please sign in to comment.