Commit 974b8596 authored by Romain Courteaud's avatar Romain Courteaud

Reduce number of array

parent e410c7ce
...@@ -12,39 +12,30 @@ function ResolvedQueueError(message) { ...@@ -12,39 +12,30 @@ 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 queue = this, var promise_stack = [],
promise_stack = [], // handleQueue
then_stack = []; detect_end_index = 1;
Promise.call(queue, function (resolveQueue, rejectQueue) { Promise.call(this, function (resolveQueue, rejectQueue) {
var detectQueueSuccess, var detectQueueSuccess,
detectQueueError; detectQueueError;
function handleQueue() { function handleQueue() {
if (then_stack.length === 0) { if (promise_stack.length === detect_end_index) {
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(
last_promise.then(detectQueueSuccess, promise_stack[promise_stack.length - 1].then(detectQueueSuccess,
detectQueueError) detectQueueError)
); );
detect_end_index = promise_stack.length;
return false; return false;
} }
...@@ -64,28 +55,29 @@ var Queue = function() { ...@@ -64,28 +55,29 @@ var Queue = function() {
// Resolve by default // Resolve by default
promise_stack.push( promise_stack.push(
resolve().then(detectQueueSuccess, detectQueueError) resolve().then(detectQueueSuccess)
); );
}, 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();
} }
}); });
queue.push = function (done, fail) { this.push = function (done, fail) {
if (queue.isFulfilled || queue.isRejected) { if (this.isFulfilled || this.isRejected) {
throw new ResolvedQueueError(); throw new ResolvedQueueError();
} }
then_stack.unshift(done, fail); promise_stack.push(
return queue; promise_stack[promise_stack.length - 1].then(done, fail)
);
return this;
}; };
return queue; return this;
}; };
Queue.prototype = Object.create(Promise.prototype); Queue.prototype = Object.create(Promise.prototype);
......
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