2013-04-01 01:06:52 +00:00
|
|
|
var ASYNC_TASK_DEFAULT_TIMEOUT = 30000;
|
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
/**
|
|
|
|
* Used to run any asynchronous tasks sequentially (ajax mainly)
|
|
|
|
* An asynchronous task must be created with:
|
2013-04-01 01:06:52 +00:00
|
|
|
* - a required run() function that may call success(), error() or retry()
|
2013-03-31 15:33:28 +00:00
|
|
|
* - an optional onSuccess() function
|
|
|
|
* - an optional onError() function
|
2013-04-01 01:06:52 +00:00
|
|
|
* - an optional timeout field (default is 30000)
|
2013-03-30 11:56:17 +00:00
|
|
|
*/
|
|
|
|
var asyncTaskRunner = (function() {
|
|
|
|
var asyncTaskRunner = {};
|
|
|
|
|
|
|
|
var asyncTaskQueue = [];
|
|
|
|
var currentTask = undefined;
|
2013-04-01 01:06:52 +00:00
|
|
|
var currentTaskRunning = false;
|
|
|
|
var currentTaskStartTime = currentTime;
|
2013-03-30 11:56:17 +00:00
|
|
|
|
|
|
|
// Run the next task in the queue if any and no other is running
|
|
|
|
asyncTaskRunner.runTask = function() {
|
|
|
|
|
|
|
|
// If there is a task currently running
|
2013-04-01 23:12:28 +00:00
|
|
|
if(currentTaskRunning === true) {
|
2013-03-30 11:56:17 +00:00
|
|
|
// If the current task takes too long
|
2013-04-01 01:06:52 +00:00
|
|
|
var timeout = currentTask.timeout || ASYNC_TASK_DEFAULT_TIMEOUT;
|
2013-03-31 15:33:28 +00:00
|
|
|
if(currentTaskStartTime + timeout < currentTime) {
|
2013-03-30 11:56:17 +00:00
|
|
|
currentTask.error();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-03-31 15:33:28 +00:00
|
|
|
|
2013-04-01 01:06:52 +00:00
|
|
|
if(currentTask === undefined) {
|
|
|
|
// If no task in the queue
|
|
|
|
if(asyncTaskQueue.length === 0) {
|
2013-03-30 11:56:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-04-01 01:06:52 +00:00
|
|
|
|
|
|
|
// Dequeue an enqueued task
|
|
|
|
currentTask = asyncTaskQueue.shift();
|
|
|
|
currentTaskStartTime = currentTime;
|
|
|
|
showWorkingIndicator(true);
|
|
|
|
|
|
|
|
// Set task attributes and functions
|
|
|
|
currentTask.finished = false;
|
|
|
|
currentTask.retryCounter = 0;
|
|
|
|
currentTask.finish = function() {
|
|
|
|
this.finished = true;
|
|
|
|
currentTask = undefined;
|
|
|
|
currentTaskRunning = false;
|
2013-04-01 23:12:28 +00:00
|
|
|
if(asyncTaskQueue.length === 0) {
|
|
|
|
showWorkingIndicator(false);
|
2013-04-01 01:06:52 +00:00
|
|
|
}
|
2013-04-01 23:12:28 +00:00
|
|
|
else {
|
|
|
|
asyncTaskRunner.runTask();
|
2013-04-01 01:06:52 +00:00
|
|
|
}
|
|
|
|
};
|
2013-04-01 23:12:28 +00:00
|
|
|
currentTask.success = function() {
|
|
|
|
runSafe(this.onSuccess);
|
|
|
|
};
|
2013-04-01 01:06:52 +00:00
|
|
|
currentTask.error = function() {
|
2013-04-01 23:12:28 +00:00
|
|
|
runSafe(this.onError);
|
2013-04-01 01:06:52 +00:00
|
|
|
};
|
|
|
|
currentTask.retry = function() {
|
|
|
|
if(this.finished === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(currentTask.retryCounter === 5) {
|
|
|
|
this.error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Implement an exponential backoff
|
|
|
|
var delay = (Math.pow(2, currentTask.retryCounter++) + Math.random()) * 1000;
|
|
|
|
console.log(delay);
|
|
|
|
currentTaskStartTime = currentTime + delay;
|
|
|
|
currentTaskRunning = false;
|
|
|
|
asyncTaskRunner.runTask();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the task
|
|
|
|
if(currentTaskStartTime <= currentTime) {
|
|
|
|
currentTaskRunning = true;
|
|
|
|
currentTask.run();
|
|
|
|
}
|
2013-03-30 11:56:17 +00:00
|
|
|
};
|
|
|
|
|
2013-04-01 23:12:28 +00:00
|
|
|
function runSafe(func) {
|
|
|
|
if(currentTask.finished === true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if(func) {
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
currentTask.finished = true;
|
|
|
|
currentTask = undefined;
|
|
|
|
currentTaskRunning = false;
|
|
|
|
if(asyncTaskQueue.length === 0) {
|
|
|
|
showWorkingIndicator(false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
asyncTaskRunner.runTask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-30 11:56:17 +00:00
|
|
|
// Add a task in the queue
|
|
|
|
asyncTaskRunner.addTask = function(asyncTask) {
|
|
|
|
asyncTaskQueue.push(asyncTask);
|
|
|
|
};
|
|
|
|
|
|
|
|
return asyncTaskRunner;
|
|
|
|
})();
|
|
|
|
|