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

RSVP library updated

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