varjio=function(){vara=function(a,b){varc={};a=a||{},b=b||{};vard={};returnd.path=a.path||"",d.option=a.option||{},d.respond=d.option.onResponse||function(){},d.done=d.option.onDone||function(){},d.fail=d.option.onFail||function(){},d.end=function(){},c.getLabel=function(){return"command"},c.getPath=function(){returnd.path},c.getOption=function(a){returnd.option[a]},c.validate=function(a){c.validateState()},c.execute=function(a){c.validate(a),a.execute(c)},c.executeOn=function(a){},c.validateState=function(){if(d.path==="")throwg({command:c,message:"Path is empty"})},c.done=function(a){console.log("test"),d.done(a),d.respond({value:a}),d.end()},c.fail=function(a){d.fail(a),d.respond({error:a}),d.end()},c.onEndDo=function(a){d.end=a},c.serialized=function(){return{label:c.getLabel(),path:d.path,option:d.option}},c},b=function(b,c){vard=a(b,c);returnb=b||{},c=c||{},d.label=function(){return"getDocumentList"},d.executeOn=function(a){a.getDocumentList(d)},d},c=function(b,c){vard=a(b,c);returnb=b||{},c=c||{},d.label=function(){return"loadDocument"},d.executeOn=function(a){a.loadDocument(d)},d},d=function(b,c){vard=a(b,c);returnb=b||{},c=c||{},d.label=function(){return"removeDocument"},d.executeOn=function(a){a.removeDocument(d)},d},e=function(b,c){vard=a(b,c);b=b||{},c=c||{};vare=b.content;d.label=function(){return"saveDocument"},d.getContent=function(){returne};varf=d.validate;returnd.validate=function(a){if(typeofe!="string")throwg({command:d,message:"No data to save"});f(a)},d.executeOn=function(a){a.saveDocument(d)},d},f=function(a,b){varc={};returna=a||{},b=b||{},c.name="jioException",c.message=a.message||"Unknown Reason.",c.toString=function(){returnc.name+": "+c.message},c},g=function(a,b){varc=f(a,b);a=a||{};vard=a.command;returnc.name="invalidCommandState",c.toString=function(){returnc.name+": "+d.getLabel()+", "+c.message},c},h=function(a,b){varc=f(a,b);a=a||{};vard=a.storage.getType();returnc.name="invalidStorage",c.toString=function(){returnc.name+": "+'Type "'+d+'", '+c.message},c},i=function(a,b){varc=f(a,b),d=a.type;returnc.name="invalidStorageType",c.toString=function(){returnc.name+": "+d+", "+c.message},c},j=function(a,b){varc=f(a,b);returnc.name="jobNotReadyException",c},k=function(a,b){varc=f(a,b);returnc.name="tooMuchTriesJobException",c},l=function(a,b){varc=f(a,b);returnc.name="invalidJobException",c},m=function(a,b){varc={};returna=a||{},b=b||{},c.getLabel=function(){return"job status"},c.canStart=function(){},c.canRestart=function(){},c.serialized=function(){return{label:c.getLabel()}},c},n=function(a,b){varc=m(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"done"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},o=function(a,b){varc=m(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"fail"},c.canStart=function(){return!1},c.canRestart=function(){return!0},c},p=function(a,b){varc=m(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"initial"},c.canStart=function(){return!0},c.canRestart=function(){return!0},c},q=function(a,b){varc=m(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"on going"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},r=function(a,b){varc=m(a,b);a=a||{},b=b||{};vard=a.job_id_array||[],e=0;returnc.getLabel=function(){return"wait"},c.waitForJob=function(a){varb;for(b=0;b<d.length;b+=1)if(d[b]===a.getId())return;d.push(a.getId())},c.dontWaitForJob=function(a){varb,c=[];for(b=0;b<d.length;b+=1)d[b]!==a.getId()&&c.push(d[b]);d=c},c.waitForTime=function(a){e=Date.now()+a},c.stopWaitForTime=function(){e=0},c.canStart=function(){returnd.length===0&&Date.now()>=e},c.canRestart=function(){return!1},c.serialized=function(){return{label:c.getLabel(),waitfortime:e,waitforjob:d}},c},s=function(a,b){varc={};a=a||{},b=b||{};vard={};returnd.type=a.type||"",c.getType=function(){returnd.type},c.execute=function(a){a.executeOn(c)},c.isValid=function(){return!0},c.validate=function(a){a.validate(c)},c.serialized=function(){return{type:c.getType()}},c.saveDocument=function(a){throwh({storage:c,message:"Unknown storage."})},c.loadDocument=function(a){c.saveDocument()},c.removeDocument=function(a){c.saveDocument()},c.getDocumentList=function(a){c.saveDocument()},c},t=function(a,b){varc=s(a,b);a=a||{},b=b||{};vard={};returnd.storage_a=a.storagelist||[],c.beforeExecute=function(a,b){},c.execute=function(a,b){vare;c.validate(a),c.beforeExecute(a,b);for(e=0;e<d.storage_a.length;e++)d.storage_a[e].execute(a);c.afterExecute(a,b)},c.afterExecute=function(a,b){c.done()},c.serialized=function(){return{type:d.type,storagelist:d.storagelist}},c},u=function(a,b){varc={};a=a||{},b=b||{};vard={};returnd.id=a.id||0,d.interval=400,d.interval_id=null,d.touch=function(){LocalOrCookieStorage.setItem("jio/id/"+d.id,Date.now())},c.setId=function(a){d.id=a},c.setIntervalDelay=function(a){d.interval=a},c.getIntervalDelay=function(){returnd.interval},c.start=function(){d.interval_id||(d.touch(),d.interval_id=setInterval(function(){d.touch()},d.interval))},c.stop=function(){d.interval_id!==null&&(clearInterval(d.interval_id),d.interval_id=null)},c}(),v=function(a,f){varg=function(a,b){varc={};a=a||{},b=b||{};vard={};returnd.id=m.nextId(),d.command=a.command,d.storage=a.storage,d.status=p(),d.tried=0,d.max_retry=0,d.date=newDate,function(){if(!d.storage)throwl({job:c,message:"No storage set"});if(!d.command)throwl({job:c,message:"No command set"})}(),c.getCommand=function(){returnd.command},c.getStatus=function(){returnd.status},c.getId=function(){returnd.id},c.getStorage=function(){returnd.storage},c.isReady=function(){returnd.tried===0?d.status.canStart():d.status.canRestart()},c.serialized=function(){return{id:d.id,date:d.date.getTime(),tried:d.tried,max_retry:d.max_retry,status:d.status.serialized(),command:d.command.serialized(),storage:d.storage.serialized()}},c.waitForJob=function(a){d.status.getLabel()!=="wait"&&(d.status=r()),d.status.waitForJob(a)},c.dontWaitFor=function(a){d.status.getLabel()==="wait"&&d.status.dontWaitForJob(a)},c.waitForTime=function(a){d.status.getLabel()!=="wait"&&(d.status=r()),d.status.waitForTime(a)},c.stopWaitForTime=function(){d.status.getLabel()==="wait"&&d.status.stopWaitForTime()},c.update=function(a){d.date=a.getDate()},c.execute=function(){if(d.max_retry!==0&&d.tried>=d.max_retry)throwk({job:c,message:"The job was invoked too much time."});if(!c.isReady())throwj({message:"Can not execute this job."});d.status=q(),d.tried++,d.command.onEndDo(function(){n.terminateJob(c)}),d.command.execute(d.storage)},c},h=function(a,b){varc={};a=a||{},b=b||{};vard=[],e=a.name||"";returnc.add=function(a){d.push(a)},c.remove=function(a){varb,c=[];for(b=0;b<d.length;b+=1)d[b]!==a&&c.push(d[b]);d=c},c.register=function(){i.register(c)},c.unregister=function(){i.unregister(c)},c.trigger=function(a){varb;for(b=0;b<d.length;b++)d[b].apply(null,a)},c},i=function(a,b){varc={};a=a||{},b=b||{};vard={};returnc.register=function(a){d[a]||(d[a]=h())},c.unregister=function(a){d[a]&&deleted[a]},c.at=function(a){returnd[a]},c.on=function(a,b){c.register(a),c.at(a).add(b)},c.trigger=function(a,b){c.at(a).trigger(b)},c}(),m=function(a,b){varc={};a=a||{},b=b||{};vard=0;returnc.nextId=function(){returnd=d+1,d},c}(),n=function(a,b){varc={};a=a||{},b=b||{};vard="jio/job_array",e={};returne.id=a.id,e.interval_id=null,e.interval=200,e.job_a=[],e.getJobArrayName=function(){returnd+"/"+e.id},e.getJobArray=function(){returnLocalOrCookieStorage.getItem(e.getJobArrayName())||[]},e.copyJobArrayToLocal=function(){vara=[],b;for(b=0;b<e.job_a.length;b+=1)a.push(e.job_a[b].serialized());LocalOrCookieStorage.setItem(e.getJobArrayName(),a)},e.removeJob=function(a){varb,c=[];for(b=0;b<e.job_a.length;b+=1)e.job_a[b]!==a&&c.push(e.job_a[b]);e.job_a=c,e.copyJobArrayToLocal()},c.setId=function(a){e.id=a},c.start=function(){vara;e.interval_id===null&&(e.interval_id=setInterval(function(){for(a=0;a<e.job_a.length;a+=1)c.execute(e.job_a[a])},e.interval))},c.stop=function(){e.interval_id!==null&&(clearInterval(e.interval_id),e.interval_id=null,e.job_a.length===0&&LocalOrCookieStorage.deleteItem(e.getJobArrayName()))},c.execute=function(a){try{a.execute()}catch(b){switch(b.name){case"jobNotReadyException":break;case"tooMuchTriesJobException":break;default:throwb}}e.copyJobArrayToLocal()},c.terminateJob=function(a){e.removeJob(a),e.copyJobArrayToLocal()},c.addJob=function(a){varb=c.validateJobAccordingToJobList(e.job_a,a);e.manage(a,b),e.copyJobArrayToLocal()},c.validateJobAccordingToJobList=function(a,b){varc,d=[];for(c=0;c<a.length;c+=1)d.push(o.validateJobAccordingToJob(a[c],b));returnd},e.manage=function(a,b){vard;if(e.job_a.length!==b.length)thrownewRangeError("Array out of bound");for(d=0;d<b.length;d+=1)if(b[d].action==="dont accept")return;for(d=0;d<b.length;d+=1)switch(b[d].action){case"eliminate":c.eliminate(b[d].job);break;case"replace":a.update(b[d].job),e.copyJobArrayToLocal();return;case"wait":a.waitForJob(b[d].job);break;default:}e.job_a.push(a),e.copyJobArrayToLocal()},c.eliminate=function(a){varb,c=[];for(b=0;b<e.job_a.length;b+=1)e.job_a[b].getId()!==a.getId()&&c.push(e.job_a[b]);e.job_a=c},c}(),o=function(a,b){varc={},d={};returnc.eliminate=function(){return"eliminate"},c.update=function(){return"update"},c.dontAccept=function(){return"dont accept"},c.wait=function(){return"wait"},c.none=function(){return"none"},d.compare={},d.default_compare=function(a,b){returna.getCommand().getPath()===b.getCommand().getPath()&&JSON.stringify(a.getStorage())===JSON.stringify(b.getStorage())},d.action={saveDocument:{"on going":{saveDocument:function(a,b){returna.getCommand().getContent()===b.getCommand().getContent()?c.dontAccept():c.wait()},loadDocument:c.wait,removeDocument:c.wait,getDocumentList:c.none},"not on going":{saveDocument:c.update,loadDocument:c.wait,removeDocument:c.eliminate,getDocumentList:c.none}},loadDocument:{"on going":{saveDocument:c.wait,loadDocument:c.dontAccept,removeDocument:c.wait,getDocumentList:c.none},"not on going":{saveDocument:c.wait,loadDocument:c.update,removeDocument:c.wait,getDocumentList:c.none}},removeDocument:{"on going":{saveDocument:c.wait,loadDocument:c.dontAccept,removeDocument:c.dontAccept,getDocumentList:c.none},"not on going":{saveDocument:c.eliminate,loadDocument:c.dontAccept,removeDocument:c.update,getDocumentList:c.none}},getDocumentList:{"on going":{saveDocument:c.none,loadDocument:c.none,removeDocument:c.none,getDocumentList:c.dontAccept},"not on going":{saveDocument:c.none,loadDocument:c.none,removeDocument:c.none,getDocumentList:c.update}}},d.default_action="none",d.getAction=function(a,b){varc,e,f;c=a.getCommand().getLabel(),e=b.getCommand().getLabel(),f=a.getStatus().getLabel()==="on going"?"on going":"not on going";try{returnd.action[c][f][e](a,b)}catch(g){returnd.default_action}},d.canCompare=function(a,b){varc=d.stringifyJobForCompare(a,b);returnd.compare[c]?d.compare[c](a,b):d.default_compare(a,b)},c.validateJobAccordingToJob=function(a,b){varc=d.stringifyJobForAction(a,b);returnd.canCompare(a,b)?{action:d.getAction(a,b),job:a}:{action:d.default_action,job:a}},c}(),s={};a=a||{},f=f||{};vart={},v="jio/id_array";returnt.id=1,t.storage=w.storage(a,s),function(){vara,b=LocalOrCookieStorage.getItem(v)||[];for(a=0;a<b.length;a+=1)b[a]>=t.id&&(t.id=b[a]+1);b.push(t.id),LocalOrCookieStorage.setItem(v,b)}(),function(){u.setId(t.id),u.start(),n.setId(t.id),n.start()}(),s.start=function(){n.start()},s.stop=function(){n.stop()},s.getId=function(){returnt.id},s.validateStorageDescription=function(a){returnw.storage(a.type)(a).isValid()},s.saveDocument=function(a,b,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,console.log("add job save: "+JSON.stringify(t.storage.serialized())),n.addJob(g({storage:d?w.storage(d):t.storage,command:e({path:a,content:b,option:c})}))},s.loadDocument=function(a,b,d){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,b.metadata_only=b.metadata_only!==undefined?b.metadata_only:!1,n.addJob(g({storage:d?w.storage(d):t.storage,command:c({path:a,option:b})}))},s.removeDocument=function(a,b,c){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,n.addJob(g({storage:c?w.storage(c):t.storage,command:d({path:a,option:b})}))},s.getDocumentList=function(a,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,c.metadata_only=c.metadata_only!==undefined?c.metadata_only:!0,n.addJob(g({storage:d?w.storage(d):t.storage,command:b({path:a,option:c})}))},s},w=function(a,b){varc={};a=a||{},b=b||{};vard={base:s,handler:t};returnc.storage=function(a,b){a=a||{};varc=a.type||"base";if(!d[c])throwi({type:c});returnconsole.log("create storage: "+JSON.stringify(a)+JSON.stringify(b)),d[c](a,b)},c.newJio=function(a){varb=a;returntypeofb=="string"&&(b=JSON.parse(b)),b=b||{type:"base"},console.log("new jio: storage: "+JSON.stringify(a)),v(a)},c.addStorageType=function(a,b){b=b||function(){returnnull};if(d[a])throwi({type:a,message:"Already known."});d[a]=b,console.log("adding: "+a)},c}();returnw}();
\ No newline at end of file
varjio=function(){vara=function(a,b){varc={};returna=a||{},b=b||{},c.name="jioException",c.message=a.message||"Unknown Reason.",c.toString=function(){returnc.name+": "+c.message},c},b=function(b,c){vard=a(b,c);b=b||{};vare=b.command;returnd.name="invalidCommandState",d.toString=function(){returnd.name+": "+e.getLabel()+", "+d.message},d},c=function(b,c){vard=a(b,c);b=b||{};vare=b.storage.getType();returnd.name="invalidStorage",d.toString=function(){returnd.name+": "+'Type "'+e+'", '+d.message},d},d=function(b,c){vard=a(b,c),e=b.type;returnd.name="invalidStorageType",d.toString=function(){returnd.name+": "+e+", "+d.message},d},e=function(b,c){vard=a(b,c);returnd.name="jobNotReadyException",d},f=function(b,c){vard=a(b,c);returnd.name="tooMuchTriesJobException",d},g=function(b,c){vard=a(b,c);returnd.name="invalidJobException",d},h=function(a,b){vard={};a=a||{},b=b||{};vare={};returne.type=a.type||"",d.getType=function(){returne.type},d.setType=function(a){e.type=a},d.execute=function(a){a.executeOn(d)},d.isValid=function(){return!0},d.validate=function(a){a.validate(d)},d.serialized=function(){return{type:d.getType()}},d.saveDocument=function(a){throwc({storage:d,message:"Unknown storage."})},d.loadDocument=function(a){d.saveDocument()},d.removeDocument=function(a){d.saveDocument()},d.getDocumentList=function(a){d.saveDocument()},d},i=function(a,b){varc=h(a,b);a=a||{},b=b||{};vard={};returnd.storage_a=a.storagelist||[],c.beforeExecute=function(a,b){},c.execute=function(a,b){vare;c.validate(a),c.beforeExecute(a,b);for(e=0;e<d.storage_a.length;e++)d.storage_a[e].execute(a);c.afterExecute(a,b)},c.afterExecute=function(a,b){c.done()},c.serialized=function(){return{type:d.type,storagelist:d.storagelist}},c},j=function(a,c){vard=function(a,c){vard={};a=a||{},c=c||{};vare={};returne.commandlist={saveDocument:l,loadDocument:i,removeDocument:j,getDocumentList:h},a.label&&e.commandlist[a.label]?(e.label=a.label,deletea.label,e.commandlist[e.label](a,c)):(e.path=a.path||"",e.tried=0,e.option=a.option||{},e.respond=e.option.onResponse||function(){},e.done=e.option.onDone||function(){},e.fail=e.option.onFail||function(){},e.retry=function(){d.setMaxRetry(-1),d.fail({status:0,statusText:"Fail Retry",message:"Impossible to retry."})},e.end=function(){},d.getLabel=function(){return"command"},d.getPath=function(){returne.path},d.getOption=function(a){returne.option[a]},d.validate=function(a){d.validateState()},d.getTried=function(){returne.tried},d.setMaxRetry=function(a){e.option.max_retry=a},d.execute=function(a){d.validate(a),e.tried++,a.execute(d)},d.executeOn=function(a){},d.validateState=function(){if(e.path==="")throwb({command:d,message:"Path is empty"})},d.done=function(a){console.log("done"),e.done(a),e.respond({status:n(),value:a}),e.end()},d.fail=function(a){e.option.max_retry===0||e.tried<e.option.max_retry?e.retry():(console.log("fail"),e.fail(a),e.respond({status:o(),error:a}),e.end())},d.onEndDo=function(a){e.end=a},d.onRetryDo=function(a){e.retry=a},d.serialized=function(){return{label:d.getLabel(),tried:e.tried,max_retry:e.max_retry,path:e.path,option:e.option}},d)},h=function(a,b){varc=d(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"getDocumentList"},c.executeOn=function(a){a.getDocumentList(c)},c},i=function(a,b){varc=d(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"loadDocument"},c.executeOn=function(a){a.loadDocument(c)},c},j=function(a,b){varc=d(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"removeDocument"},c.executeOn=function(a){a.removeDocument(c)},c},l=function(a,c){vare=d(a,c);a=a||{},c=c||{};varf=a.content;e.getLabel=function(){return"saveDocument"},e.getContent=function(){returnf};varg=e.validate;returne.validate=function(a){if(typeoff!="string")throwb({command:e,message:"No data to save"});g(a)},e.executeOn=function(a){a.saveDocument(e)},e},m=function(a,b){varc={};returna=a||{},b=b||{},c.getLabel=function(){return"job status"},c.canStart=function(){},c.canRestart=function(){},c.serialized=function(){return{label:c.getLabel()}},c},n=function(a,b){varc=m(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"done"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},o=function(a,b){varc=m(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"fail"},c.canStart=function(){return!1},c.canRestart=function(){return!0},c},p=function(a,b){varc=m(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"initial"},c.canStart=function(){return!0},c.canRestart=function(){return!0},c},q=function(a,b){varc=m(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"on going"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},r=function(a,b){varc=m(a,b);a=a||{},b=b||{};vard={};returnd.job_id_a=a.job_id_array||[],d.threshold=0,c.getLabel=function(){return"wait"},d.refreshJobIdArray=function(){vara=[],b;for(b=0;b<d.job_id_a.length;b+=1)x.jobIdExists(d.job_id_a[b])&&a.push(d.job_id_a[b]);d.job_id_a=a},c.waitForJob=function(a){varb;for(b=0;b<d.job_id_a.length;b+=1)if(d.job_id_a[b]===a.getId())return;d.job_id_a.push(a.getId())},c.dontWaitForJob=function(a){varb,c=[];for(b=0;b<d.job_id_a.length;b+=1)d.job_id_a[b]!==a.getId()&&c.push(d.job_id_a[b]);d.job_id_a=c},c.waitForTime=function(a){d.threshold=Date.now()+a},c.stopWaitForTime=function(){d.threshold=0},c.canStart=function(){returnd.refreshJobIdArray(),console.log(d.job_id_a),d.job_id_a.length===0&&Date.now()>=d.threshold},c.canRestart=function(){returnc.canStart()},c.serialized=function(){return{label:c.getLabel(),waitfortime:d.threshold,waitforjob:d.job_id_a}},c},s=function(a,b){varc={};a=a||{},b=b||{};vard={};returnd.id=w.nextId(),d.command=a.command,d.storage=a.storage,d.status=p(),d.date=newDate,function(){if(!d.storage)throwg({job:c,message:"No storage set"});if(!d.command)throwg({job:c,message:"No command set"})}(),c.getCommand=function(){returnd.command},c.getStatus=function(){returnd.status},c.getId=function(){returnd.id},c.getStorage=function(){returnd.storage},c.getDate=function(){returnd.date},c.isReady=function(){returnd.tried===0?d.status.canStart():d.status.canRestart()},c.serialized=function(){return{id:d.id,date:d.date.getTime(),status:d.status.serialized(),command:d.command.serialized(),storage:d.storage.serialized()}},c.waitForJob=function(a){d.status.getLabel()!=="wait"&&(d.status=r()),d.status.waitForJob(a)},c.dontWaitFor=function(a){d.status.getLabel()==="wait"&&d.status.dontWaitForJob(a)},c.waitForTime=function(a){d.status.getLabel()!=="wait"&&(d.status=r()),d.status.waitForTime(a)},c.stopWaitForTime=function(){d.status.getLabel()==="wait"&&d.status.stopWaitForTime()},c.update=function(a){console.log("updating"),d.command.setMaxRetry(-1),d.command.fail({status:0,statusText:"Replaced",message:"Job has been replaced by another one."}),d.date=a.getDate(),d.command=a.getCommand(),d.status=a.getStatus()},c.execute=function(){if(d.max_retry!==0&&d.tried>=d.max_retry)throwf({job:c,message:"The job was invoked too much time."});if(!c.isReady())throwe({message:"Can not execute this job."});d.status=q(),d.command.onRetryDo(function(){vara=d.command.getTried();a=a*a*200,a>1e4&&(a=1e4),c.waitForTime(a)}),d.command.onEndDo(function(){x.terminateJob(c)}),d.command.execute(d.storage)},c},t=function(a,b){varc={};a=a||{},b=b||{};vard=[],e=a.name||"";returnc.add=function(a){d.push(a)},c.remove=function(a){varb,c=[];for(b=0;b<d.length;b+=1)d[b]!==a&&c.push(d[b]);d=c},c.register=function(){v.register(c)},c.unregister=function(){v.unregister(c)},c.trigger=function(a){varb;for(b=0;b<d.length;b++)d[b].apply(null,a)},c},u=function(a,b){varc={};a=a||{},b=b||{};vard={};returnd.id=a.id||0,d.interval=400,d.interval_id=null,d.touch=function(){LocalOrCookieStorage.setItem("jio/id/"+d.id,Date.now())},c.setId=function(a){d.id=a},c.setIntervalDelay=function(a){d.interval=a},c.getIntervalDelay=function(){returnd.interval},c.start=function(){d.interval_id||(d.touch(),d.interval_id=setInterval(function(){d.touch()},d.interval))},c.stop=function(){d.interval_id!==null&&(clearInterval(d.interval_id),d.interval_id=null)},c}(),v=function(a,b){varc={};a=a||{},b=b||{};vard={};returnc.register=function(a){d[a]||(d[a]=t())},c.unregister=function(a){d[a]&&deleted[a]},c.at=function(a){returnd[a]},c.on=function(a,b){c.register(a),c.at(a).add(b)},c.trigger=function(a,b){c.at(a).trigger(b)},c}(),w=function(a,b){varc={};a=a||{},b=b||{};vard=0;returnc.nextId=function(){returnd=d+1,d},c}(),x=function(a,b){varc={};a=a||{},b=b||{};vare="jio/job_array",f={};returnf.id=a.id,f.interval_id=null,f.interval=200,f.job_a=[],f.getJobArrayName=function(){returne+"/"+f.id},f.getJobArray=function(){returnLocalOrCookieStorage.getItem(f.getJobArrayName())||[]},f.copyJobArrayToLocal=function(){vara=[],b;for(b=0;b<f.job_a.length;b+=1)a.push(f.job_a[b].serialized());LocalOrCookieStorage.setItem(f.getJobArrayName(),a)},f.removeJob=function(a){varb,c=[];for(b=0;b<f.job_a.length;b+=1)f.job_a[b]!==a&&c.push(f.job_a[b]);f.job_a=c,f.copyJobArrayToLocal()},c.setId=function(a){f.id=a},c.start=function(){vara;f.interval_id===null&&(f.interval_id=setInterval(function(){f.restoreOldJio();for(a=0;a<f.job_a.length;a+=1)c.execute(f.job_a[a])},f.interval))},c.stop=function(){f.interval_id!==null&&(clearInterval(f.interval_id),f.interval_id=null,f.job_a.length===0&&LocalOrCookieStorage.deleteItem(f.getJobArrayName()))},f.restoreOldJio=function(){vara,b;f.lastrestore=f.lastrestore||0;if(f.lastrestore>Date.now()-2e3)return;b=LocalOrCookieStorage.getItem("jio/id_array")||[];for(a=0;a<b.length;a+=1)f.restoreOldJioId(b[a]);f.lastrestore=Date.now()},f.restoreOldJioId=function(a){varb;b=LocalOrCookieStorage.getItem("jio/id/"+a)||0,b<Date.now()-1e4&&(f.restoreOldJobFromJioId(a),f.removeOldJioId(a))},f.restoreOldJobFromJioId=function(a){varb,e;e=LocalOrCookieStorage.getItem("jio/job_array/"+a)||[];for(b=0;b<e.length;b+=1)c.addJob(s({storage:k.storage(e[b]),command:d(e[b].command)}))},f.removeOldJioId=function(a){varb,c,d=[];c=LocalOrCookieStorage.getItem("jio/id_array")||[];for(b=0;b<c.length;b+=1)c[b]!==a&&d.push(c[b]);LocalOrCookieStorage.setItem("jio/id_array",d)},c.execute=function(a){try{a.execute()}catch(b){switch(b.name){case"jobNotReadyException":break;case"tooMuchTriesJobException":break;default:throwb}}f.copyJobArrayToLocal()},c.jobIdExists=function(a){varb;for(b=0;b<f.job_a.length;b+=1)if(f.job_a[b].getId()===a)returnconsole.log("found"),!0;returnconsole.log("not found"),!1},c.terminateJob=function(a){f.removeJob(a),f.copyJobArrayToLocal()},c.addJob=function(a){varb=c.validateJobAccordingToJobList(f.job_a,a);f.manage(a,b)},c.validateJobAccordingToJobList=function(a,b){varc,d=[];for(c=0;c<a.length;c+=1)d.push(y.validateJobAccordingToJob(a[c],b));returnd},f.manage=function(a,b){vard;if(f.job_a.length!==b.length)thrownewRangeError("Array out of bound");for(d=0;d<b.length;d+=1)if(b[d].action==="dont accept")return;console.log("managing "+JSON.stringify(b));for(d=0;d<b.length;d+=1)switch(b[d].action){case"eliminate":console.log("eliminating"),c.eliminate(b[d].job);break;case"update":b[d].job.update(a),f.copyJobArrayToLocal();return;case"wait":console.log("wait"),a.waitForJob(b[d].job);break;default:}f.job_a.push(a),f.copyJobArrayToLocal()},c.eliminate=function(a){varb,c=[];for(b=0;b<f.job_a.length;b+=1)f.job_a[b].getId()!==a.getId()&&(c.push(f.job_a[b]),console.log("add: "+f.job_a[b].getId()+" -> it is not "+a.getId()));f.job_a=c,f.copyJobArrayToLocal()},c}(),y=function(a,b){varc={},d={};returnc.eliminate=function(){return"eliminate"},c.update=function(){return"update"},c.dontAccept=function(){return"dont accept"},c.wait=function(){return"wait"},c.none=function(){return"none"},d.compare={},d.default_compare=function(a,b){returna.getCommand().getPath()===b.getCommand().getPath()&&JSON.stringify(a.getStorage().serialized())===JSON.stringify(b.getStorage().serialized())},d.action={saveDocument:{"on going":{saveDocument:function(a,b){returna.getCommand().getContent()===b.getCommand().getContent()?c.dontAccept():c.wait()},loadDocument:c.wait,removeDocument:c.wait,getDocumentList:c.none},"not on going":{saveDocument:c.update,loadDocument:c.wait,removeDocument:c.eliminate,getDocumentList:c.none}},loadDocument:{"on going":{saveDocument:c.wait,loadDocument:c.dontAccept,removeDocument:c.wait,getDocumentList:c.none},"not on going":{saveDocument:c.wait,loadDocument:c.update,removeDocument:c.wait,getDocumentList:c.none}},removeDocument:{"on going":{saveDocument:c.wait,loadDocument:c.dontAccept,removeDocument:c.dontAccept,getDocumentList:c.none},"not on going":{saveDocument:c.eliminate,loadDocument:c.dontAccept,removeDocument:c.update,getDocumentList:c.none}},getDocumentList:{"on going":{saveDocument:c.none,loadDocument:c.none,removeDocument:c.none,getDocumentList:c.dontAccept},"not on going":{saveDocument:c.none,loadDocument:c.none,removeDocument:c.none,getDocumentList:c.update}}},d.default_action=c.none,d.getAction=function(a,b){varc,e,f;c=a.getCommand().getLabel(),e=b.getCommand().getLabel(),f=a.getStatus().getLabel()==="on going"?"on going":"not on going";try{returnconsole.log(c),console.log(e),console.log(f),d.action[c][f][e](a,b)}catch(g){if(g.name==="TypeError")returnd.default_action(a,b);throwg}},d.canCompare=function(a,b){varc=a.getCommand().getLabel(),e=b.getCommand().getLabel();try{returnd.compare[c][e](a,b)}catch(f){if(f.name==="TypeError")returnd.default_compare(a,b);throwf}},c.validateJobAccordingToJob=function(a,b){returnd.canCompare(a,b)?{action:d.getAction(a,b),job:a}:{action:d.default_action(a,b),job:a}},c}(),z={};a=a||{},c=c||{};varA={},B="jio/id_array";returnA.id=1,A.storage=k.storage(a,z),function(){vara,b=LocalOrCookieStorage.getItem(B)||[];for(a=0;a<b.length;a+=1)b[a]>=A.id&&(A.id=b[a]+1);b.push(A.id),LocalOrCookieStorage.setItem(B,b)}(),function(){u.setId(A.id),u.start(),x.setId(A.id),x.start()}(),z.start=function(){x.start()},z.stop=function(){x.stop()},z.getId=function(){returnA.id},z.validateStorageDescription=function(a){returnk.storage(a.type)(a).isValid()},z.saveDocument=function(a,b,c,d){c=c||{},c.onResponse=c.onResponse||function(){},c.onDone=c.onDone||function(){},c.onFail=c.onFail||function(){},c.max_retry=c.max_retry||0,x.addJob(s({storage:d?k.storage(d):A.storage,command:l({path:a,content:b,option:c})}))},z.loadDocument=function(a,b,c){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,b.metadata_only=b.metadata_only!==undefined?b.metadata_only:!1,x.addJob(s({storage:c?k.storage(c):A.storage,command:i({path:a,option:b})}))},z.removeDocument=function(a,b,c){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,x.addJob(s({storage:c?k.storage(c):A.storage,command:j({path:a,option:b})}))},z.getDocumentList=function(a,b,c){b=b||{},b.onResponse=b.onResponse||function(){},b.onDone=b.onDone||function(){},b.onFail=b.onFail||function(){},b.max_retry=b.max_retry||0,b.metadata_only=b.metadata_only!==undefined?b.metadata_only:!0,x.addJob(s({storage:c?k.storage(c):A.storage,command:h({path:a,option:b})}))},z},k=function(a,b){varc={};a=a||{},b=b||{};vare={base:h,handler:i};returnc.storage=function(a,b){a=a||{};varc=a.type||"base";if(!e[c])throwd({type:c,message:"Storage does not exists."});returne[c](a,b)},c.newJio=function(a){varb=a;returntypeofb=="string"&&(b=JSON.parse(b)),b=b||{type:"base"},j(a)},c.addStorageType=function(a,b){b=b||function(){returnnull};if(e[a])throwd({type:a,message:"Already known."});e[a]=b},c}();returnk}();