varjio=function(){"use strict";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){varb=function(a,b){vard={};a=a||{},b=b||{};vare={};returne.type=a.type||"",Object.defineProperty(d,"getType",{configurable:!1,enumerable:!1,writable:!1,value:function(){returne.type}}),d.execute=function(a){d.success=a.success,d.error=a.error,d.retry=a.retry,d.end=a.end,d.validate(a)&&a.executeOn(d)},d.isValid=function(){return!0},d.validate=function(){vara=d.validateState();returna?(d.error({status:0,statusText:"Invalid Storage",error:"invalid_storage",message:a,reason:a}),!1):!0},d.serialized=function(){return{type:d.getType()}},d.saveDocument=function(a){d.error({status:0,statusText:"Unknown storage",error:"unknown_storage",message:"Unknown Storage"})},d.loadDocument=function(a){d.saveDocument()},d.removeDocument=function(a){d.saveDocument()},d.getDocumentList=function(a){d.saveDocument()},d.validateState=function(){return""},d.success=function(){},d.retry=function(){},d.error=function(){},d.end=function(){},e.newCommand=function(a,d){vare=d||{};returne.label=a,c(e,b)},d.addJob=function(a,c,d,f,g,h){vari={options:f,callbacks:{success:g,error:h}};d&&(a==="get"?i.docid=d:i.doc=d),y.addJob(t({storage:b.storage(c||{}),command:e.newCommand(a,i)},b))},d},c=function(a,b){vard={};a=a||{},b=b||{};vare={};returne.commandlist={post:m,put:l,get:j,remove:k,allDocs:h},a.label&&e.commandlist[a.label]?(e.label=a.label,deletea.label,e.commandlist[e.label](a,b)):(e.tried=0,e.doc=a.doc||{},e.docid=a.docid||"",e.option=a.options||{},e.callbacks=a.callbacks||{},e.success=e.callbacks.success||function(){},e.error=e.callbacks.error||function(){},e.retry=function(){d.error({status:13,statusText:"Fail Retry",error:"fail_retry",message:"Impossible to retry.",reason:"Impossible to retry."})},e.end=function(){},e.on_going=!1,d.serialized=function(){return{label:d.getLabel(),tried:e.tried,doc:d.cloneDoc(),option:d.cloneOption()}},d.getLabel=function(){return"command"},d.getDocId=function(){returne.docid||e.doc._id},d.getDocContent=function(){returne.doc.content},d.getDocInfo=function(a){returne.doc[a]},d.getOption=function(a){returne.option[a]},d.validate=function(a){returnd.validateState()?a.validate():!1},d.validateState=function(){returntypeofe.doc!="object"?(d.error({status:20,statusText:"Document_Id Required",error:"document_id_required",message:"No document id.",reason:"no document id"}),!1):!0},d.canBeRetried=function(){returntypeofe.option.max_retry=="undefined"||e.option.max_retry===0||e.tried<e.option.max_retry},d.getTried=function(){returne.tried},d.execute=function(a){e.on_going||d.validate(a)&&(e.tried++,e.on_going=!0,a.execute(d))},d.executeOn=function(a){},d.success=function(a){e.on_going=!1,e.success(a),e.end(o())},d.retry=function(a){e.on_going=!1,d.canBeRetried()?e.retry():d.error(a)},d.error=function(a){e.on_going=!1,e.error(a),e.end(p())},d.end=function(){e.end(o())},d.onSuccessDo=function(a){if(a)e.success=a;elsereturne.success},d.onErrorDo=function(a){if(a)e.error=a;elsereturne.error},d.onEndDo=function(a){e.end=a},d.onRetryDo=function(a){e.retry=a},d.canBeRestored=function(){return!0},d.clone=function(){returnc(d.serialized(),b)},d.cloneOption=function(){vara,b={};for(aine.option)b[a]=e.option[a];returnb},d.cloneDoc=function(){if(e.docid)returne.docid;vara,b={};for(aine.doc)b[a]=e.doc[a];returnb},d)},h=function(a,b){vard=c(a,b);returna=a||{},b=b||{},d.getLabel=function(){return"allDocs"},d.executeOn=function(a){a.allDocs(d)},d.canBeRestored=function(){return!1},d},j=function(a,b){vard=c(a,b);returna=a||{},b=b||{},d.getLabel=function(){return"get"},d.validateState=function(){returnd.getDocId()?!0:(d.error({status:20,statusText:"Document Id Required",error:"document_id_required",message:"No document id.",reason:"no document id"}),!1)},d.executeOn=function(a){a.get(d)},d.canBeRestored=function(){return!1},d},k=function(a,b){vard=c(a,b);returna=a||{},b=b||{},d.getLabel=function(){return"remove"},d.executeOn=function(a){a.remove(d)},d},l=function(a,b){vard=c(a,b);a=a||{},b=b||{};vare={};d.getLabel=function(){return"put"};varf=d.validateState;returnd.validate=function(){returntypeofd.getDocInfo("content")!="string"?(d.error({status:21,statusText:"Content Required",error:"content_required",message:"No data to put.",reason:"no data to put"}),!1):f()},d.executeOn=function(a){a.put(d)},d},m=function(a,b){vard=c(a,b);a=a||{},b=b||{};vare={};d.getLabel=function(){return"post"};varf=d.validateState;returnd.validate=function(){returntypeofd.getDocInfo("content")!="string"?(d.error({status:21,statusText:"Content Required",error:"content_required",message:"No data to put.",reason:"no data to put"}),!1):f()},d.executeOn=function(a){a.put(d)},d},n=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.isWaitStatus=function(){return!1},c.isDone=function(){return!1},c},o=function(a,b){varc=n(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"done"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c.isDone=function(){return!0},c},p=function(a,b){varc=n(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"fail"},c.canStart=function(){return!1},c.canRestart=function(){return!0},c},q=function(a,b){varc=n(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"initial"},c.canStart=function(){return!0},c.canRestart=function(){return!0},c},r=function(a,b){varc=n(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"on going"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},s=function(a,b){varc=n(a,b);a=a||{},b=b||{};vard={};returnd.job_id_array=a.job_id_array||[],d.threshold=0,c.getLabel=function(){return"wait"},d.refreshJobIdArray=function(){vara=[],c;for(c=0;c<d.job_id_array.length;c+=1)b.jobManager.jobIdExists(d.job_id_array[c])&&a.push(d.job_id_array[c]);d.job_id_array=a},c.waitForJob=function(a){varb;for(b=0;b<d.job_id_array.length;b+=1)if(d.job_id_array[b]===a.getId())return;d.job_id_array.push(a.getId())},c.dontWaitForJob=function(a){varb,c=[];for(b=0;b<d.job_id_array.length;b+=1)d.job_id_array[b]!==a.getId()&&c.push(d.job_id_array[b]);d.job_id_array=c},c.waitForTime=function(a){d.threshold=Date.now()+a},c.stopWaitForTime=function(){d.threshold=0},c.canStart=function(){returnd.refreshJobIdArray(),d.job_id_array.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_array}},c.isWaitStatus=function(){return!0},c},t=function(a){varb={};a=a||{};varc={};c.id=x.nextId(),c.command=a.command,c.storage=a.storage,c.status=q(),c.date=newDate;if(!c.storage)throwg({job:b,message:"No storage set"});if(!c.command)throwg({job:b,message:"No command set"});returnb.getCommand=function(){returnc.command},b.getStatus=function(){returnc.status},b.getId=function(){returnc.id},b.getStorage=function(){returnc.storage},b.getDate=function(){returnc.date},b.isReady=function(){returnc.command.getTried()===0?c.status.canStart():c.status.canRestart()},b.serialized=function(){return{id:c.id,date:c.date.getTime(),status:c.status.serialized(),command:c.command.serialized(),storage:c.storage.serialized()}},b.waitForJob=function(a){c.status.getLabel()!=="wait"&&(c.status=s({})),c.status.waitForJob(a)},b.dontWaitFor=function(a){c.status.getLabel()==="wait"&&c.status.dontWaitForJob(a)},b.waitForTime=function(a){c.status.getLabel()!=="wait"&&(c.status=s({})),c.status.waitForTime(a)},b.stopWaitForTime=function(){c.status.getLabel()==="wait"&&c.status.stopWaitForTime()},b.eliminated=function(){c.command.error({status:10,statusText:"Stopped",error:"stopped",message:"This job has been stopped by another one.",reason:"this job has been stopped by another one"})},b.notAccepted=function(){c.command.onEndDo(function(){c.status=p(),y.terminateJob(b)}),c.command.error({status:11,statusText:"Not Accepted",error:"not_accepted",message:"This job is already running.",reason:"this job is already running"})},b.update=function(a){c.command.error({status:12,statusText:"Replaced",error:"replaced",message:"Job has been replaced by another one.",reason:"job has been replaced by another one"}),c.date=newDate(a.getDate().getTime()),c.command=a.getCommand(),c.status=a.getStatus()},b.execute=function(){if(!b.getCommand().canBeRetried())throwf({job:b,message:"The job was invoked too much time."});if(!b.isReady())throwe({job:b,message:"Can not execute this job."});c.status=r(),c.command.onRetryDo(function(){vara=c.command.getTried();a=a*a*200,a>1e4&&(a=1e4),b.waitForTime(a)}),c.command.onEndDo(function(a){c.status=a,y.terminateJob(b)}),c.command.execute(c.storage)},b},u=function(a,b){varc={};a=a||{},b=b||{};vard=[],e=a.name||"",f=a.announcer||{};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(){f.register(c)},c.unregister=function(){f.unregister(c)},c.trigger=function(a){varb;for(b=0;b<d.length;b++)d[b].apply(null,a)},c},v=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}(),w=function(a,b){varc={};a=a||{},b=b||{};vard={};returnc.register=function(a){d[a]||(d[a]=u())},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}(),x=function(a){varb={};a=a||{};varc=0;returnb.nextId=function(){returnc=c+1,c},b}(),y=function(a){varb={};a=a||{};vard="jio/job_array",e={};returne.id=a.id,e.interval_id=null,e.interval=200,e.job_array=[],e.getJobArrayName=function(){returnd+"/"+e.id},e.getJobArray=function(){returnLocalOrCookieStorage.getItem(e.getJobArrayName())||[]},e.copyJobArrayToLocal=function(){vara=[],b;for(b=0;b<e.job_array.length;b+=1)a.push(e.job_array[b].serialized());LocalOrCookieStorage.setItem(e.getJobArrayName(),a)},e.removeJob=function(a){varb,c=[];for(b=0;b<e.job_array.length;b+=1)e.job_array[b]!==a&&c.push(e.job_array[b]);e.job_array=c,e.copyJobArrayToLocal()},b.setId=function(a){e.id=a},b.start=function(){vara;e.interval_id===null&&(e.interval_id=setInterval(function(){e.restoreOldJio();for(a=0;a<e.job_array.length;a+=1)b.execute(e.job_array[a])},e.interval))},b.stop=function(){e.interval_id!==null&&(clearInterval(e.interval_id),e.interval_id=null,e.job_array.length===0&&LocalOrCookieStorage.deleteItem(e.getJobArrayName()))},e.restoreOldJio=function(){vara,b;e.lastrestore=e.lastrestore||0;if(e.lastrestore>Date.now()-2e3)return;b=LocalOrCookieStorage.getItem("jio/id_array")||[];for(a=0;a<b.length;a+=1)e.restoreOldJioId(b[a]);e.lastrestore=Date.now()},e.restoreOldJioId=function(a){varb;b=LocalOrCookieStorage.getItem("jio/id/"+a)||0,(newDate(b)).getTime()<Date.now()-1e4&&(e.restoreOldJobFromJioId(a),e.removeOldJioId(a),e.removeJobArrayFromJioId(a))},e.restoreOldJobFromJioId=function(a){vard,e;e=LocalOrCookieStorage.getItem("jio/job_array/"+a)||[];for(d=0;d<e.length;d+=1){varf=c(e[d].command);f.canBeRestored()&&b.addJob(t({storage:b.storage(e[d].storage),command:f}))}},e.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),LocalOrCookieStorage.deleteItem("jio/id/"+a)},e.removeJobArrayFromJioId=function(a){LocalOrCookieStorage.deleteItem("jio/job_array/"+a)},b.execute=function(a){try{a.execute()}catch(b){switch(b.name){case"jobNotReadyException":break;case"tooMuchTriesJobException":break;default:throwb}}e.copyJobArrayToLocal()},b.jobIdExists=function(a){varb;for(b=0;b<e.job_array.length;b+=1)if(e.job_array[b].getId()===a)return!0;return!1},b.terminateJob=function(a){e.removeJob(a)},b.addJob=function(a){varc=b.validateJobAccordingToJobList(e.job_array,a);e.appendJob(a,c)},b.validateJobAccordingToJobList=function(a,b){varc,d=[];for(c=0;c<a.length;c+=1)d.push(z.validateJobAccordingToJob(a[c],b));returnd},e.appendJob=function(a,b){varc;if(e.job_array.length!==b.length)thrownewRangeError("Array out of bound");for(c=0;c<b.length;c+=1)if(b[c].action==="dont accept")returna.notAccepted();for(c=0;c<b.length;c+=1)switch(b[c].action){case"eliminate":b[c].job.eliminated(),e.removeJob(b[c].job);break;case"update":b[c].job.update(a),e.copyJobArrayToLocal();return;case"wait":a.waitForJob(b[c].job);break;default:}e.job_array.push(a),e.copyJobArrayToLocal()},b.serialized=function(){vara=[],b,c=e.job_array||[];for(b=0;b<c.length;b+=1)a.push(c[b].serialized());returna},b}(),z=function(a){varb={},c={};returnc.compare={},c.action={},Object.defineProperty(b,"eliminate",{configurable:!1,enumerable:!1,writable:!1,value:function(){return"eliminate"}}),Object.defineProperty(b,"update",{configurable:!1,enumerable:!1,writable:!1,value:function(){return"update"}}),Object.defineProperty(b,"dontAccept",{configurable:!1,enumerable:!1,writable:!1,value:function(){return"dont accept"}}),Object.defineProperty(b,"wait",{configurable:!1,enumerable:!1,writable:!1,value:function(){return"wait"}}),Object.defineProperty(b,"none",{configurable:!1,enumerable:!1,writable:!1,value:function(){return"none"}}),b.default_action=b.none,b.default_compare=function(a,b){returna.getCommand().getDocId()===b.getCommand().getDocId()&&a.getCommand().getDocInfo("_rev")===b.getCommand().getDocInfo("_rev")&&a.getCommand().getOption("rev")===b.getCommand().getOption("rev")&&JSON.stringify(a.getStorage().serialized())===JSON.stringify(b.getStorage().serialized())},c.getAction=function(a,d){vare,f,g;returne=a.getCommand().getLabel(),f=d.getCommand().getLabel(),g=a.getStatus().getLabel()==="on going"?"on going":"not on going",c.action[e]&&c.action[e][g]&&c.action[e][g][f]?c.action[e][g][f](a,d):b.default_action(a,d)},c.canCompare=function(a,d){vare=a.getCommand().getLabel(),f=d.getCommand().getLabel();returnc.compare[e]&&c.compare[f]?c.compare[e][f](a,d):b.default_compare(a,d)},Object.defineProperty(b,"validateJobAccordingToJob",{configurable:!1,enumerable:!1,writable:!1,value:function(a,d){returnc.canCompare(a,d)?{action:c.getAction(a,d),job:a}:{action:b.default_action(a,d),job:a}}}),Object.defineProperty(b,"addActionRule",{configurable:!1,enumerable:!1,writable:!1,value:function(a,b,d,e){varf=b?"on going":"not on going";c.action[a]=c.action[a]||{},c.action[a][f]=c.action[a][f]||{},c.action[a][f][d]=e}}),Object.defineProperty(b,"addCompareRule",{configurable:!1,enumerable:!1,writable:!1,value:function(a,b,d){c.compare[a]=c.compare[a]||{},c.compare[a][b]=d}}),b.addActionRule("put",!0,"put",function(a,c){returna.getCommand().getDocInfo("content")===c.getCommand().getDocInfo("content")?b.dontAccept():b.wait()}),b.addActionRule("put",!0,"get",b.wait),b.addActionRule("put",!0,"remove",b.wait),b.addActionRule("put",!1,"put",b.update),b.addActionRule("put",!1,"get",b.wait),b.addActionRule("put",!1,"remove",b.eliminate),b.addActionRule("get",!0,"put",b.wait),b.addActionRule("get",!0,"get",b.dontAccept),b.addActionRule("get",!0,"remove",b.wait),b.addActionRule("get",!1,"put",b.wait),b.addActionRule("get",!1,"get",b.update),b.addActionRule("get",!1,"remove",b.wait),b.addActionRule("remove",!0,"get",b.dontAccept),b.addActionRule("remove",!0,"remove",b.dontAccept),b.addActionRule("remove",!1,"put",b.eliminate),b.addActionRule("remove",!1,"get",b.dontAccept),b.addActionRule("remove",!1,"remove",b.update),b.addActionRule("allDocs",!0,"allDocs",b.dontAccept),b.addActionRule("allDocs",!1,"allDocs",b.update),b}(),A={},B={};a=a||{};varC="jio/id_array";returnB.id=null,B.storage_spec=a,B.init=function(){if(B.id===null){vara,b=LocalOrCookieStorage.getItem(C)||[];B.id=1;for(a=0;a<b.length;a+=1)b[a]>=B.id&&(B.id=b[a]+1);b.push(B.id),LocalOrCookieStorage.setItem(C,b),v.setId(B.id),y.setId(B.id)}},Object.defineProperty(A,"storage",{configurable:!1,enumerable:!1,writable:!1,value:function(a,c,e){a=a||{},c=c||{},c.basicStorage=b,c.storage=A.storage;varf=e||a.type||"base";if(f==="base")returnb(a,c);if(!i[f])throwd({type:f,message:"Storage does not exists."});returni[f](a,c)}}),y.storage=A.storage,Object.defineProperty(A,"start",{configurable:!1,enumerable:!1,writable:!1,value:function(){B.init(),v.start(),y.start()}}),Object.defineProperty(A,"stop",{configurable:!1,enumerable:!1,writable:!1,value:function(){y.stop()}}),Object.defineProperty(A,"close",{configurable:!1,enumerable:!1,writable:!1,value:function(){v.stop(),y.stop(),B.id=null}}),Object.defineProperty(A,"getId",{configurable:!1,enumerable:!1,writable:!1,value:function(){returnB.id}}),Object.defineProperty(A,"getJobRules",{configurable:!1,enumerable:!1,writable:!1,value:function(){returnz}}),Object.defineProperty(A,"validateStorageDescription",{configurable:!1,enumerable:!1,writable:!1,value:function(a){returnA.storage(a).isValid()}}),Object.defineProperty(A,"getJobArray",{configurable:!1,enumerable:!1,writable:!1,value:function(){returny.serialized()}}),B.getParam=function(a,b){varc={},d=0;returnb||(c.doc=a[d],d++),typeofa[d]=="object"?(c.options=a[d],d++):c.options={},c.callback=function(a,b){},c.success=function(a){c.callback(undefined,a)},c.error=function(a){c.callback(a,undefined)},typeofa[d]=="function"&&(typeofa[d+1]=="function"?(c.success=a[d],c.error=a[d+1]):c.callback=a[d]),c},B.addJob=function(a,b){y.addJob(t({storage:A.storage(B.storage_spec),command:a(b)}))},Object.defineProperty(A,"post",{configurable:!1,enumerable:!1,writable:!1,value:function(){vara=B.getParam(arguments);a.options.max_retry=a.options.max_retry||0,B.addJob(m,{doc:a.doc,options:a.options,callbacks:{success:a.success,error:a.error}})}}),Object.defineProperty(A,"put",{configurable:!1,enumerable:!1,writable:!1,value:function(){vara=B.getParam(arguments);a.options.max_retry=a.options.max_retry||0,B.addJob(l,{doc:a.doc,options:a.options,callbacks:{success:a.success,error:a.error}})}}),Object.defineProperty(A,"get",{configurable:!1,enumerable:!1,writable:!1,value:function(){vara=B.getParam(arguments);a.options.max_retry=a.options.max_retry||3,a.options.metadata_only=a.options.metadata_only!==undefined?a.options.metadata_only:!1,B.addJob(j,{docid:a.doc,options:a.options,callbacks:{success:a.success,error:a.error}})}}),Object.defineProperty(A,"remove",{configurable:!1,enumerable:!1,writable:!1,value:function(){vara=B.getParam(arguments);a.options.max_retry=a.options.max_retry||0,B.addJob(k,{doc:a.doc,options:a.options,callbacks:{success:a.success,error:a.error}})}}),Object.defineProperty(A,"allDocs",{configurable:!1,enumerable:!1,writable:!1,value:function(){vara=B.getParam(arguments,"no doc");a.options.max_retry=a.options.max_retry||3,a.options.metadata_only=a.options.metadata_only!==undefined?a.options.metadata_only:!0,B.addJob(h,{options:a.options,callbacks:{success:a.success,error:a.error}})}}),A},i={base:function(){}},j=function(a){varb={};returna=a||{},Object.defineProperty(b,"newJio",{configurable:!1,enumerable:!1,writable:!1,value:function(a){varb=a,c=null;returntypeofb=="string"&&(b=JSON.parse(b)),b=b||{type:"base"},c=h(a),c.start(),c}}),Object.defineProperty(b,"addStorageType",{configurable:!1,enumerable:!1,writable:!1,value:function(a,b){b=b||function(){returnnull};if(i[a])throwd({type:a,message:"Already known."});i[a]=b}}),b}();returnj}();
varjio=function(){"use strict";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){varb=function(a,b){vard={};a=a||{},b=b||{};vare={};returne.type=a.type||"",Object.defineProperty(d,"getType",{configurable:!1,enumerable:!1,writable:!1,value:function(){returne.type}}),d.execute=function(a){d.success=a.success,d.error=a.error,d.retry=a.retry,d.end=a.end,d.validate(a)&&a.executeOn(d)},d.isValid=function(){return!0},d.validate=function(){vara=d.validateState();returna?(d.error({status:0,statusText:"Invalid Storage",error:"invalid_storage",message:a,reason:a}),!1):!0},d.serialized=function(){return{type:d.getType()}},d.saveDocument=function(a){d.error({status:0,statusText:"Unknown storage",error:"unknown_storage",message:"Unknown Storage"})},d.loadDocument=function(a){d.saveDocument()},d.removeDocument=function(a){d.saveDocument()},d.getDocumentList=function(a){d.saveDocument()},d.validateState=function(){return""},d.success=function(){},d.retry=function(){},d.error=function(){},d.end=function(){},e.newCommand=function(a,d){vare=d||{};returne.label=a,c(e,b)},d.addJob=function(a,c,d,f,g,h){vari={options:f,callbacks:{success:g,error:h}};d&&(a==="get"?i.docid=d:i.doc=d),y.addJob(t({storage:b.storage(c||{}),command:e.newCommand(a,i)},b))},d},c=function(a,b){vard={};a=a||{},b=b||{};vare={};returne.commandlist={post:m,put:l,get:j,remove:k,allDocs:h},a.label&&e.commandlist[a.label]?(e.label=a.label,deletea.label,e.commandlist[e.label](a,b)):(e.tried=0,e.doc=a.doc||{},e.docid=a.docid||"",e.option=a.options||{},e.callbacks=a.callbacks||{},e.success=e.callbacks.success||function(){},e.error=e.callbacks.error||function(){},e.retry=function(){d.error({status:13,statusText:"Fail Retry",error:"fail_retry",message:"Impossible to retry.",reason:"Impossible to retry."})},e.end=function(){},e.on_going=!1,d.serialized=function(){return{label:d.getLabel(),tried:e.tried,doc:d.cloneDoc(),option:d.cloneOption()}},d.getLabel=function(){return"command"},d.getDocId=function(){returne.docid||e.doc._id},d.getDocContent=function(){returne.doc.content},d.getDocInfo=function(a){returne.doc[a]},d.getOption=function(a){returne.option[a]},d.validate=function(a){returnd.validateState()?a.validate():!1},d.validateState=function(){returntypeofe.doc!="object"?(d.error({status:20,statusText:"Document_Id Required",error:"document_id_required",message:"No document id.",reason:"no document id"}),!1):!0},d.canBeRetried=function(){returntypeofe.option.max_retry=="undefined"||e.option.max_retry===0||e.tried<e.option.max_retry},d.getTried=function(){returne.tried},d.execute=function(a){e.on_going||d.validate(a)&&(e.tried++,e.on_going=!0,a.execute(d))},d.executeOn=function(a){},d.success=function(a){e.on_going=!1,e.success(a),e.end(o())},d.retry=function(a){e.on_going=!1,d.canBeRetried()?e.retry():d.error(a)},d.error=function(a){e.on_going=!1,e.error(a),e.end(p())},d.end=function(){e.end(o())},d.onSuccessDo=function(a){if(a)e.success=a;elsereturne.success},d.onErrorDo=function(a){if(a)e.error=a;elsereturne.error},d.onEndDo=function(a){e.end=a},d.onRetryDo=function(a){e.retry=a},d.canBeRestored=function(){return!0},d.clone=function(){returnc(d.serialized(),b)},d.cloneOption=function(){vara,b={};for(aine.option)b[a]=e.option[a];returnb},d.cloneDoc=function(){if(e.docid)returne.docid;vara,b={};for(aine.doc)b[a]=e.doc[a];returnb},d)},h=function(a,b){vard=c(a,b);returna=a||{},b=b||{},d.getLabel=function(){return"allDocs"},d.executeOn=function(a){a.allDocs(d)},d.canBeRestored=function(){return!1},d},j=function(a,b){vard=c(a,b);returna=a||{},b=b||{},d.getLabel=function(){return"get"},d.validateState=function(){returnd.getDocId()?!0:(d.error({status:20,statusText:"Document Id Required",error:"document_id_required",message:"No document id.",reason:"no document id"}),!1)},d.executeOn=function(a){a.get(d)},d.canBeRestored=function(){return!1},d},k=function(a,b){vard=c(a,b);returna=a||{},b=b||{},d.getLabel=function(){return"remove"},d.executeOn=function(a){a.remove(d)},d},l=function(a,b){vard=c(a,b);a=a||{},b=b||{};vare={};d.getLabel=function(){return"put"};varf=d.validateState;returnd.validate=function(){returntypeofd.getDocInfo("content")!="string"?(d.error({status:21,statusText:"Content Required",error:"content_required",message:"No data to put.",reason:"no data to put"}),!1):f()},d.executeOn=function(a){a.put(d)},d},m=function(a,b){vard=c(a,b);a=a||{},b=b||{};vare={};d.getLabel=function(){return"post"};varf=d.validateState;returnd.validate=function(){returntypeofd.getDocInfo("content")!="string"?(d.error({status:21,statusText:"Content Required",error:"content_required",message:"No data to put.",reason:"no data to put"}),!1):f()},d.executeOn=function(a){a.put(d)},d},n=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.isWaitStatus=function(){return!1},c.isDone=function(){return!1},c},o=function(a,b){varc=n(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"done"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c.isDone=function(){return!0},c},p=function(a,b){varc=n(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"fail"},c.canStart=function(){return!1},c.canRestart=function(){return!0},c},q=function(a,b){varc=n(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"initial"},c.canStart=function(){return!0},c.canRestart=function(){return!0},c},r=function(a,b){varc=n(a,b);returna=a||{},b=b||{},c.getLabel=function(){return"on going"},c.canStart=function(){return!1},c.canRestart=function(){return!1},c},s=function(a,b){varc=n(a,b);a=a||{},b=b||{};vard={};returnd.job_id_array=a.job_id_array||[],d.threshold=0,c.getLabel=function(){return"wait"},d.refreshJobIdArray=function(){vara=[],b;for(b=0;b<d.job_id_array.length;b+=1)y.jobIdExists(d.job_id_array[b])&&a.push(d.job_id_array[b]);d.job_id_array=a},c.waitForJob=function(a){varb;for(b=0;b<d.job_id_array.length;b+=1)if(d.job_id_array[b]===a.getId())return;d.job_id_array.push(a.getId())},c.dontWaitForJob=function(a){varb,c=[];for(b=0;b<d.job_id_array.length;b+=1)d.job_id_array[b]!==a.getId()&&c.push(d.job_id_array[b]);d.job_id_array=c},c.waitForTime=function(a){d.threshold=Date.now()+a},c.stopWaitForTime=function(){d.threshold=0},c.canStart=function(){returnd.refreshJobIdArray(),d.job_id_array.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_array}},c.isWaitStatus=function(){return!0},c},t=function(a){varb={};a=a||{};varc={};c.id=x.nextId(),c.command=a.command,c.storage=a.storage,c.status=q(),c.date=newDate;if(!c.storage)throwg({job:b,message:"No storage set"});if(!c.command)throwg({job:b,message:"No command set"});returnb.getCommand=function(){returnc.command},b.getStatus=function(){returnc.status},b.getId=function(){returnc.id},b.getStorage=function(){returnc.storage},b.getDate=function(){returnc.date},b.isReady=function(){returnc.command.getTried()===0?c.status.canStart():c.status.canRestart()},b.serialized=function(){return{id:c.id,date:c.date.getTime(),status:c.status.serialized(),command:c.command.serialized(),storage:c.storage.serialized()}},b.waitForJob=function(a){c.status.getLabel()!=="wait"&&(c.status=s({})),c.status.waitForJob(a)},b.dontWaitFor=function(a){c.status.getLabel()==="wait"&&c.status.dontWaitForJob(a)},b.waitForTime=function(a){c.status.getLabel()!=="wait"&&(c.status=s({})),c.status.waitForTime(a)},b.stopWaitForTime=function(){c.status.getLabel()==="wait"&&c.status.stopWaitForTime()},b.eliminated=function(){c.command.error({status:10,statusText:"Stopped",error:"stopped",message:"This job has been stopped by another one.",reason:"this job has been stopped by another one"})},b.notAccepted=function(){c.command.onEndDo(function(){c.status=p(),y.terminateJob(b)}),c.command.error({status:11,statusText:"Not Accepted",error:"not_accepted",message:"This job is already running.",reason:"this job is already running"})},b.update=function(a){c.command.error({status:12,statusText:"Replaced",error:"replaced",message:"Job has been replaced by another one.",reason:"job has been replaced by another one"}),c.date=newDate(a.getDate().getTime()),c.command=a.getCommand(),c.status=a.getStatus()},b.execute=function(){if(!b.getCommand().canBeRetried())throwf({job:b,message:"The job was invoked too much time."});if(!b.isReady())throwe({job:b,message:"Can not execute this job."});c.status=r(),c.command.onRetryDo(function(){vara=c.command.getTried();a=a*a*200,a>1e4&&(a=1e4),b.waitForTime(a)}),c.command.onEndDo(function(a){c.status=a,y.terminateJob(b)}),c.command.execute(c.storage)},b},u=function(a,b){varc={};a=a||{},b=b||{};vard=[],e=a.name||"",f=a.announcer||{};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(){f.register(c)},c.unregister=function(){f.unregister(c)},c.trigger=function(a){varb;for(b=0;b<d.length;b++)d[b].apply(null,a)},c},v=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}(),w=function(a,b){varc={};a=a||{},b=b||{};vard={};returnc.register=function(a){d[a]||(d[a]=u())},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}(),x=function(a){varb={};a=a||{};varc=0;returnb.nextId=function(){returnc=c+1,c},b}(),y=function(a){varb={};a=a||{};vard="jio/job_array",e={};returne.id=a.id,e.interval_id=null,e.interval=200,e.job_array=[],e.getJobArrayName=function(){returnd+"/"+e.id},e.getJobArray=function(){returnLocalOrCookieStorage.getItem(e.getJobArrayName())||[]},e.copyJobArrayToLocal=function(){vara=[],b;for(b=0;b<e.job_array.length;b+=1)a.push(e.job_array[b].serialized());LocalOrCookieStorage.setItem(e.getJobArrayName(),a)},e.removeJob=function(a){varb,c=[];for(b=0;b<e.job_array.length;b+=1)e.job_array[b]!==a&&c.push(e.job_array[b]);e.job_array=c,e.copyJobArrayToLocal()},b.setId=function(a){e.id=a},b.start=function(){vara;e.interval_id===null&&(e.interval_id=setInterval(function(){e.restoreOldJio();for(a=0;a<e.job_array.length;a+=1)b.execute(e.job_array[a])},e.interval))},b.stop=function(){e.interval_id!==null&&(clearInterval(e.interval_id),e.interval_id=null,e.job_array.length===0&&LocalOrCookieStorage.deleteItem(e.getJobArrayName()))},e.restoreOldJio=function(){vara,b;e.lastrestore=e.lastrestore||0;if(e.lastrestore>Date.now()-2e3)return;b=LocalOrCookieStorage.getItem("jio/id_array")||[];for(a=0;a<b.length;a+=1)e.restoreOldJioId(b[a]);e.lastrestore=Date.now()},e.restoreOldJioId=function(a){varb;b=LocalOrCookieStorage.getItem("jio/id/"+a)||0,(newDate(b)).getTime()<Date.now()-1e4&&(e.restoreOldJobFromJioId(a),e.removeOldJioId(a),e.removeJobArrayFromJioId(a))},e.restoreOldJobFromJioId=function(a){vard,e;e=LocalOrCookieStorage.getItem("jio/job_array/"+a)||[];for(d=0;d<e.length;d+=1){varf=c(e[d].command);f.canBeRestored()&&b.addJob(t({storage:b.storage(e[d].storage),command:f}))}},e.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),LocalOrCookieStorage.deleteItem("jio/id/"+a)},e.removeJobArrayFromJioId=function(a){LocalOrCookieStorage.deleteItem("jio/job_array/"+a)},b.execute=function(a){try{a.execute()}catch(b){switch(b.name){case"jobNotReadyException":break;case"tooMuchTriesJobException":break;default:throwb}}e.copyJobArrayToLocal()},b.jobIdExists=function(a){varb;for(b=0;b<e.job_array.length;b+=1)if(e.job_array[b].getId()===a)return!0;return!1},b.terminateJob=function(a){e.removeJob(a)},b.addJob=function(a){varc=b.validateJobAccordingToJobList(e.job_array,a);e.appendJob(a,c)},b.validateJobAccordingToJobList=function(a,b){varc,d=[];for(c=0;c<a.length;c+=1)d.push(z.validateJobAccordingToJob(a[c],b));returnd},e.appendJob=function(a,b){varc;if(e.job_array.length!==b.length)thrownewRangeError("Array out of bound");for(c=0;c<b.length;c+=1)if(b[c].action==="dont accept")returna.notAccepted();for(c=0;c<b.length;c+=1)switch(b[c].action){case"eliminate":b[c].job.eliminated(),e.removeJob(b[c].job);break;case"update":b[c].job.update(a),e.copyJobArrayToLocal();return;case"wait":a.waitForJob(b[c].job);break;default:}e.job_array.push(a),e.copyJobArrayToLocal()},b.serialized=function(){vara=[],b,c=e.job_array||[];for(b=0;b<c.length;b+=1)a.push(c[b].serialized());returna},b}(),z=function(a){varb={},c={};returnc.compare={},c.action={},Object.defineProperty(b,"eliminate",{configurable:!1,enumerable:!1,writable:!1,value:function(){return"eliminate"}}),Object.defineProperty(b,"update",{configurable:!1,enumerable:!1,writable:!1,value:function(){return"update"}}),Object.defineProperty(b,"dontAccept",{configurable:!1,enumerable:!1,writable:!1,value:function(){return"dont accept"}}),Object.defineProperty(b,"wait",{configurable:!1,enumerable:!1,writable:!1,value:function(){return"wait"}}),Object.defineProperty(b,"none",{configurable:!1,enumerable:!1,writable:!1,value:function(){return"none"}}),b.default_action=b.none,b.default_compare=function(a,b){returna.getCommand().getDocId()===b.getCommand().getDocId()&&a.getCommand().getDocInfo("_rev")===b.getCommand().getDocInfo("_rev")&&a.getCommand().getOption("rev")===b.getCommand().getOption("rev")&&JSON.stringify(a.getStorage().serialized())===JSON.stringify(b.getStorage().serialized())},c.getAction=function(a,d){vare,f,g;returne=a.getCommand().getLabel(),f=d.getCommand().getLabel(),g=a.getStatus().getLabel()==="on going"?"on going":"not on going",c.action[e]&&c.action[e][g]&&c.action[e][g][f]?c.action[e][g][f](a,d):b.default_action(a,d)},c.canCompare=function(a,d){vare=a.getCommand().getLabel(),f=d.getCommand().getLabel();returnc.compare[e]&&c.compare[f]?c.compare[e][f](a,d):b.default_compare(a,d)},Object.defineProperty(b,"validateJobAccordingToJob",{configurable:!1,enumerable:!1,writable:!1,value:function(a,d){returnc.canCompare(a,d)?{action:c.getAction(a,d),job:a}:{action:b.default_action(a,d),job:a}}}),Object.defineProperty(b,"addActionRule",{configurable:!1,enumerable:!1,writable:!1,value:function(a,b,d,e){varf=b?"on going":"not on going";c.action[a]=c.action[a]||{},c.action[a][f]=c.action[a][f]||{},c.action[a][f][d]=e}}),Object.defineProperty(b,"addCompareRule",{configurable:!1,enumerable:!1,writable:!1,value:function(a,b,d){c.compare[a]=c.compare[a]||{},c.compare[a][b]=d}}),b.addActionRule("put",!0,"put",function(a,c){returna.getCommand().getDocInfo("content")===c.getCommand().getDocInfo("content")?b.dontAccept():b.wait()}),b.addActionRule("put",!0,"get",b.wait),b.addActionRule("put",!0,"remove",b.wait),b.addActionRule("put",!1,"put",b.update),b.addActionRule("put",!1,"get",b.wait),b.addActionRule("put",!1,"remove",b.eliminate),b.addActionRule("get",!0,"put",b.wait),b.addActionRule("get",!0,"get",b.dontAccept),b.addActionRule("get",!0,"remove",b.wait),b.addActionRule("get",!1,"put",b.wait),b.addActionRule("get",!1,"get",b.update),b.addActionRule("get",!1,"remove",b.wait),b.addActionRule("remove",!0,"get",b.dontAccept),b.addActionRule("remove",!0,"remove",b.dontAccept),b.addActionRule("remove",!1,"put",b.eliminate),b.addActionRule("remove",!1,"get",b.dontAccept),b.addActionRule("remove",!1,"remove",b.update),b.addActionRule("allDocs",!0,"allDocs",b.dontAccept),b.addActionRule("allDocs",!1,"allDocs",b.update),b}(),A={},B={};a=a||{};varC="jio/id_array";returnB.id=null,B.storage_spec=a,B.init=function(){if(B.id===null){vara,b=LocalOrCookieStorage.getItem(C)||[];B.id=1;for(a=0;a<b.length;a+=1)b[a]>=B.id&&(B.id=b[a]+1);b.push(B.id),LocalOrCookieStorage.setItem(C,b),v.setId(B.id),y.setId(B.id)}},Object.defineProperty(A,"storage",{configurable:!1,enumerable:!1,writable:!1,value:function(a,c,e){a=a||{},c=c||{},c.basicStorage=b,c.storage=A.storage;varf=e||a.type||"base";if(f==="base")returnb(a,c);if(!i[f])throwd({type:f,message:"Storage does not exists."});returni[f](a,c)}}),y.storage=A.storage,Object.defineProperty(A,"start",{configurable:!1,enumerable:!1,writable:!1,value:function(){B.init(),v.start(),y.start()}}),Object.defineProperty(A,"stop",{configurable:!1,enumerable:!1,writable:!1,value:function(){y.stop()}}),Object.defineProperty(A,"close",{configurable:!1,enumerable:!1,writable:!1,value:function(){v.stop(),y.stop(),B.id=null}}),Object.defineProperty(A,"getId",{configurable:!1,enumerable:!1,writable:!1,value:function(){returnB.id}}),Object.defineProperty(A,"getJobRules",{configurable:!1,enumerable:!1,writable:!1,value:function(){returnz}}),Object.defineProperty(A,"validateStorageDescription",{configurable:!1,enumerable:!1,writable:!1,value:function(a){returnA.storage(a).isValid()}}),Object.defineProperty(A,"getJobArray",{configurable:!1,enumerable:!1,writable:!1,value:function(){returny.serialized()}}),B.getParam=function(a,b){varc={},d=0;returnb||(c.doc=a[d],d++),typeofa[d]=="object"?(c.options=a[d],d++):c.options={},c.callback=function(a,b){},c.success=function(a){c.callback(undefined,a)},c.error=function(a){c.callback(a,undefined)},typeofa[d]=="function"&&(typeofa[d+1]=="function"?(c.success=a[d],c.error=a[d+1]):c.callback=a[d]),c},B.addJob=function(a,b){y.addJob(t({storage:A.storage(B.storage_spec),command:a(b)}))},Object.defineProperty(A,"post",{configurable:!1,enumerable:!1,writable:!1,value:function(){vara=B.getParam(arguments);a.options.max_retry=a.options.max_retry||0,B.addJob(m,{doc:a.doc,options:a.options,callbacks:{success:a.success,error:a.error}})}}),Object.defineProperty(A,"put",{configurable:!1,enumerable:!1,writable:!1,value:function(){vara=B.getParam(arguments);a.options.max_retry=a.options.max_retry||0,B.addJob(l,{doc:a.doc,options:a.options,callbacks:{success:a.success,error:a.error}})}}),Object.defineProperty(A,"get",{configurable:!1,enumerable:!1,writable:!1,value:function(){vara=B.getParam(arguments);a.options.max_retry=a.options.max_retry||3,a.options.metadata_only=a.options.metadata_only!==undefined?a.options.metadata_only:!1,B.addJob(j,{docid:a.doc,options:a.options,callbacks:{success:a.success,error:a.error}})}}),Object.defineProperty(A,"remove",{configurable:!1,enumerable:!1,writable:!1,value:function(){vara=B.getParam(arguments);a.options.max_retry=a.options.max_retry||0,B.addJob(k,{doc:a.doc,options:a.options,callbacks:{success:a.success,error:a.error}})}}),Object.defineProperty(A,"allDocs",{configurable:!1,enumerable:!1,writable:!1,value:function(){vara=B.getParam(arguments,"no doc");a.options.max_retry=a.options.max_retry||3,a.options.metadata_only=a.options.metadata_only!==undefined?a.options.metadata_only:!0,B.addJob(h,{options:a.options,callbacks:{success:a.success,error:a.error}})}}),A},i={base:function(){}},j=function(a){varb={};returna=a||{},Object.defineProperty(b,"newJio",{configurable:!1,enumerable:!1,writable:!1,value:function(a){varb=a,c=null;returntypeofb=="string"&&(b=JSON.parse(b)),b=b||{type:"base"},c=h(a),c.start(),c}}),Object.defineProperty(b,"addStorageType",{configurable:!1,enumerable:!1,writable:!1,value:function(a,b){b=b||function(){returnnull};if(i[a])throwd({type:a,message:"Already known."});i[a]=b}}),b}();returnj}();