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

Reduce number of array

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