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