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 = new Promise(function(done, fail) { promise_stack.push(
fulfill = function (fulfillmentValue) { last_promise.then(detectQueueSuccess,
if (resolved) {return;} detectQueueError)
queue.isFulfilled = true; );
queue.fulfillmentValue = fulfillmentValue; return false;
resolved = true; }
return done(fulfillmentValue);
};
reject = function (rejectedReason) {
if (resolved) {return;}
queue.isRejected = true;
queue.rejectedReason = rejectedReason ;
resolved = true;
return fail(rejectedReason);
};
}, canceller);
promise_list.push(resolve()); detectQueueSuccess = function (fulfillmentValue) {
promise_list.push(promise_list[0].then(function () { if (handleQueue()) {
promise_list.splice(0, 2); return resolveQueue(fulfillmentValue);
if (promise_list.length === 0) {
fulfill();
} }
})); return fulfillmentValue;
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); detectQueueError = function (rejectedReason) {
if (handleQueue()) {
return rejectQueue(rejectedReason);
}
throw rejectedReason;
}; };
queue.push = function(done, fail) { // Resolve by default
var last_promise = promise_list[promise_list.length - 1], promise_stack.push(
next_promise; resolve().then(detectQueueSuccess, detectQueueError)
);
if (resolved) { }, function () {
throw new ResolvedQueueError(); // 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();
} }
next_promise = last_promise.then(done, fail); });
promise_list.push(next_promise);
// Handle pop queue.push = function (done, fail) {
promise_list.push(next_promise.then(function (fulfillmentValue) { if (queue.isFulfilled || queue.isRejected) {
promise_list.splice(0, 2); throw new ResolvedQueueError();
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;
} }
})); then_stack.unshift(done, fail);
return queue;
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