Commit 8d6e596b authored by Sven Franck's avatar Sven Franck

jslint pass jobManager.js

parent 20b49bfa
var jobManager = (function(spec) {
var that = {};
/*jslint indent: 2, maxlen: 80, sloppy: true */
/*global localstorage: true, setInterval: true, clearInterval: true,
command: true, job: true, jobRules: true */
var jobManager = (function (spec) {
var that = {},
job_array_name = 'jio/job_array',
priv = {};
spec = spec || {};
// Attributes //
var job_array_name = 'jio/job_array';
var priv = {};
priv.id = spec.id;
priv.interval_id = null;
priv.interval = 200;
......@@ -15,7 +19,7 @@ var jobManager = (function(spec) {
* @method getJobArrayName
* @return {string} The job array name
*/
priv.getJobArrayName = function() {
priv.getJobArrayName = function () {
return job_array_name + '/' + priv.id;
};
......@@ -24,20 +28,21 @@ var jobManager = (function(spec) {
* @method getJobArray
* @return {array} The job array.
*/
priv.getJobArray = function() {
return localstorage.getItem(priv.getJobArrayName())||[];
priv.getJobArray = function () {
return localstorage.getItem(priv.getJobArrayName()) || [];
};
/**
* Does a backup of the job array in the localStorage.
* @method copyJobArrayToLocal
*/
priv.copyJobArrayToLocal = function() {
var new_a = [], i;
for (i = 0; i < priv.job_array.length; i+= 1) {
priv.copyJobArrayToLocal = function () {
var new_a = [],
i;
for (i = 0; i < priv.job_array.length; i += 1) {
new_a.push(priv.job_array[i].serialized());
}
localstorage.setItem(priv.getJobArrayName(),new_a);
localstorage.setItem(priv.getJobArrayName(), new_a);
};
/**
......@@ -45,9 +50,10 @@ var jobManager = (function(spec) {
* @method removeJob
* @param {object} job The job object.
*/
priv.removeJob = function(job) {
var i, tmp_job_array = [];
for (i = 0; i < priv.job_array.length; i+= 1) {
priv.removeJob = function (job) {
var i,
tmp_job_array = [];
for (i = 0; i < priv.job_array.length; i += 1) {
if (priv.job_array[i] !== job) {
tmp_job_array.push(priv.job_array[i]);
}
......@@ -61,7 +67,7 @@ var jobManager = (function(spec) {
* @method setId
* @param {number} id The id.
*/
that.setId = function(id) {
that.setId = function (id) {
priv.id = id;
};
......@@ -69,15 +75,15 @@ var jobManager = (function(spec) {
* Starts listening to the job array, executing them regulary.
* @method start
*/
that.start = function() {
that.start = function () {
var i;
if (priv.interval_id === null) {
priv.interval_id = setInterval (function() {
priv.interval_id = setInterval(function () {
priv.restoreOldJio();
for (i = 0; i < priv.job_array.length; i+= 1) {
for (i = 0; i < priv.job_array.length; i += 1) {
that.execute(priv.job_array[i]);
}
},priv.interval);
}, priv.interval);
}
};
......@@ -85,7 +91,7 @@ var jobManager = (function(spec) {
* Stops listening to the job array.
* @method stop
*/
that.stop = function() {
that.stop = function () {
if (priv.interval_id !== null) {
clearInterval(priv.interval_id);
priv.interval_id = null;
......@@ -101,12 +107,15 @@ var jobManager = (function(spec) {
* and it will add them to this job array.
* @method restoreOldJio
*/
priv.restoreOldJio = function() {
var i, jio_id_a;
priv.restoreOldJio = function () {
var i,
jio_id_a;
priv.lastrestore = priv.lastrestore || 0;
if (priv.lastrestore > (Date.now()) - 2000) { return; }
jio_id_a = localstorage.getItem('jio/id_array')||[];
for (i = 0; i < jio_id_a.length; i+= 1) {
if (priv.lastrestore > (Date.now()) - 2000) {
return;
}
jio_id_a = localstorage.getItem('jio/id_array') || [];
for (i = 0; i < jio_id_a.length; i += 1) {
priv.restoreOldJioId(jio_id_a[i]);
}
priv.lastrestore = Date.now();
......@@ -117,9 +126,9 @@ var jobManager = (function(spec) {
* @method restoreOldJioId
* @param {number} id The jio id.
*/
priv.restoreOldJioId = function(id) {
priv.restoreOldJioId = function (id) {
var jio_date;
jio_date = localstorage.getItem('jio/id/'+id)||0;
jio_date = localstorage.getItem('jio/id/' + id) || 0;
if (new Date(jio_date).getTime() < (Date.now() - 10000)) { // 10 sec
priv.restoreOldJobFromJioId(id);
priv.removeOldJioId(id);
......@@ -132,99 +141,99 @@ var jobManager = (function(spec) {
* @method restoreOldJobFromJioId
* @param {number} id The jio id.
*/
priv.restoreOldJobFromJioId = function(id) {
var i, jio_job_array;
jio_job_array = localstorage.getItem('jio/job_array/'+id)||[];
for (i = 0; i < jio_job_array.length; i+= 1) {
var command_object = command(jio_job_array[i].command);
priv.restoreOldJobFromJioId = function (id) {
var i,
command_object,
jio_job_array;
jio_job_array = localstorage.getItem('jio/job_array/' + id) || [];
for (i = 0; i < jio_job_array.length; i += 1) {
command_object = command(jio_job_array[i].command);
if (command_object.canBeRestored()) {
that.addJob ( job(
{storage:that.storage(jio_job_array[i].storage),
command:command_object}));
that.addJob(job({
storage: that.storage(jio_job_array[i].storage),
command: command_object
}));
}
}
};
/**
* Removes a jio instance according to an id.
* @method removeOldJioId
* @param {number} id The jio id.
*/
priv.removeOldJioId = function(id) {
var i, jio_id_array, new_array = [];
jio_id_array = localstorage.getItem('jio/id_array')||[];
for (i = 0; i < jio_id_array.length; i+= 1) {
priv.removeOldJioId = function (id) {
var i,
jio_id_array,
new_array = [];
jio_id_array = localstorage.getItem('jio/id_array') || [];
for (i = 0; i < jio_id_array.length; i += 1) {
if (jio_id_array[i] !== id) {
new_array.push(jio_id_array[i]);
}
}
localstorage.setItem('jio/id_array',new_array);
localstorage.removeItem('jio/id/'+id);
localstorage.setItem('jio/id_array', new_array);
localstorage.removeItem('jio/id/' + id);
};
/**
* Removes a job array from a jio instance according to an id.
* @method removeJobArrayFromJioId
* @param {number} id The jio id.
*/
priv.removeJobArrayFromJioId = function(id) {
localstorage.removeItem('jio/job_array/'+id);
priv.removeJobArrayFromJioId = function (id) {
localstorage.removeItem('jio/job_array/' + id);
};
/**
* Executes a job.
* @method execute
* @param {object} job The job object.
*/
that.execute = function(job) {
that.execute = function (job) {
try {
job.execute();
} catch (e) {
switch (e.name) {
case 'jobNotReadyException': break; // do nothing
case 'tooMuchTriesJobException': break; // do nothing
default: throw e;
case 'jobNotReadyException':
break; // do nothing
case 'tooMuchTriesJobException':
break; // do nothing
default:
throw e;
}
}
priv.copyJobArrayToLocal();
};
/**
* Checks if a job exists in the job array according to a job id.
* @method jobIdExists
* @param {number} id The job id.
* @return {boolean} true if exists, else false.
*/
that.jobIdExists = function(id) {
that.jobIdExists = function (id) {
var i;
for (i = 0; i < priv.job_array.length; i+= 1) {
for (i = 0; i < priv.job_array.length; i += 1) {
if (priv.job_array[i].getId() === id) {
return true;
}
}
return false;
};
/**
* Terminate a job. It only remove it from the job array.
* @method terminateJob
* @param {object} job The job object
*/
that.terminateJob = function(job) {
that.terminateJob = function (job) {
priv.removeJob(job);
};
/**
* Adds a job to the current job array.
* @method addJob
* @param {object} job The new job.
*/
that.addJob = function(job) {
var result_array =
that.validateJobAccordingToJobList (priv.job_array,job);
priv.appendJob (job,result_array);
that.addJob = function (job) {
var result_array = that.validateJobAccordingToJobList(priv.job_array, job);
priv.appendJob(job, result_array);
};
/**
* Generate a result array containing action string to do with the good job.
* @method validateJobAccordingToJobList
......@@ -232,14 +241,14 @@ var jobManager = (function(spec) {
* @param {object} job The new job to compare with.
* @return {array} A result array.
*/
that.validateJobAccordingToJobList = function(job_array,job) {
var i, result_array = [];
for (i = 0; i < job_array.length; i+= 1) {
result_array.push(jobRules.validateJobAccordingToJob (job_array[i],job));
that.validateJobAccordingToJobList = function (job_array, job) {
var i,
result_array = [];
for (i = 0; i < job_array.length; i += 1) {
result_array.push(jobRules.validateJobAccordingToJob(job_array[i], job));
}
return result_array;
};
/**
* It will manage the job in order to know what to do thanks to a result
* array. The new job can be added to the job array, but it can also be
......@@ -249,17 +258,17 @@ var jobManager = (function(spec) {
* @param {object} job The job to append.
* @param {array} result_array The result array.
*/
priv.appendJob = function(job,result_array) {
priv.appendJob = function (job, result_array) {
var i;
if (priv.job_array.length !== result_array.length) {
throw new RangeError("Array out of bound");
}
for (i = 0; i < result_array.length; i+= 1) {
for (i = 0; i < result_array.length; i += 1) {
if (result_array[i].action === 'dont accept') {
return job.notAccepted();
}
}
for (i = 0; i < result_array.length; i+= 1) {
for (i = 0; i < result_array.length; i += 1) {
switch (result_array[i].action) {
case 'eliminate':
result_array[i].job.eliminated();
......@@ -272,20 +281,21 @@ var jobManager = (function(spec) {
case 'wait':
job.waitForJob(result_array[i].job);
break;
default: break;
default:
break;
}
}
priv.job_array.push(job);
priv.copyJobArrayToLocal();
};
that.serialized = function () {
var a = [], i, job_array = priv.job_array || [];
for (i = 0; i < job_array.length; i+= 1) {
var a = [],
i,
job_array = priv.job_array || [];
for (i = 0; i < job_array.length; i += 1) {
a.push(job_array[i].serialized());
}
return a;
};
return that;
}());
\ No newline at end of file
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