Commit 8dcbf352 authored by Romain Courteaud's avatar Romain Courteaud

Experiment other implementations.

Not working with listbox
parent 974b8596
......@@ -17,66 +17,93 @@ var Queue = function() {
return new Queue();
}
var promise_stack = [],
// handleQueue
detect_end_index = 1;
var function_stack = [],
resolveQueue,
rejectQueue,
queue = this,
current_promise = null,
handleDone,
handleReject,
next_function;
Promise.call(this, function (resolveQueue, rejectQueue) {
var detectQueueSuccess,
detectQueueError;
function handleQueue() {
if (promise_stack.length === detect_end_index) {
return true;
handleDone = function(result) {
if (queue.isRejected) {
return;
}
if (function_stack.length > 0) {
next_function = function_stack.shift();
// Remove fail handler
function_stack.shift();
if (next_function === undefined) {
return handleDone(result);
}
promise_stack.splice(0, detect_end_index);
promise_stack.push(
promise_stack[promise_stack.length - 1].then(detectQueueSuccess,
detectQueueError)
);
detect_end_index = promise_stack.length;
return false;
try {
current_promise = next_function.call(null, result);
} catch (e) {
return handleReject(e);
}
if ((current_promise !== undefined) &&
(typeof current_promise.then === "function")) {
current_promise.then(handleDone, handleReject);
} else {
handleDone(current_promise);
}
return;
}
resolveQueue(result);
};
detectQueueSuccess = function (fulfillmentValue) {
if (handleQueue()) {
return resolveQueue(fulfillmentValue);
handleReject = function(error) {
if (queue.isRejected) {
return;
}
if (function_stack.length > 0) {
// Remove done handler
function_stack.shift();
next_function = function_stack.shift();
if (next_function === undefined) {
return handleReject(error);
}
return fulfillmentValue;
};
detectQueueError = function (rejectedReason) {
if (handleQueue()) {
return rejectQueue(rejectedReason);
try {
current_promise = next_function.call(null, error);
} catch (e) {
return handleReject(e);
}
throw rejectedReason;
};
// Resolve by default
promise_stack.push(
resolve().then(detectQueueSuccess)
);
}, function () {
// Cancel all created promises
var i;
for (i = 0; i < promise_stack.length; i += 1) {
promise_stack[i].cancel();
if ((current_promise !== undefined) &&
(typeof current_promise.then === "function")) {
current_promise.then(handleDone, handleReject);
} else {
handleDone(current_promise);
}
return;
}
});
rejectQueue(error);
};
this.push = function (done, fail) {
if (this.isFulfilled || this.isRejected) {
if (this.isFulfilled || this.isRejected || this.isCancelled) {
throw new ResolvedQueueError();
}
promise_stack.push(
promise_stack[promise_stack.length - 1].then(done, fail)
);
function_stack.push(done, fail);
return this;
};
Promise.call(this, function (done, fail) {
resolveQueue = done;
rejectQueue = fail;
// Resolve by default
function_stack.push(resolve, undefined);
handleDone();
}, function () {
// Skip not executed .push
function_stack = [];
// Cancel currently running promise
if ((current_promise !== undefined) &&
(typeof current_promise.cancel === "function")) {
current_promise.cancel();
}
});
return this;
};
......
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