Commit d1a8148c authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Sebastien Robin

Fix bug: check saveDocument dependencies.

parent 3c4f9683
...@@ -928,6 +928,17 @@ var JIO = ...@@ -928,6 +928,17 @@ var JIO =
var that = {}, priv = {}; var that = {}, priv = {};
priv.wrongParametersError = function (settings) {
var m = 'Method: '+ settings.method +
', One or some parameters are undefined.';
console.error (m);
settings.callback({status:'fail',
error: {status:0,
statusText:'Undefined Parameter',
message: m}});
return null;
};
//// Getters Setters //// Getters Setters
that.getID = function () { that.getID = function () {
return priv.id; return priv.id;
...@@ -1039,7 +1050,7 @@ var JIO = ...@@ -1039,7 +1050,7 @@ var JIO =
settings.storage && settings.applicant) { settings.storage && settings.applicant) {
return priv.queue.createJob ( settings ); return priv.queue.createJob ( settings );
} }
return null; return priv.wrongParametersError(settings);
}; };
that.saveDocument = function ( options ) { that.saveDocument = function ( options ) {
...@@ -1063,15 +1074,16 @@ var JIO = ...@@ -1063,15 +1074,16 @@ var JIO =
var settings = extend({ var settings = extend({
'storage': priv.storage, 'storage': priv.storage,
'applicant': priv.applicant, 'applicant': priv.applicant,
'fileContent': '',
'method':'saveDocument', 'method':'saveDocument',
'callback': function () {} 'callback': function () {}
},options); },options);
// check dependencies // check dependencies
if (that.isReady() && settings.fileName && settings.fileContent && if (that.isReady() && settings.fileName &&
settings.storage && settings.applicant) { settings.storage && settings.applicant) {
return priv.queue.createJob ( settings ); return priv.queue.createJob ( settings );
} }
return null; return priv.wrongParametersError(settings);
}; };
that.loadDocument = function ( options ) { that.loadDocument = function ( options ) {
...@@ -1108,7 +1120,7 @@ var JIO = ...@@ -1108,7 +1120,7 @@ var JIO =
settings.storage && settings.applicant) { settings.storage && settings.applicant) {
return priv.queue.createJob ( settings ); return priv.queue.createJob ( settings );
} }
return null; return priv.wrongParametersError(settings);
}; };
that.getDocumentList = function ( options ) { that.getDocumentList = function ( options ) {
...@@ -1141,7 +1153,7 @@ var JIO = ...@@ -1141,7 +1153,7 @@ var JIO =
if (that.isReady() && settings.storage && settings.applicant ) { if (that.isReady() && settings.storage && settings.applicant ) {
return priv.queue.createJob( settings ); return priv.queue.createJob( settings );
} }
return null; return priv.wrongParametersError(settings);
}; };
that.removeDocument = function ( options ) { that.removeDocument = function ( options ) {
...@@ -1171,7 +1183,7 @@ var JIO = ...@@ -1171,7 +1183,7 @@ var JIO =
settings.storage && settings.applicant ) { settings.storage && settings.applicant ) {
return priv.queue.createJob ( settings ); return priv.queue.createJob ( settings );
} }
return null; return priv.wrongParametersError(settings);
}; };
//// end Methods //// end Methods
......
/*! JIO - v0.1.0 - 2012-05-24 /*! JIO - v0.1.0 - 2012-05-24
* Copyright (c) 2012 Nexedi; Licensed */ * Copyright (c) 2012 Nexedi; Licensed */
var JIO=function(){var a=function(a,b){var c={job_method_object:{checkNameAvailability:{},saveDocument:{},loadDocument:{},getDocumentList:{},removeDocument:{}}},d={job_managing_method:{canSelect:function(a,b){return JSON.stringify(a.storage)===JSON.stringify(b.storage)&&JSON.stringify(a.applicant)===JSON.stringify(b.applicant)&&a.fileName===b.fileName?!0:!1},canRemoveFailOrDone:function(a,b){return a.status==="fail"||a.status==="done"?!0:!1},canEliminate:function(a,b){return a.status!=="ongoing"&&(a.method==="removeDocument"&&b.method==="saveDocument"||a.method==="saveDocument"&&b.method==="removeDocument")?!0:!1},canReplace:function(a,b){return a.status!=="ongoing"&&a.method===b.method&&a.date<b.date?!0:!1},cannotAccept:function(a,b){if(a.status!=="ongoing"){if(a.method==="removeDocument"&&b.method==="loadDocument")return!0}else{if(a.method===b.method==="loadDocument")return!0;if(a.method==="removeDocument"&&(b.method==="loadDocument"||b.method==="removeDocument"))return!0;if(a.method===b.method==="saveDocument"&&a.fileContent===b.fileContent)return!0;if(a.method===b.method==="getDocumentList"||a.method===b.method==="checkNameAvailability")return!0}return!1},mustWait:function(a,b){return a.method==="getDocumentList"||a.method==="checkNameAvailability"||b.method==="getDocumentList"||b.method==="checkNameAvailability"?!1:!0}},queue_id:1,storage_type_object:{},max_wait_time:1e4},e=function(a,b){var c;for(c in b)a[c]=b[c];return a},f,g,h,i,j,k,l,m;return f=function(a,c){var d={},e={},f={},g,h;return e.eventAction=function(a){return h=a&&f[a],h||(g=b.Callbacks(),h={publish:g.fire,subscribe:g.add,unsubscribe:g.remove},a&&(f[a]=h)),h},d.publish=function(a,b){e.eventAction(a).publish(b)},d.subscribe=function(a,b){return e.eventAction(a).subscribe(b),b},d.unsubscribe=function(a,b){e.eventAction(a).unsubscribe(b)},d},g=function(a,b){var c=e({},a);return c.id=0,c.status="initial",c.date=Date.now(),c},h=function(b,f){var h={},i={},j="jio/idarray";return h.init=function(c){var d,e=function(){},f;i.use_local_storage&&(f=a.getItem(j)||[],b.publisher&&(i.publisher=b.publisher),i.jio_id=c.jio_id,i.job_object_name="jio/jobobject/"+i.jio_id,f.push(i.jio_id),a.setItem(j,f)),i.job_object={},h.copyJobQueueToLocalStorage();for(d in i.recovered_job_object)i.recovered_job_object[d].callback=e,h.addJob(i.recovered_job_object[d])},h.close=function(){JSON.stringify(i.job_object)==="{}"&&a.deleteItem(i.job_object_name)},h.getNewQueueID=function(){var b=null,c=0,e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)e[b]>=d.queue_id&&(d.queue_id=e[b]+1);return c=d.queue_id,d.queue_id++,c},h.recoverOlderJobObject=function(){var b=null,c=[],d=!1,e;if(i.use_local_storage){e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)a.getItem("jio/id/"+e[b])<Date.now()-1e4?(a.deleteItem("jio/id/"+e[b]),i.recovered_job_object=a.getItem("jio/jobobject/"+e[b]),a.deleteItem("jio/jobobject/"+e[b]),d=!0):c.push(e[b]);d&&a.setItem(j,c)}},h.isThereJobsWhere=function(a){var b="id";if(!a)return!0;for(b in i.job_object)if(a(i.job_object[b]))return!0;return!1},h.copyJobQueueToLocalStorage=function(){return i.use_local_storage?a.setItem(i.job_object_name,i.job_object):!1},h.createJob=function(a){return h.addJob(g(a))},h.addJob=function(a){var b=!0,c=[],e=[],f=[],g=null,j="id";for(j in i.job_object){if(d.job_managing_method.canRemoveFailOrDone(i.job_object[j],a)){f.push(j);continue}if(d.job_managing_method.canSelect(i.job_object[j],a)){if(d.job_managing_method.canEliminate(i.job_object[j],a)){c.push(j);continue}if(d.job_managing_method.canReplace(i.job_object[j],a)){g=k({queue:h,job:i.job_object[j]}),g.replace(a),b=!1;break}if(d.job_managing_method.cannotAccept(i.job_object[j],a))return!1;if(d.job_managing_method.mustWait(i.job_object[j],a)){e.push(j);continue}}}if(b){for(j=0;j<c.length;j+=1)g=k({queue:h,job:i.job_object[c[j]]}),g.eliminate();if(e.length>0){a.status="wait",a.waitingFor={jobIdArray:e};for(j=0;j<e.length;j+=1)i.job_object[e[j]]&&(i.job_object[e[j]].maxtries=1)}for(j=0;j<f.length;j+=1)h.removeJob(i.job_object[f[j]]);a.id=i.job_id,a.tries=0,i.job_id++,i.job_object[a.id]=a}return h.copyJobQueueToLocalStorage(),!0},h.removeJob=function(a){var b=e({where:function(a){return!0}},a),c,d=!1,f="key";if(b.job)i.job_object[b.job.id]&&b.where(i.job_object[b.job.id])&&(delete i.job_object[b.job.id],d=!0);else for(f in i.job_object)b.where(i.job_object[f])&&(delete i.job_object[f],d=!0);d||console.error("No jobs was found, when trying to remove some."),h.copyJobQueueToLocalStorage()},h.resetAll=function(){var a="id";for(a in i.job_object)i.job_object[a].status="initial";h.copyJobQueueToLocalStorage()},h.invokeAll=function(){var a="id",b,c;for(a in i.job_object){c=!1;if(i.job_object[a].status==="initial")h.invoke(i.job_object[a]);else if(i.job_object[a].status==="wait"){c=!0;if(i.job_object[a].waitingFor.jobIdArray)for(b=0;b<i.job_object[a].waitingFor.jobIdArray.length;b+=1)if(i.job_object[i.job_object[a].waitingFor.jobIdArray[b]]){c=!1;break}i.job_object[a].waitingFor.time&&i.job_object[a].waitingFor.time>Date.now()&&(c=!1),c&&h.invoke(i.job_object[a])}}this.copyJobQueueToLocalStorage()},h.invoke=function(a){var b;if(!c.job_method_object[a.method])return!1;h.isThereJobsWhere(function(b){return b.method===a.method&&b.method==="initial"})?a.status="ongoing":(a.status="ongoing",i.publisher.publish(c.job_method_object[a.method]["start_"+a.method])),b=k({queue:this,job:a}),b.execute()},h.ended=function(a){var b=e({},a);h.removeJob({job:b});if(!c.job_method_object[b.method])return!1;if(!h.isThereJobsWhere(function(a){return a.method===b.method&&a.status==="ongoing"||a.status==="initial"})){i.publisher.publish(c.job_method_object[b.method]["stop_"+b.method]);return}},h.clean=function(){h.removeJob(undefined,{where:function(a){return a.status==="fail"}})},i.use_local_storage=b.options.use_local_storage,i.publisher=b.publisher,i.job_id=1,i.jio_id=0,i.job_object_name="",i.job_object={},i.recovered_job_object={},h},i=function(a,b){var c={},d={};return d.interval=200,d.id=null,d.queue=a.queue,c.setIntervalDelay=function(a){d.interval=a},c.start=function(){return d.id?!1:(d.id=setInterval(function(){d.queue.recoverOlderJobObject(),d.queue.invokeAll()},d.interval),!0)},c.stop=function(){return d.id?(clearInterval(d.id),d.id=null,!0):!1},c},j=function(){var b={},c={};return c.interval=400,c.id=null,b.start=function(a){return c.id?!1:(b.touch(a),c.id=setInterval(function(){b.touch(a)},c.interval),!0)},b.stop=function(){return c.id?(clearInterval(c.id),c.id=null,!0):!1},b.touch=function(b){a.setItem("jio/id/"+b,Date.now())},b},k=function(a){var b={},c={};return c.job=a.job,c.callback=a.job.callback,c.queue=a.queue,c.res={status:"done",message:""},c.fail_checkNameAvailability=function(){c.res.message="Unable to check name availability."},c.done_checkNameAvailability=function(a){c.res.message=c.job.userName+" is "+(a?"":"not ")+"available.",c.res.return_value=a},c.fail_loadDocument=function(){c.res.message="Unable to load document."},c.done_loadDocument=function(a){c.res.message="Document loaded.",c.res.return_value=a,c.res.return_value.lastModified=(new Date(c.res.return_value.lastModified)).getTime(),c.res.return_value.creationDate=(new Date(c.res.return_value.creationDate)).getTime()},c.fail_saveDocument=function(){c.res.message="Unable to save document."},c.done_saveDocument=function(){c.res.message="Document saved."},c.fail_getDocumentList=function(){c.res.message="Unable to retrieve document list."},c.done_getDocumentList=function(a){var b;c.res.message="Document list received.",c.res.return_value=a;for(b=0;b<c.res.return_value.length;b+=1)c.res.return_value[b].lastModified=(new Date(c.res.return_value[b].lastModified)).getTime(),c.res.return_value[b].creationDate=(new Date(c.res.return_value[b].creationDate)).getTime()},c.fail_removeDocument=function(){c.res.message="Unable to removed document."},c.done_removeDocument=function(){c.res.message="Document removed."},c.retryLater=function(){var a=c.job.tries*c.job.tries*1e3;a>d.max_wait_time&&(a=d.max_wait_time),c.job.status="wait",c.job.waitingFor={time:Date.now()+a}},b.cloneJob=function(){return e({},c.job)},b.getUserName=function(){return c.job.userName||""},b.getApplicantID=function(){return c.job.applicant.ID||""},b.getStorageUserName=function(){return c.job.storage.userName||""},b.getStoragePassword=function(){return c.job.storage.password||""},b.getStorageLocation=function(){return c.job.storage.location||""},b.getStorageArray=function(){return c.job.storage.storageArray||[]},b.getFileName=function(){return c.job.fileName||""},b.getFileContent=function(){return c.job.fileContent||""},b.cloneOptionObject=function(){return e({},c.job.options)},b.getMaxTries=function(){return c.job.maxtries},b.getTries=function(){return c.job.tries||0},b.setMaxTries=function(a){c.job.maxtries=a},b.addJob=function(a){return c.queue.createJob(a)},b.eliminate=function(){c.job.maxtries=1,c.job.tries=1,b.fail("Job Stopped!",0)},b.replace=function(a){c.job.tries=0,c.job.date=a.date,c.job.callback=a.callback,c.res.status="fail",c.res.message="Job Stopped!",c.res.error={},c.res.error.status=0,c.res.error.statusText="Replaced",c.res.error.message="The job was replaced by a newer one.",c["fail_"+c.job.method](),c.callback(c.res)},b.fail=function(a){c.res.status="fail",c.res.error=a,c.res.error.status=c.res.error.status||0,c.res.error.statusText=c.res.error.statusText||"Unknown Error",c.res.error.array=c.res.error.array||[],c.res.error.message=c.res.error.message||"",!c.job.maxtries||c.job.tries<c.job.maxtries?c.retryLater():(c.job.status="fail",c["fail_"+c.job.method](),c.queue.ended(c.job),c.callback(c.res))},b.done=function(a){c.job.status="done",c["done_"+c.job.method](a),c.queue.ended(c.job),c.callback(c.res)},b.execute=function(){return c.job.tries=b.getTries()+1,d.storage_type_object[c.job.storage.type]?d.storage_type_object[c.job.storage.type]({job:c.job,queue:c.queue})[c.job.method]():null},b.checkNameAvailability=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.loadDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.saveDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.getDocumentList=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.removeDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b},l=function(a,b){var c={},g={};c.getID=function(){return g.id},c.start=function(){return g.id!==0?!1:(g.id=g.queue.getNewQueueID(),g.queue.init({jio_id:g.id}),g.updater&&g.updater.start(g.id),g.listener.start(),g.ready=!0,c.isReady())},c.stop=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,g.id=0,!0},c.kill=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,!0},c.isReady=function(){return g.ready},c.publish=function(a,b){if(!c.isReady())return;return g.pubsub.publish(a,b)},c.subscribe=function(a,b){return g.pubsub.subscribe(a,b)},c.unsubscribe=function(a,b){return g.pubsub.unsubscribe(a,b)},c.checkNameAvailability=function(a){var b=e({userName:g.storage.userName,storage:g.storage,applicant:g.applicant,method:"checkNameAvailability",callback:function(){}},a);return c.isReady()&&b.userName&&b.storage&&b.applicant?g.queue.createJob(b):null},c.saveDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"saveDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.fileContent&&b.storage&&b.applicant?g.queue.createJob(b):null},c.loadDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"loadDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):null},c.getDocumentList=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"getDocumentList",callback:function(){}},a);return c.isReady()&&b.storage&&b.applicant?g.queue.createJob(b):null},c.removeDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"removeDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):null};var k=e({use_local_storage:!0},a.options);return typeof a.storage=="string"&&(a.storage=JSON.parse(a.storage)),typeof a.applicant=="string"&&(a.applicant=JSON.parse(a.applicant)),g.storage=a.storage,g.applicant=a.applicant,g.id=0,g.pubsub=f({options:k}),g.queue=h({publisher:g.pubsub,options:k}),g.listener=i({queue:g.queue,options:k}),g.ready=!1,k.use_local_storage?g.updater=j({options:k}):g.updater=null,g.storage&&!d.storage_type_object[g.storage.type]&&console.error('Unknown storage type "'+g.storage.type+'"'),c.start(),c},m=function(a,b){var f={};return f.createNew=function(a,b,c){var d=e({use_local_storage:!0},c);return l({storage:a,applicant:b,options:d})},f.newBaseStorage=function(a,b){return k(a,b)},f.addStorageType=function(a,b){return a&&b?(d.storage_type_object[a]=b,!0):!1},f.getGlobalObject=function(){return d},f.getConstObject=function(){return e({},c)},f},m()};return window.requirejs?(define("JIO",["LocalOrCookieStorage","jQuery"],a),undefined):a(LocalOrCookieStorage,jQuery)}(); var JIO=function(){var a=function(a,b){var c={job_method_object:{checkNameAvailability:{},saveDocument:{},loadDocument:{},getDocumentList:{},removeDocument:{}}},d={job_managing_method:{canSelect:function(a,b){return JSON.stringify(a.storage)===JSON.stringify(b.storage)&&JSON.stringify(a.applicant)===JSON.stringify(b.applicant)&&a.fileName===b.fileName?!0:!1},canRemoveFailOrDone:function(a,b){return a.status==="fail"||a.status==="done"?!0:!1},canEliminate:function(a,b){return a.status!=="ongoing"&&(a.method==="removeDocument"&&b.method==="saveDocument"||a.method==="saveDocument"&&b.method==="removeDocument")?!0:!1},canReplace:function(a,b){return a.status!=="ongoing"&&a.method===b.method&&a.date<b.date?!0:!1},cannotAccept:function(a,b){if(a.status!=="ongoing"){if(a.method==="removeDocument"&&b.method==="loadDocument")return!0}else{if(a.method===b.method==="loadDocument")return!0;if(a.method==="removeDocument"&&(b.method==="loadDocument"||b.method==="removeDocument"))return!0;if(a.method===b.method==="saveDocument"&&a.fileContent===b.fileContent)return!0;if(a.method===b.method==="getDocumentList"||a.method===b.method==="checkNameAvailability")return!0}return!1},mustWait:function(a,b){return a.method==="getDocumentList"||a.method==="checkNameAvailability"||b.method==="getDocumentList"||b.method==="checkNameAvailability"?!1:!0}},queue_id:1,storage_type_object:{},max_wait_time:1e4},e=function(a,b){var c;for(c in b)a[c]=b[c];return a},f,g,h,i,j,k,l,m;return f=function(a,c){var d={},e={},f={},g,h;return e.eventAction=function(a){return h=a&&f[a],h||(g=b.Callbacks(),h={publish:g.fire,subscribe:g.add,unsubscribe:g.remove},a&&(f[a]=h)),h},d.publish=function(a,b){e.eventAction(a).publish(b)},d.subscribe=function(a,b){return e.eventAction(a).subscribe(b),b},d.unsubscribe=function(a,b){e.eventAction(a).unsubscribe(b)},d},g=function(a,b){var c=e({},a);return c.id=0,c.status="initial",c.date=Date.now(),c},h=function(b,f){var h={},i={},j="jio/idarray";return h.init=function(c){var d,e=function(){},f;i.use_local_storage&&(f=a.getItem(j)||[],b.publisher&&(i.publisher=b.publisher),i.jio_id=c.jio_id,i.job_object_name="jio/jobobject/"+i.jio_id,f.push(i.jio_id),a.setItem(j,f)),i.job_object={},h.copyJobQueueToLocalStorage();for(d in i.recovered_job_object)i.recovered_job_object[d].callback=e,h.addJob(i.recovered_job_object[d])},h.close=function(){JSON.stringify(i.job_object)==="{}"&&a.deleteItem(i.job_object_name)},h.getNewQueueID=function(){var b=null,c=0,e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)e[b]>=d.queue_id&&(d.queue_id=e[b]+1);return c=d.queue_id,d.queue_id++,c},h.recoverOlderJobObject=function(){var b=null,c=[],d=!1,e;if(i.use_local_storage){e=a.getItem(j)||[];for(b=0;b<e.length;b+=1)a.getItem("jio/id/"+e[b])<Date.now()-1e4?(a.deleteItem("jio/id/"+e[b]),i.recovered_job_object=a.getItem("jio/jobobject/"+e[b]),a.deleteItem("jio/jobobject/"+e[b]),d=!0):c.push(e[b]);d&&a.setItem(j,c)}},h.isThereJobsWhere=function(a){var b="id";if(!a)return!0;for(b in i.job_object)if(a(i.job_object[b]))return!0;return!1},h.copyJobQueueToLocalStorage=function(){return i.use_local_storage?a.setItem(i.job_object_name,i.job_object):!1},h.createJob=function(a){return h.addJob(g(a))},h.addJob=function(a){var b=!0,c=[],e=[],f=[],g=null,j="id";for(j in i.job_object){if(d.job_managing_method.canRemoveFailOrDone(i.job_object[j],a)){f.push(j);continue}if(d.job_managing_method.canSelect(i.job_object[j],a)){if(d.job_managing_method.canEliminate(i.job_object[j],a)){c.push(j);continue}if(d.job_managing_method.canReplace(i.job_object[j],a)){g=k({queue:h,job:i.job_object[j]}),g.replace(a),b=!1;break}if(d.job_managing_method.cannotAccept(i.job_object[j],a))return!1;if(d.job_managing_method.mustWait(i.job_object[j],a)){e.push(j);continue}}}if(b){for(j=0;j<c.length;j+=1)g=k({queue:h,job:i.job_object[c[j]]}),g.eliminate();if(e.length>0){a.status="wait",a.waitingFor={jobIdArray:e};for(j=0;j<e.length;j+=1)i.job_object[e[j]]&&(i.job_object[e[j]].maxtries=1)}for(j=0;j<f.length;j+=1)h.removeJob(i.job_object[f[j]]);a.id=i.job_id,a.tries=0,i.job_id++,i.job_object[a.id]=a}return h.copyJobQueueToLocalStorage(),!0},h.removeJob=function(a){var b=e({where:function(a){return!0}},a),c,d=!1,f="key";if(b.job)i.job_object[b.job.id]&&b.where(i.job_object[b.job.id])&&(delete i.job_object[b.job.id],d=!0);else for(f in i.job_object)b.where(i.job_object[f])&&(delete i.job_object[f],d=!0);d||console.error("No jobs was found, when trying to remove some."),h.copyJobQueueToLocalStorage()},h.resetAll=function(){var a="id";for(a in i.job_object)i.job_object[a].status="initial";h.copyJobQueueToLocalStorage()},h.invokeAll=function(){var a="id",b,c;for(a in i.job_object){c=!1;if(i.job_object[a].status==="initial")h.invoke(i.job_object[a]);else if(i.job_object[a].status==="wait"){c=!0;if(i.job_object[a].waitingFor.jobIdArray)for(b=0;b<i.job_object[a].waitingFor.jobIdArray.length;b+=1)if(i.job_object[i.job_object[a].waitingFor.jobIdArray[b]]){c=!1;break}i.job_object[a].waitingFor.time&&i.job_object[a].waitingFor.time>Date.now()&&(c=!1),c&&h.invoke(i.job_object[a])}}this.copyJobQueueToLocalStorage()},h.invoke=function(a){var b;if(!c.job_method_object[a.method])return!1;h.isThereJobsWhere(function(b){return b.method===a.method&&b.method==="initial"})?a.status="ongoing":(a.status="ongoing",i.publisher.publish(c.job_method_object[a.method]["start_"+a.method])),b=k({queue:this,job:a}),b.execute()},h.ended=function(a){var b=e({},a);h.removeJob({job:b});if(!c.job_method_object[b.method])return!1;if(!h.isThereJobsWhere(function(a){return a.method===b.method&&a.status==="ongoing"||a.status==="initial"})){i.publisher.publish(c.job_method_object[b.method]["stop_"+b.method]);return}},h.clean=function(){h.removeJob(undefined,{where:function(a){return a.status==="fail"}})},i.use_local_storage=b.options.use_local_storage,i.publisher=b.publisher,i.job_id=1,i.jio_id=0,i.job_object_name="",i.job_object={},i.recovered_job_object={},h},i=function(a,b){var c={},d={};return d.interval=200,d.id=null,d.queue=a.queue,c.setIntervalDelay=function(a){d.interval=a},c.start=function(){return d.id?!1:(d.id=setInterval(function(){d.queue.recoverOlderJobObject(),d.queue.invokeAll()},d.interval),!0)},c.stop=function(){return d.id?(clearInterval(d.id),d.id=null,!0):!1},c},j=function(){var b={},c={};return c.interval=400,c.id=null,b.start=function(a){return c.id?!1:(b.touch(a),c.id=setInterval(function(){b.touch(a)},c.interval),!0)},b.stop=function(){return c.id?(clearInterval(c.id),c.id=null,!0):!1},b.touch=function(b){a.setItem("jio/id/"+b,Date.now())},b},k=function(a){var b={},c={};return c.job=a.job,c.callback=a.job.callback,c.queue=a.queue,c.res={status:"done",message:""},c.fail_checkNameAvailability=function(){c.res.message="Unable to check name availability."},c.done_checkNameAvailability=function(a){c.res.message=c.job.userName+" is "+(a?"":"not ")+"available.",c.res.return_value=a},c.fail_loadDocument=function(){c.res.message="Unable to load document."},c.done_loadDocument=function(a){c.res.message="Document loaded.",c.res.return_value=a,c.res.return_value.lastModified=(new Date(c.res.return_value.lastModified)).getTime(),c.res.return_value.creationDate=(new Date(c.res.return_value.creationDate)).getTime()},c.fail_saveDocument=function(){c.res.message="Unable to save document."},c.done_saveDocument=function(){c.res.message="Document saved."},c.fail_getDocumentList=function(){c.res.message="Unable to retrieve document list."},c.done_getDocumentList=function(a){var b;c.res.message="Document list received.",c.res.return_value=a;for(b=0;b<c.res.return_value.length;b+=1)c.res.return_value[b].lastModified=(new Date(c.res.return_value[b].lastModified)).getTime(),c.res.return_value[b].creationDate=(new Date(c.res.return_value[b].creationDate)).getTime()},c.fail_removeDocument=function(){c.res.message="Unable to removed document."},c.done_removeDocument=function(){c.res.message="Document removed."},c.retryLater=function(){var a=c.job.tries*c.job.tries*1e3;a>d.max_wait_time&&(a=d.max_wait_time),c.job.status="wait",c.job.waitingFor={time:Date.now()+a}},b.cloneJob=function(){return e({},c.job)},b.getUserName=function(){return c.job.userName||""},b.getApplicantID=function(){return c.job.applicant.ID||""},b.getStorageUserName=function(){return c.job.storage.userName||""},b.getStoragePassword=function(){return c.job.storage.password||""},b.getStorageLocation=function(){return c.job.storage.location||""},b.getStorageArray=function(){return c.job.storage.storageArray||[]},b.getFileName=function(){return c.job.fileName||""},b.getFileContent=function(){return c.job.fileContent||""},b.cloneOptionObject=function(){return e({},c.job.options)},b.getMaxTries=function(){return c.job.maxtries},b.getTries=function(){return c.job.tries||0},b.setMaxTries=function(a){c.job.maxtries=a},b.addJob=function(a){return c.queue.createJob(a)},b.eliminate=function(){c.job.maxtries=1,c.job.tries=1,b.fail("Job Stopped!",0)},b.replace=function(a){c.job.tries=0,c.job.date=a.date,c.job.callback=a.callback,c.res.status="fail",c.res.message="Job Stopped!",c.res.error={},c.res.error.status=0,c.res.error.statusText="Replaced",c.res.error.message="The job was replaced by a newer one.",c["fail_"+c.job.method](),c.callback(c.res)},b.fail=function(a){c.res.status="fail",c.res.error=a,c.res.error.status=c.res.error.status||0,c.res.error.statusText=c.res.error.statusText||"Unknown Error",c.res.error.array=c.res.error.array||[],c.res.error.message=c.res.error.message||"",!c.job.maxtries||c.job.tries<c.job.maxtries?c.retryLater():(c.job.status="fail",c["fail_"+c.job.method](),c.queue.ended(c.job),c.callback(c.res))},b.done=function(a){c.job.status="done",c["done_"+c.job.method](a),c.queue.ended(c.job),c.callback(c.res)},b.execute=function(){return c.job.tries=b.getTries()+1,d.storage_type_object[c.job.storage.type]?d.storage_type_object[c.job.storage.type]({job:c.job,queue:c.queue})[c.job.method]():null},b.checkNameAvailability=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.loadDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.saveDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.getDocumentList=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b.removeDocument=function(){b.fail({status:0,statusText:"Undefined Method",message:"This method must be redefined!"})},b},l=function(a,b){var c={},g={};g.wrongParametersError=function(a){var b="Method: "+a.method+", One or some parameters are undefined.";return console.error(b),a.callback({status:"fail",error:{status:0,statusText:"Undefined Parameter",message:b}}),null},c.getID=function(){return g.id},c.start=function(){return g.id!==0?!1:(g.id=g.queue.getNewQueueID(),g.queue.init({jio_id:g.id}),g.updater&&g.updater.start(g.id),g.listener.start(),g.ready=!0,c.isReady())},c.stop=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,g.id=0,!0},c.kill=function(){return g.queue.close(),g.listener.stop(),g.updater&&g.updater.stop(),g.ready=!1,!0},c.isReady=function(){return g.ready},c.publish=function(a,b){if(!c.isReady())return;return g.pubsub.publish(a,b)},c.subscribe=function(a,b){return g.pubsub.subscribe(a,b)},c.unsubscribe=function(a,b){return g.pubsub.unsubscribe(a,b)},c.checkNameAvailability=function(a){var b=e({userName:g.storage.userName,storage:g.storage,applicant:g.applicant,method:"checkNameAvailability",callback:function(){}},a);return c.isReady()&&b.userName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.saveDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,fileContent:"",method:"saveDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.loadDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"loadDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.getDocumentList=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"getDocumentList",callback:function(){}},a);return c.isReady()&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)},c.removeDocument=function(a){var b=e({storage:g.storage,applicant:g.applicant,method:"removeDocument",callback:function(){}},a);return c.isReady()&&b.fileName&&b.storage&&b.applicant?g.queue.createJob(b):g.wrongParametersError(b)};var k=e({use_local_storage:!0},a.options);return typeof a.storage=="string"&&(a.storage=JSON.parse(a.storage)),typeof a.applicant=="string"&&(a.applicant=JSON.parse(a.applicant)),g.storage=a.storage,g.applicant=a.applicant,g.id=0,g.pubsub=f({options:k}),g.queue=h({publisher:g.pubsub,options:k}),g.listener=i({queue:g.queue,options:k}),g.ready=!1,k.use_local_storage?g.updater=j({options:k}):g.updater=null,g.storage&&!d.storage_type_object[g.storage.type]&&console.error('Unknown storage type "'+g.storage.type+'"'),c.start(),c},m=function(a,b){var f={};return f.createNew=function(a,b,c){var d=e({use_local_storage:!0},c);return l({storage:a,applicant:b,options:d})},f.newBaseStorage=function(a,b){return k(a,b)},f.addStorageType=function(a,b){return a&&b?(d.storage_type_object[a]=b,!0):!1},f.getGlobalObject=function(){return d},f.getConstObject=function(){return e({},c)},f},m()};return window.requirejs?(define("JIO",["LocalOrCookieStorage","jQuery"],a),undefined):a(LocalOrCookieStorage,jQuery)}();
\ No newline at end of file \ No newline at end of file
...@@ -1071,11 +1071,12 @@ var JIO = ...@@ -1071,11 +1071,12 @@ var JIO =
var settings = extend({ var settings = extend({
'storage': priv.storage, 'storage': priv.storage,
'applicant': priv.applicant, 'applicant': priv.applicant,
'fileContent': '',
'method':'saveDocument', 'method':'saveDocument',
'callback': function () {} 'callback': function () {}
},options); },options);
// check dependencies // check dependencies
if (that.isReady() && settings.fileName && settings.fileContent && if (that.isReady() && settings.fileName &&
settings.storage && settings.applicant) { settings.storage && settings.applicant) {
return priv.queue.createJob ( settings ); return priv.queue.createJob ( settings );
} }
......
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