Commit e410c7ce authored by Romain Courteaud's avatar Romain Courteaud 🐙

Try to speed up queue

Reduce number of .then call.
But memory usage increases :/
parent a95309fe
...@@ -121,7 +121,7 @@ Promise.prototype = { ...@@ -121,7 +121,7 @@ Promise.prototype = {
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', {});
}); });
......
...@@ -12,92 +12,80 @@ function ResolvedQueueError(message) { ...@@ -12,92 +12,80 @@ function ResolvedQueueError(message) {
ResolvedQueueError.prototype = new Error(); ResolvedQueueError.prototype = new Error();
ResolvedQueueError.prototype.constructor = ResolvedQueueError; ResolvedQueueError.prototype.constructor = ResolvedQueueError;
var Queue = function() { var last_promise,
var queue = this, i;
promise_list = [],
promise,
fulfill,
reject,
resolved;
var Queue = function() {
if (!(this instanceof Queue)) { if (!(this instanceof Queue)) {
return new Queue(); return new Queue();
} }
function canceller() { var queue = this,
for (var i = 0; i < 2; i++) { promise_stack = [],
promise_list[i].cancel(); then_stack = [];
Promise.call(queue, function (resolveQueue, rejectQueue) {
var detectQueueSuccess,
detectQueueError;
function handleQueue() {
if (then_stack.length === 0) {
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.push(
last_promise.then(detectQueueSuccess,
detectQueueError)
);
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, detectQueueError)
if (promise_list.length === 0) { );
fulfill();
}, 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.cancel = function () {
if (resolved) {return;}
resolved = true;
promise.cancel();
promise.fail(function (rejectedReason) {
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) { queue.push = function (done, fail) {
if (queue.isFulfilled || queue.isRejected) {
throw new ResolvedQueueError(); throw new ResolvedQueueError();
} }
then_stack.unshift(done, fail);
next_promise = last_promise.then(done, fail); return queue;
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 queue;
}; };
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