Commit 8e55feab authored by Tristan Cavelier's avatar Tristan Cavelier

RSVP library updated

parent f42a2f78
...@@ -59,11 +59,16 @@ define("rsvp/all", ...@@ -59,11 +59,16 @@ define("rsvp/all",
}; };
} }
function cancelAll(rejectionValue) {
reject(rejectionValue);
canceller();
}
for (var i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
promise = promises[i]; promise = promises[i];
if (promise && typeof promise.then === 'function') { if (promise && typeof promise.then === 'function') {
promise.then(resolver(i), reject, notifier(i)); promise.then(resolver(i), cancelAll, notifier(i));
} else { } else {
resolveAll(i, promise); resolveAll(i, promise);
} }
...@@ -506,6 +511,9 @@ define("rsvp/promise", ...@@ -506,6 +511,9 @@ define("rsvp/promise",
var hasCallback = isFunction(callback), var hasCallback = isFunction(callback),
value, error, succeeded, failed; value, error, succeeded, failed;
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
if (hasCallback) { if (hasCallback) {
try { try {
value = callback(event.detail); value = callback(event.detail);
...@@ -623,6 +631,11 @@ define("rsvp/promise", ...@@ -623,6 +631,11 @@ define("rsvp/promise",
then = value.then; then = value.then;
if (isFunction(then)) { if (isFunction(then)) {
if (isFunction(value.on)) {
value.on('promise:notified', function (event) {
notify(promise, event.detail);
});
}
promise.on('promise:cancelled', function(event) { promise.on('promise:cancelled', function(event) {
if (isFunction(value.cancel)) { if (isFunction(value.cancel)) {
value.cancel(); value.cancel();
...@@ -657,6 +670,8 @@ define("rsvp/promise", ...@@ -657,6 +670,8 @@ define("rsvp/promise",
function fulfill(promise, value) { function fulfill(promise, value) {
config.async(function() { config.async(function() {
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
promise.isFulfilled = true; promise.isFulfilled = true;
promise.fulfillmentValue = value; promise.fulfillmentValue = value;
...@@ -665,6 +680,8 @@ define("rsvp/promise", ...@@ -665,6 +680,8 @@ define("rsvp/promise",
function reject(promise, value) { function reject(promise, value) {
config.async(function() { config.async(function() {
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
promise.trigger('promise:failed', { detail: value }); promise.trigger('promise:failed', { detail: value });
promise.isRejected = true; promise.isRejected = true;
promise.rejectedReason = value; promise.rejectedReason = value;
...@@ -699,25 +716,41 @@ define("rsvp/queue", ...@@ -699,25 +716,41 @@ define("rsvp/queue",
ResolvedQueueError.prototype.constructor = ResolvedQueueError; ResolvedQueueError.prototype.constructor = ResolvedQueueError;
var Queue = function() { var Queue = function() {
var promise = this, var queue = this,
promise_list = [], promise_list = [],
promise,
fulfill, fulfill,
reject; reject,
notify,
resolved;
if (!(this instanceof Queue)) { if (!(this instanceof Queue)) {
return new Queue(); return new Queue();
} }
function canceller() { function canceller() {
for (var i = 0; i < promise_list.length; i++) { for (var i = 0; i < 2; i++) {
promise_list[i].cancel(); promise_list[i].cancel();
} }
} }
Promise.apply(promise, [function (done, fail) { promise = new Promise(function(done, fail, progress) {
fulfill = done; fulfill = function (fulfillmentValue) {
reject = fail; if (resolved) {return;}
}, canceller]); queue.isFulfilled = true;
queue.fulfillmentValue = fulfillmentValue;
resolved = true;
return done(fulfillmentValue);
};
reject = function (rejectedReason) {
if (resolved) {return;}
queue.isRejected = true;
queue.rejectedReason = rejectedReason ;
resolved = true;
return fail(rejectedReason);
};
notify = progress;
}, canceller);
promise_list.push(delay()); promise_list.push(delay());
promise_list.push(promise_list[0].then(function () { promise_list.push(promise_list[0].then(function () {
...@@ -727,19 +760,32 @@ define("rsvp/queue", ...@@ -727,19 +760,32 @@ define("rsvp/queue",
} }
})); }));
promise.push = function(done, fail) { queue.cancel = function () {
if (resolved) {return;}
resolved = true;
promise.cancel();
promise.fail(function (rejectedReason) {
queue.isRejected = true;
queue.rejectedReason = rejectedReason;
});
};
queue.then = function () {
return promise.then.apply(promise, arguments);
};
queue.push = function(done, fail, progress) {
var last_promise = promise_list[promise_list.length - 1], var last_promise = promise_list[promise_list.length - 1],
next_promise; next_promise;
if (this.isRejected || this.isFulfilled) { if (resolved) {
throw new ResolvedQueueError(); throw new ResolvedQueueError();
} }
next_promise = last_promise.then(done, fail); next_promise = last_promise.then(done, fail, progress);
promise_list.push(next_promise); promise_list.push(next_promise);
// Handle pop // Handle pop
promise_list.push(next_promise.then(function (fulfillmentValue) { last_promise = next_promise.then(function (fulfillmentValue) {
promise_list.splice(0, 2); promise_list.splice(0, 2);
if (promise_list.length === 0) { if (promise_list.length === 0) {
fulfill(fulfillmentValue); fulfill(fulfillmentValue);
...@@ -747,11 +793,19 @@ define("rsvp/queue", ...@@ -747,11 +793,19 @@ define("rsvp/queue",
return fulfillmentValue; return fulfillmentValue;
} }
}, function (rejectedReason) { }, function (rejectedReason) {
if (!(promise.isRejected || promise.isFulfilled)) { promise_list.splice(0, 2);
if (promise_list.length === 0) {
reject(rejectedReason); reject(rejectedReason);
} else {
throw rejectedReason;
} }
throw rejectedReason; }, function (notificationValue) {
})); if (promise_list[promise_list.length - 1] === last_promise) {
notify(notificationValue);
}
return notificationValue;
});
promise_list.push(last_promise);
return this; return this;
}; };
...@@ -787,7 +841,17 @@ define("rsvp/resolve", ...@@ -787,7 +841,17 @@ define("rsvp/resolve",
function resolve(thenable) { function resolve(thenable) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
resolve(thenable); if (typeof thenable === "object" && thenable !== null) {
var then = thenable.then;
if ((then !== undefined) && (typeof then === "function")) {
return then.apply(thenable, [resolve, reject]);
}
}
return resolve(thenable);
}, function () {
if ((thenable !== undefined) && (thenable.cancel !== undefined)) {
thenable.cancel();
}
}); });
} }
......
...@@ -96,11 +96,16 @@ define("rsvp/all", ...@@ -96,11 +96,16 @@ define("rsvp/all",
}; };
} }
function cancelAll(rejectionValue) {
reject(rejectionValue);
canceller();
}
for (var i = 0; i < promises.length; i++) { for (var i = 0; i < promises.length; i++) {
promise = promises[i]; promise = promises[i];
if (promise && typeof promise.then === 'function') { if (promise && typeof promise.then === 'function') {
promise.then(resolver(i), reject, notifier(i)); promise.then(resolver(i), cancelAll, notifier(i));
} else { } else {
resolveAll(i, promise); resolveAll(i, promise);
} }
...@@ -543,6 +548,9 @@ define("rsvp/promise", ...@@ -543,6 +548,9 @@ define("rsvp/promise",
var hasCallback = isFunction(callback), var hasCallback = isFunction(callback),
value, error, succeeded, failed; value, error, succeeded, failed;
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
if (hasCallback) { if (hasCallback) {
try { try {
value = callback(event.detail); value = callback(event.detail);
...@@ -660,6 +668,11 @@ define("rsvp/promise", ...@@ -660,6 +668,11 @@ define("rsvp/promise",
then = value.then; then = value.then;
if (isFunction(then)) { if (isFunction(then)) {
if (isFunction(value.on)) {
value.on('promise:notified', function (event) {
notify(promise, event.detail);
});
}
promise.on('promise:cancelled', function(event) { promise.on('promise:cancelled', function(event) {
if (isFunction(value.cancel)) { if (isFunction(value.cancel)) {
value.cancel(); value.cancel();
...@@ -694,6 +707,8 @@ define("rsvp/promise", ...@@ -694,6 +707,8 @@ define("rsvp/promise",
function fulfill(promise, value) { function fulfill(promise, value) {
config.async(function() { config.async(function() {
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
promise.isFulfilled = true; promise.isFulfilled = true;
promise.fulfillmentValue = value; promise.fulfillmentValue = value;
...@@ -702,6 +717,8 @@ define("rsvp/promise", ...@@ -702,6 +717,8 @@ define("rsvp/promise",
function reject(promise, value) { function reject(promise, value) {
config.async(function() { config.async(function() {
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
promise.trigger('promise:failed', { detail: value }); promise.trigger('promise:failed', { detail: value });
promise.isRejected = true; promise.isRejected = true;
promise.rejectedReason = value; promise.rejectedReason = value;
...@@ -736,25 +753,41 @@ define("rsvp/queue", ...@@ -736,25 +753,41 @@ define("rsvp/queue",
ResolvedQueueError.prototype.constructor = ResolvedQueueError; ResolvedQueueError.prototype.constructor = ResolvedQueueError;
var Queue = function() { var Queue = function() {
var promise = this, var queue = this,
promise_list = [], promise_list = [],
promise,
fulfill, fulfill,
reject; reject,
notify,
resolved;
if (!(this instanceof Queue)) { if (!(this instanceof Queue)) {
return new Queue(); return new Queue();
} }
function canceller() { function canceller() {
for (var i = 0; i < promise_list.length; i++) { for (var i = 0; i < 2; i++) {
promise_list[i].cancel(); promise_list[i].cancel();
} }
} }
Promise.apply(promise, [function (done, fail) { promise = new Promise(function(done, fail, progress) {
fulfill = done; fulfill = function (fulfillmentValue) {
reject = fail; if (resolved) {return;}
}, canceller]); queue.isFulfilled = true;
queue.fulfillmentValue = fulfillmentValue;
resolved = true;
return done(fulfillmentValue);
};
reject = function (rejectedReason) {
if (resolved) {return;}
queue.isRejected = true;
queue.rejectedReason = rejectedReason ;
resolved = true;
return fail(rejectedReason);
};
notify = progress;
}, canceller);
promise_list.push(delay()); promise_list.push(delay());
promise_list.push(promise_list[0].then(function () { promise_list.push(promise_list[0].then(function () {
...@@ -764,19 +797,32 @@ define("rsvp/queue", ...@@ -764,19 +797,32 @@ define("rsvp/queue",
} }
})); }));
promise.push = function(done, fail) { queue.cancel = function () {
if (resolved) {return;}
resolved = true;
promise.cancel();
promise.fail(function (rejectedReason) {
queue.isRejected = true;
queue.rejectedReason = rejectedReason;
});
};
queue.then = function () {
return promise.then.apply(promise, arguments);
};
queue.push = function(done, fail, progress) {
var last_promise = promise_list[promise_list.length - 1], var last_promise = promise_list[promise_list.length - 1],
next_promise; next_promise;
if (this.isRejected || this.isFulfilled) { if (resolved) {
throw new ResolvedQueueError(); throw new ResolvedQueueError();
} }
next_promise = last_promise.then(done, fail); next_promise = last_promise.then(done, fail, progress);
promise_list.push(next_promise); promise_list.push(next_promise);
// Handle pop // Handle pop
promise_list.push(next_promise.then(function (fulfillmentValue) { last_promise = next_promise.then(function (fulfillmentValue) {
promise_list.splice(0, 2); promise_list.splice(0, 2);
if (promise_list.length === 0) { if (promise_list.length === 0) {
fulfill(fulfillmentValue); fulfill(fulfillmentValue);
...@@ -784,11 +830,19 @@ define("rsvp/queue", ...@@ -784,11 +830,19 @@ define("rsvp/queue",
return fulfillmentValue; return fulfillmentValue;
} }
}, function (rejectedReason) { }, function (rejectedReason) {
if (!(promise.isRejected || promise.isFulfilled)) { promise_list.splice(0, 2);
if (promise_list.length === 0) {
reject(rejectedReason); reject(rejectedReason);
} else {
throw rejectedReason;
} }
throw rejectedReason; }, function (notificationValue) {
})); if (promise_list[promise_list.length - 1] === last_promise) {
notify(notificationValue);
}
return notificationValue;
});
promise_list.push(last_promise);
return this; return this;
}; };
...@@ -824,7 +878,17 @@ define("rsvp/resolve", ...@@ -824,7 +878,17 @@ define("rsvp/resolve",
function resolve(thenable) { function resolve(thenable) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
resolve(thenable); if (typeof thenable === "object" && thenable !== null) {
var then = thenable.then;
if ((then !== undefined) && (typeof then === "function")) {
return then.apply(thenable, [resolve, reject]);
}
}
return resolve(thenable);
}, function () {
if ((thenable !== undefined) && (thenable.cancel !== undefined)) {
thenable.cancel();
}
}); });
} }
......
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