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

jslint pass jobManager.js

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