Commit 1ed8109e authored by Romain Courteaud's avatar Romain Courteaud

WIP Update RSVP version

parent 86121def
...@@ -602,7 +602,7 @@ define("rsvp/promise", ...@@ -602,7 +602,7 @@ define("rsvp/promise",
then: function(done, fail) { then: function(done, fail) {
this.off('error', onerror); this.off('error', onerror);
var thenPromise = new this.constructor(function() {}, var thenPromise = new Promise(function() {},
function () { function () {
thenPromise.trigger('promise:cancelled', {}); thenPromise.trigger('promise:cancelled', {});
}); });
...@@ -736,97 +736,76 @@ define("rsvp/queue", ...@@ -736,97 +736,76 @@ define("rsvp/queue",
ResolvedQueueError.prototype.constructor = ResolvedQueueError; ResolvedQueueError.prototype.constructor = ResolvedQueueError;
var Queue = function() { var Queue = function() {
var queue = this,
promise_list = [],
promise,
fulfill,
reject,
resolved;
if (!(this instanceof Queue)) { if (!(this instanceof Queue)) {
return new Queue(); return new Queue();
} }
function canceller() { var promise_stack = [],
for (var i = 0; i < 2; i++) { // handleQueue
promise_list[i].cancel(); detect_end_index = 1;
Promise.call(this, function (resolveQueue, rejectQueue) {
var detectQueueSuccess,
detectQueueError;
function handleQueue() {
if (promise_stack.length === detect_end_index) {
return true;
}
promise_stack.splice(0, detect_end_index);
promise_stack.push(
promise_stack[promise_stack.length - 1].then(detectQueueSuccess,
detectQueueError)
);
detect_end_index = promise_stack.length;
return false;
} }
}
promise = new Promise(function(done, fail) { detectQueueSuccess = function (fulfillmentValue) {
fulfill = function (fulfillmentValue) { if (handleQueue()) {
if (resolved) {return;} return resolveQueue(fulfillmentValue);
queue.isFulfilled = true; }
queue.fulfillmentValue = fulfillmentValue; return fulfillmentValue;
resolved = true;
return done(fulfillmentValue);
}; };
reject = function (rejectedReason) {
if (resolved) {return;} detectQueueError = function (rejectedReason) {
queue.isRejected = true; if (handleQueue()) {
queue.rejectedReason = rejectedReason ; return rejectQueue(rejectedReason);
resolved = true; }
return fail(rejectedReason); throw rejectedReason;
}; };
}, canceller);
promise_list.push(resolve()); // Resolve by default
promise_list.push(promise_list[0].then(function () { promise_stack.push(
promise_list.splice(0, 2); resolve().then(detectQueueSuccess)
if (promise_list.length === 0) { );
fulfill();
}
}));
queue.cancel = function () { }, function () {
if (resolved) {return;} // Cancel all created promises
resolved = true; var i;
promise.cancel(); for (i = 0; i < promise_stack.length; i += 1) {
promise.fail(function (rejectedReason) { promise_stack[i].cancel();
queue.isRejected = true; }
queue.rejectedReason = rejectedReason;
});
};
queue.then = function () {
return promise.then.apply(promise, arguments);
};
queue.push = function(done, fail) { });
var last_promise = promise_list[promise_list.length - 1],
next_promise;
if (resolved) { this.push = function (done, fail) {
if (this.isFulfilled || this.isRejected) {
throw new ResolvedQueueError(); throw new ResolvedQueueError();
} }
promise_stack.push(
next_promise = last_promise.then(done, fail); promise_stack[promise_stack.length - 1].then(done, fail)
promise_list.push(next_promise); );
// Handle pop
promise_list.push(next_promise.then(function (fulfillmentValue) {
promise_list.splice(0, 2);
if (promise_list.length === 0) {
fulfill(fulfillmentValue);
} else {
return fulfillmentValue;
}
}, function (rejectedReason) {
promise_list.splice(0, 2);
if (promise_list.length === 0) {
reject(rejectedReason);
} else {
throw rejectedReason;
}
}));
return this; return this;
}; };
return this;
}; };
Queue.prototype = Object.create(Promise.prototype); Queue.prototype = Object.create(Promise.prototype);
Queue.prototype.constructor = Queue; Queue.prototype.constructor = Queue;
__exports__.Queue = Queue; __exports__.Queue = Queue;
__exports__.ResolvedQueueError = ResolvedQueueError; __exports__.ResolvedQueueError = ResolvedQueueError;
}); });
......
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