diff --git a/src/jio/core/JobQueue.js b/src/jio/core/JobQueue.js index f3f43e48eac664001e4f15ef6882518ca8696474..451ae57956cba246fd19c755e2f450a1173de52a 100644 --- a/src/jio/core/JobQueue.js +++ b/src/jio/core/JobQueue.js @@ -1,5 +1,5 @@ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ -/*global deepClone */ +/*global deepClone, dictFilter, uniqueJSONStringify */ /** * Tool to manipulate a list of object containing at least one property: 'id'. @@ -7,12 +7,72 @@ * * @class JobQueue * @constructor - * @param {Array} An array of object + * @param {Workspace} workspace The workspace where to store + * @param {String} namespace The namespace to use in the workspace + * @param {Array} job_keys An array of job keys to store + * @param {Array} [array] An array of object */ -function JobQueue(array) { - this._array = array; +function JobQueue(workspace, namespace, job_keys, array) { + this._workspace = workspace; + this._namespace = namespace; + this._job_keys = job_keys; + if (Array.isArray(array)) { + this._array = array; + } else { + this._array = []; + } } +/** + * Store the job queue into the workspace. + * + * @method save + */ +JobQueue.prototype.save = function () { + var i, job_queue = deepClone(this._array); + for (i = 0; i < job_queue.length; i += 1) { + dictFilter(job_queue[i], this._job_keys); + } + if (this._array.length === 0) { + this._workspace.removeItem(this._namespace); + } else { + this._workspace.setItem( + this._namespace, + uniqueJSONStringify(job_queue) + ); + } + return this; +}; + +/** + * Loads the job queue from the workspace. + * + * @method load + */ +JobQueue.prototype.load = function () { + var job_list; + try { + job_list = JSON.parse(this._workspace.getItem(this._namespace)); + } catch (ignore) {} + if (!Array.isArray(job_list)) { + job_list = []; + } + this.clear(); + new JobQueue(job_list).repair(); + this.update(job_list); + return this; +}; + +/** + * Returns the array version of the job queue + * + * @method asArray + * @return {Array} The job queue as array + */ +JobQueue.prototype.asArray = function () { + return this._array; +}; + /** * Removes elements which are not objects containing at least 'id' property. * diff --git a/src/jio/core/JobWorkspace.js b/src/jio/core/JobWorkspace.js deleted file mode 100644 index 126145be572e769d689080b2eb5fe447bf2e4ab5..0000000000000000000000000000000000000000 --- a/src/jio/core/JobWorkspace.js +++ /dev/null @@ -1,111 +0,0 @@ -/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ -/*global JobQueue, uniqueJSONStringify, deepClone, dictFilter */ - -/** - * Manipulates a job queue and is also able to store it in a specific place. - * - * @class JobWorkspace - * @constructor - * @param {Workspace} workspace The workspace where to store - * @param {JobQueue} job_queue The job queue to use - * @param {String} namespace The namespace to use in the workspace - * @param {Array} job_keys An array of job keys to store - */ -function JobWorkspace(workspace, job_queue, namespace, job_keys) { - this._workspace = workspace; - this._job_queue = job_queue; - this._namespace = namespace; - this._job_keys = job_keys; -} - -/** - * Store the job queue into the workspace. - * - * @method save - */ -JobWorkspace.prototype.save = function () { - var i, job_queue = deepClone(this._job_queue._array); - for (i = 0; i < job_queue.length; i += 1) { - dictFilter(job_queue[i], this._job_keys); - } - if (this._job_queue._array.length === 0) { - this._workspace.removeItem(this._namespace); - } else { - this._workspace.setItem( - this._namespace, - uniqueJSONStringify(job_queue) - ); - } - return this; -}; - -/** - * Loads the job queue from the workspace. - * - * @method load - */ -JobWorkspace.prototype.load = function () { - var job_list; - try { - job_list = JSON.parse(this._workspace.getItem(this._namespace)); - } catch (ignore) {} - if (!Array.isArray(job_list)) { - job_list = []; - } - this._job_queue.clear(); - new JobQueue(job_list).repair(); - this._job_queue.update(job_list); - return this; -}; - -/** - * Returns the array version of the job queue - * - * @method asArray - * @return {Array} The job queue as array - */ -JobWorkspace.prototype.asArray = function () { - return this._job_queue._array; -}; - -/** - * Post a job in the job queue - * - * @method post - * @param {Object} job The job object - * @return {Number} The generated id - */ -JobWorkspace.prototype.post = function (job) { - return this._job_queue.post(job); -}; - -/** - * Put a job to the job queue - * - * @method put - * @param {Object} job The job object with an id - */ -JobWorkspace.prototype.put = function (job) { - return this._job_queue.put(job); -}; - -/** - * Get a job from an id. Returns undefined if not found - * - * @method get - * @param {Number} id The job id - * @return {Object} The job or undefined - */ -JobWorkspace.prototype.get = function (id) { - return this._job_queue.get(id); -}; - -/** - * Removes a job from an id - * - * @method remove - * @param {Number} id The job id - */ -JobWorkspace.prototype.remove = function (id) { - return this._job_queue.remove(id); -}; diff --git a/src/jio/features/jobQueue.js b/src/jio/features/jobQueue.js index d711f2037973a8279ece68f2cb8338449df37e4b..a372d01383936a9b39adf390d2c50e05816c1a17 100644 --- a/src/jio/features/jobQueue.js +++ b/src/jio/features/jobQueue.js @@ -1,6 +1,6 @@ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true, unparam: true */ /*global arrayExtend, localStorage, Workspace, uniqueJSONStringify, JobQueue, - JobWorkspace, constants */ + constants */ function enableJobQueue(jio, shared, options) { @@ -14,7 +14,7 @@ function enableJobQueue(jio, shared, options) { // creates // - shared.storage_spec_str String // - shared.workspace Workspace - // - shared.job_queue JobWorkspace + // - shared.job_queue JobQueue // uses 'job', 'jobRun', 'jobStop', 'jobEnd' events // emits 'jobEnd' events @@ -33,9 +33,8 @@ function enableJobQueue(jio, shared, options) { shared.storage_spec_str = uniqueJSONStringify(shared.storage_spec); } - shared.job_queue = new JobWorkspace( + shared.job_queue = new JobQueue( shared.workspace, - new JobQueue([]), 'jio/jobs/' + shared.storage_spec_str, shared.job_keys ); diff --git a/src/jio/features/jobRecovery.js b/src/jio/features/jobRecovery.js index 1d42588e432ad401a66a3da6bd6b3b845651cca6..364fe78168cf11a6c3eacd6d222defa0ce028157 100644 --- a/src/jio/features/jobRecovery.js +++ b/src/jio/features/jobRecovery.js @@ -7,7 +7,7 @@ function enableJobRecovery(jio, shared, options) { // - JobQueue enabled and before this // uses - // - shared.job_queue JobWorkspace + // - shared.job_queue JobQueue function numberOrDefault(number, default_value) { return (typeof number === 'number' &&