;LokiLocalStorageAdapter.prototype.deleteDatabase=functiondeleteDatabase(dbname,callback){if(localStorageAvailable()){localStorage.removeItem(dbname);callback(null)}else{callback(newError("localStorage is not available"))}};Loki.prototype.throttledSaveDrain=function(callback,options){varself=this;varnow=(newDate).getTime();if(!this.throttledSaves){callback(true)}options=options||{};if(!options.hasOwnProperty("recursiveWait")){options.recursiveWait=true}if(!options.hasOwnProperty("recursiveWaitLimit")){options.recursiveWaitLimit=false}if(!options.hasOwnProperty("recursiveWaitLimitDuration")){options.recursiveWaitLimitDuration=2e3}if(!options.hasOwnProperty("started")){options.started=(newDate).getTime()}if(this.throttledSaves&&this.throttledSavePending){if(options.recursiveWait){this.throttledCallbacks.push(function(){if(self.throttledSavePending){if(options.recursiveWaitLimit&&now-options.started>options.recursiveWaitLimitDuration){callback(false);return}self.throttledSaveDrain(callback,options);return}else{callback(true);return}})}else{this.throttledCallbacks.push(callback);return}}else{callback(true)}};Loki.prototype.loadDatabaseInternal=function(options,callback){varcFun=callback||function(err,data){if(err){throwerr}},self=this;if(this.persistenceAdapter!==null){this.persistenceAdapter.loadDatabase(this.filename,functionloadDatabaseCallback(dbString){if(typeofdbString==="string"){varparseSuccess=false;try{self.loadJSON(dbString,options||{});parseSuccess=true}catch(err){cFun(err)}if(parseSuccess){cFun(null);self.emit("loaded","database "+self.filename+" loaded")}}else{if(!dbString){cFun(null);self.emit("loaded","empty database "+self.filename+" loaded");return}if(dbStringinstanceofError){cFun(dbString);return}if(typeofdbString==="object"){self.loadJSONObject(dbString,options||{});cFun(null);self.emit("loaded","database "+self.filename+" loaded");return}cFun("unexpected adapter response : "+dbString)}})}else{cFun(newError("persistenceAdapter not configured"))}};Loki.prototype.loadDatabase=function(options,callback){varself=this;if(!this.throttledSaves){this.loadDatabaseInternal(options,callback);return}this.throttledSaveDrain(function(success){if(success){self.throttledSavePending=true;self.loadDatabaseInternal(options,function(err){if(self.throttledCallbacks.length===0){self.throttledSavePending=false}else{self.saveDatabase()}if(typeofcallback==="function"){callback(err)}});return}else{if(typeofcallback==="function"){callback(newError("Unable to pause save throttling long enough to read database"))}}},options)};Loki.prototype.saveDatabaseInternal=function(callback){varcFun=callback||function(err){if(err){throwerr}return},self=this;if(this.persistenceAdapter!==null){if(this.persistenceAdapter.mode==="reference"&&typeofthis.persistenceAdapter.exportDatabase==="function"){this.persistenceAdapter.exportDatabase(this.filename,this.copy({removeNonSerializable:true}),functionexportDatabaseCallback(err){self.autosaveClearFlags();cFun(err)})}else{self.autosaveClearFlags();this.persistenceAdapter.saveDatabase(this.filename,self.serialize(),functionsaveDatabasecallback(err){cFun(err)})}}else{cFun(newError("persistenceAdapter not configured"))}};Loki.prototype.saveDatabase=function(callback){if(!this.throttledSaves){this.saveDatabaseInternal(callback);return}if(this.throttledSavePending){this.throttledCallbacks.push(callback);return}varlocalCallbacks=this.throttledCallbacks;this.throttledCallbacks=[];localCallbacks.unshift(callback);this.throttledSavePending=true;varself=this;this.saveDatabaseInternal(function(err){self.throttledSavePending=false;localCallbacks.forEach(function(pcb){if(typeofpcb==="function"){setTimeout(function(){pcb(err)},1)}});if(self.throttledCallbacks.length>0){self.saveDatabase()}})};Loki.prototype.save=Loki.prototype.saveDatabase;Loki.prototype.deleteDatabase=function(options,callback){varcFun=callback||function(err,data){if(err){throwerr}};if(typeofoptions==="function"&&!callback){cFun=options}if(this.persistenceAdapter!==null){this.persistenceAdapter.deleteDatabase(this.fil
obj:op=="U"&&!this.disableDeltaChangesApi?this.getChangeDelta(obj,old):JSON.parse(JSON.stringify(obj))})};Collection.prototype.insertMeta=function(obj){varlen,idx;if(this.disableMeta||!obj){return}if(Array.isArray(obj)){len=obj.length;for(idx=0;idx<len;idx++){if(!obj[idx].hasOwnProperty("meta")){obj[idx].meta={}}obj[idx].meta.created=(newDate).getTime();obj[idx].meta.revision=0}return}if(!obj.meta){obj.meta={}}obj.meta.created=(newDate).getTime();obj.meta.revision=0};Collection.prototype.updateMeta=function(obj){if(this.disableMeta||!obj){return}obj.meta.updated=(newDate).getTime();obj.meta.revision+=1};Collection.prototype.createInsertChange=function(obj){this.createChange(this.name,"I",obj)};Collection.prototype.createUpdateChange=function(obj,old){this.createChange(this.name,"U",obj,old)};Collection.prototype.insertMetaWithChange=function(obj){this.insertMeta(obj);this.createInsertChange(obj)};Collection.prototype.updateMetaWithChange=function(obj,old){this.updateMeta(obj);this.createUpdateChange(obj,old)};Collection.prototype.console={log:function(){},warn:function(){},error:function(){}};Collection.prototype.addAutoUpdateObserver=function(object){if(!this.autoupdate||typeofObject.observe!=="function")return;Object.observe(object,this.observerCallback,["add","update","delete","reconfigure","setPrototype"])};Collection.prototype.removeAutoUpdateObserver=function(object){if(!this.autoupdate||typeofObject.observe!=="function")return;Object.unobserve(object,this.observerCallback)};Collection.prototype.addTransform=function(name,transform){if(this.transforms.hasOwnProperty(name)){thrownewError("a transform by that name already exists")}this.transforms[name]=transform};Collection.prototype.getTransform=function(name){returnthis.transforms[name]};Collection.prototype.setTransform=function(name,transform){this.transforms[name]=transform};Collection.prototype.removeTransform=function(name){deletethis.transforms[name]};Collection.prototype.byExample=function(template){vark,obj,query;query=[];for(kintemplate){if(!template.hasOwnProperty(k))continue;query.push((obj={},obj[k]=template[k],obj))}return{$and:query}};Collection.prototype.findObject=function(template){returnthis.findOne(this.byExample(template))};Collection.prototype.findObjects=function(template){returnthis.find(this.byExample(template))};Collection.prototype.ttlDaemonFuncGen=function(){varcollection=this;varage=this.ttl.age;returnfunctionttlDaemon(){varnow=Date.now();vartoRemove=collection.chain().where(functiondaemonFilter(member){vartimestamp=member.meta.updated||member.meta.created;vardiff=now-timestamp;returnage<diff});toRemove.remove()}};Collection.prototype.setTTL=function(age,interval){if(age<0){clearInterval(this.ttl.daemon)}else{this.ttl.age=age;this.ttl.ttlInterval=interval;this.ttl.daemon=setInterval(this.ttlDaemonFuncGen(),interval)}};Collection.prototype.prepareFullDocIndex=function(){varlen=this.data.length;varindexes=newArray(len);for(vari=0;i<len;i+=1){indexes[i]=i}returnindexes};Collection.prototype.configureOptions=function(options){options=options||{};if(options.hasOwnProperty("adaptiveBinaryIndices")){this.adaptiveBinaryIndices=options.adaptiveBinaryIndices;if(this.adaptiveBinaryIndices){this.ensureAllIndexes()}}};Collection.prototype.ensureIndex=function(property,force){if(typeofforce==="undefined"){force=false}if(property===null||property===undefined){thrownewError("Attempting to set index without an associated property")}if(this.binaryIndices[property]&&!force){if(!this.binaryIndices[property].dirty)return}if(this.adaptiveBinaryIndices===true&&this.binaryIndices.hasOwnProperty(property)&&!force){return}varindex={name:property,dirty:true,values:this.prepareFullDocIndex()};this.binaryIndices[property]=index;varwrappedComparer=function(prop,data){varval1,val2,arr;returnfunction(a,b){if(~prop.indexOf(".")){arr=prop.split(".");val1=arr.reduce(function(obj,i){returnobj&&obj[i]||undefined},data[a]);val2=arr.reduce(function(obj,i){returnobj&&obj[i]||undefined},data[b])}else{val1=data[a][prop];val2=data[b][prop]}