Commit 89646829 authored by Romain Courteaud's avatar Romain Courteaud

Revert "Reduce number of array"

This reverts commit 974b8596.
parent 9b922f24
......@@ -12,30 +12,39 @@ function ResolvedQueueError(message) {
ResolvedQueueError.prototype = new Error();
ResolvedQueueError.prototype.constructor = ResolvedQueueError;
var last_promise,
i;
var Queue = function() {
if (!(this instanceof Queue)) {
return new Queue();
}
var promise_stack = [],
// handleQueue
detect_end_index = 1;
var queue = this,
promise_stack = [],
then_stack = [];
Promise.call(this, function (resolveQueue, rejectQueue) {
Promise.call(queue, function (resolveQueue, rejectQueue) {
var detectQueueSuccess,
detectQueueError;
function handleQueue() {
if (promise_stack.length === detect_end_index) {
if (then_stack.length === 0) {
return true;
}
promise_stack.splice(0, detect_end_index);
last_promise = promise_stack[promise_stack.length - 1];
promise_stack.splice(0, promise_stack.length);
for (i = then_stack.length - 1; i >= 0; i -= 2) {
last_promise = last_promise.then(then_stack[i - 1], then_stack[i]);
promise_stack.push(last_promise);
}
then_stack.splice(0, then_stack.length);
promise_stack.push(
promise_stack[promise_stack.length - 1].then(detectQueueSuccess,
detectQueueError)
last_promise.then(detectQueueSuccess,
detectQueueError)
);
detect_end_index = promise_stack.length;
return false;
}
......@@ -55,32 +64,31 @@ var Queue = function() {
// Resolve by default
promise_stack.push(
resolve().then(detectQueueSuccess)
resolve().then(detectQueueSuccess, detectQueueError)
);
}, function () {
// Cancel all created promises
var i;
then_stack.splice(0, then_stack.length);
for (i = 0; i < promise_stack.length; i += 1) {
promise_stack[i].cancel();
}
});
this.push = function (done, fail) {
if (this.isFulfilled || this.isRejected) {
queue.push = function (done, fail) {
if (queue.isFulfilled || queue.isRejected) {
throw new ResolvedQueueError();
}
promise_stack.push(
promise_stack[promise_stack.length - 1].then(done, fail)
);
return this;
then_stack.unshift(done, fail);
return queue;
};
return this;
return queue;
};
Queue.prototype = Object.create(Promise.prototype);
Queue.prototype.constructor = Queue;
export { Queue, ResolvedQueueError };
\ No newline at end of file
export { Queue, 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