2 lines
7.1 KiB
JavaScript
2 lines
7.1 KiB
JavaScript
|
define(["./createTaskProcessorWorker","./when-b60132fc"],(function(e,t){"use strict";function r(e){return null!=e}function n(e){var t;this.name="DeveloperError",this.message=e;try{throw new Error}catch(e){t=e.stack}this.stack=t}r(Object.create)&&(n.prototype=Object.create(Error.prototype),n.prototype.constructor=n),n.prototype.toString=function(){var e=this.name+": "+this.message;return r(this.stack)&&(e+="\n"+this.stack.toString()),e},n.throwInstantiationError=function(){throw new n("This function defines an interface and should not be called directly.")};var c=1,o=2,a=3;function s(e){if(!r(e.name))throw new n("options.name is required.");var c=t.when.defer();this.dbname=e.name;var o=indexedDB.open(this.dbname),a=this;return o.onsuccess=function(e){a.db=e.target.result,a.version=a.db.version,r(a.cachestatus)||(a.cachestatus={}),c.resolve(a)},o.onupgradeneeded=function(e){a.db=e.target.result,a.version=a.db.version,c.resolve(a)},o.onerror=function(e){a.db=null,c.reject("create database fail, error code : "+e.target.errorcode)},this.layer=e.layer||null,this.storageType=e.storageType||"arrayBuffer",this.creatingTable=!1,this.cachestatus={},c.promise}s.prototype.checkObjectStoreExit=function(e){return!!r(this.db)&&this.db.objectStoreNames.contains(e)},s.prototype.createObjectStore=function(e){var n=t.when.defer();if(this.creatingTable)n.reject(!1);else{if(this.db.objectStoreNames.contains(e))return n.reject(!1),n.promise;this.creatingTable=!0;var c=this,o=parseInt(c.db.version);c.db.close();var a=indexedDB.open(c.dbname,o+1);a.onupgradeneeded=function(t){var o=t.target.result;c.db=o;var a=o.createObjectStore(e,{keyPath:"id"});r(a)?(a.createIndex("value","value",{unique:!1}),c.creatingTable=!1,r(c.cachestatus)||(c.cachestatus={}),c.cachestatus[e]={},c.db.close(),indexedDB.open(c.dbname).onsuccess=function(e){var t=e.target.result;c.db=t,n.resolve(!0)}):(c.creatingTable=!1,n.reject(!1))},a.onsuccess=function(e){e.target.result.close(),n.resolve(!0)},a.onerror=function(e){c.creatingTable=!1,n.reject(!1)}}return n.promise},s.prototype.putElementInDB=function(e,n,s,i){var u,l=t.when.defer();if(!r(this.db))return l.reject(!1),l.promise;var h=this;if(r(h.cachestatus[e])&&!r(i)&&r(h.cachestatus[e][n])&&(h.cachestatus[e][n]===c||h.cachestatus[e][n]===o))return l.resolve(!1),l.promise;if(this.db.objectStoreNames.contains(e)){var d;r(h.cachestatus[e])||(h.cachestatus[e]={});try{d=this.db.transaction([e],"readwrite")}catch(e){return l.reject(null),l.promise}if(u=d.objectStore(e),r(i))if(i instanceof Array){for(var v=0,f=i.length;v<f;v++)h.cachestatus[e][i[v].key]!==o&&(u.add({id:i[v].key,value:i[v].value}),h.cachestatus[e][i[v].key]=o);l.resolve(!0)}else{for(var n in i)isNaN(1*n)||u.add({id:n,value:i[n]});l.resolve(!0)}else{if(!r(n)||!r(s))return;if(n instanceof Array&&s instanceof Array){for(v=0,f=n.length;v<f;v++)h.cachestatus[e][n[v]]!==o&&(u.add({id:n[v],value:s[v]}),h.cachestatus[e][n[v]]=o);d.oncomplete=function(e){l.resolve(!0)},d.onerror=function(e){l.reject(!1)}}else{var b=u.add({id:n,value:s});h.cachestatus[e][n]=c,b.onsuccess=function(t){h.cachestatus[e][n]=o,l.resolve(!0)},b.onerror=function(t){h.cachestatus[e][n]=a,l.reject(!1)}}}}else this.createObjectStore(e).then((function(t){var c=h.db.transaction([e],"readwrite");if(u=c.objectStore(e),r(i)){for(var o=0,a=i.length;o<a;o++)u.add({id:i[o].key,value:i[o].value});l.resolve(!0)}else{var d=u.add({id:n,value:s});d.onsuccess=function(e){l.resolve(!0)},d.onerror=function(e){l.reject(!1)}}}),(function(e){l.reject(!1)}));return l.promise},s.prototype.getRangeFromDB=function(e,n){var c,o,a=t.when.defer();if(!r(this.db))return null;if(!this.db.objectStoreNames.contains(e))return null;try{c=this.db.transaction([e])}catch(e){return a.reject(null),a.promise}try{o=c.objectStore(e)}catch(e){a.reject(null)}var s=o.openCursor(IDBKeyRange.bound(n[0],n[1])),i=[];return s.onsuccess=function(e){var t=e.target.result;r(t)?(i.push(t.value),t.continue()):a.resolve(i)},s.onerror=function(e){a.reject(null)},a.promise},s.prototype.getElementFromDB=function(e,n){var c,o,a=t.when.defer();if(!r(this.
|