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