lycabinet
Version:
A simple small JSON Object storage helper with good performance.
2 lines • 7.73 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("Lycabinet",[],e):"object"==typeof exports?exports.Lycabinet=e():t.Lycabinet=e()}(self,(function(){return(()=>{"use strict";var t={d:(e,o)=>{for(var r in o)t.o(o,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:o[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{default:()=>u}),(()=>{const t=[];window.addEventListener("storage",(e=>{t.forEach((t=>{t(e)}))}),!1)})();const o=function(...t){let e;return t.reduce(((t,r)=>{for(let e in r)t[e]&&s(t[e])?o(t[e],r[e]):t[e]=r[e];return e=t,t}),t[0]),e},r=function(t,e,o=null){for(let i in e)t[i]&&s(t[i])?r(t[i],e[i]):t[i]=o?o(t[i],e[i]):e[i];return t},i=t=>null!=t,s=t=>"[object Object]"===Object.prototype.toString.call(t);const n="[Lycabinet]: ",a="idle",c="loading";function h(...t){this.__init.apply(this,t)}!function(t){const e=Object.create(null),o=t.prototype;o.getCabinet=function(){return this.__storage},o.isIdentical=function(){return this.__storage===e[this.__root]},o.hasStore=function(){return i(e[this.__root])&&s(e[this.__root])},o.getStore=function(){return e[this.__root]},o.setStore=function(t){e[this.__root]=t},o.removeStore=function(){return!(this.options.useSharedCabinet||!this.options.shareCabinet||!this.isIdentical()||(e[this.__root]=void 0,0))},t.$removeStore=function(t){e[t]=void 0}}(h),function(t){const e=t.prototype,o=new Function;e._isHappend=o,e._setlog=o,e._once=o,e._trigger=o,e._off=o,e._on=o}(h),function(t){t.DEBUG=!0,t.SeparateLog=!1;const e=t.prototype;e.__init=function(t,e={}){if(e.initStorage&&!s(e.initStorage))throw new Error(`${n}The type of the provided option "initStorage" must be an Object!`);if("string"!=typeof(r=t)||r.constructor!=String)throw new Error(`${n}The param "root" should be an string, than type ${typeof t}!`);var r;this.__root=(t||"lycabinet")+"";const i={root:this.__root,autoload:!0,lazyPeriod:~~e.lazyPeriod||5e3,saveMutex:!0,autoLazy:!0,logEvent:!1,useSharedCabinet:!0,shareCabinet:!0,deepMerge:!1,customMerge:null,localInterface:{database:window.localStorage,getItem:"getItem",setItem:"setItem",removeItem:"removeItem"},concurrent:!!(e.outerLoad||e.outerSave||e.outerClear),outerLoad:null,outerSave:null,outerClear:null};this.options=o(i,e),this.__install(i),i.logEvent&&this._setlog(),this.status="created",this._trigger("created"),i.autoload&&this._init(e.initStorage||Object.create(null))},e._init=function(t=null){t=t||this.options.initStorage||Object.create(null);const e=function(){var t,e;(t=this.__tempStorage)&&(e=t,Array.isArray&&Array.isArray(e)||e instanceof Array||"object"==typeof e&&"Array"===Object.prototype.toString.call(e).slice(-6,-1)?t.length:Object.keys(t).length)&&(o(this.__storage,this.__tempStorage),this.__tempStorage=Object.create(null),this._trigger("writeBackflow"))};this._on("loaded",e),this._on("cleared",e);const r=this.options.useSharedCabinet&&this.hasStore();return r?(this.__storage=this.getStore(),Object.assign(t,this.__storage),this._trigger("loadFromCache")):(this.__storage=this.__storage||t,this.options.shareCabinet&&this.setStore(this.__storage)),this.status="mounted",this._trigger("mounted"),r?this.status=a:this.options.autoload?this.load():this.status=a,this},e.isVacant=function(){return this.status===a},e.set=function(t,e){return[c,"clearing"].indexOf(this.status)>-1?(this._trigger("writeLock"),this.__tempStorage=this.__tempStorage||(this.__tempStorage=Object.create(null)),this.__tempStorage[t]=e,this):(this.__storage[t]=e,this._trigger("setItem",t,e),this)},e.get=function(t){let e=this.__storage[t];return this._trigger("getItem",t,e),e},e.remove=function(t){let e=!1;var o,r;return r=t=>{this.__storage.hasOwnProperty(t)&&(this.set(t,void 0),e=!0)},(o=t).forEach?o.forEach(r):r(o),e&&this._trigger("removeItem",t,e),this},e.clear=function(t={}){const e=i(t.concurrent)?t.concurrent:this.options.concurrent,o=i(t.onCloud)?t.onCloud:!!this.options.outerClear;this.status=c,this._trigger("beforeClear");let r=()=>{const t=o&&!e;if(this._trigger("beforeLocalClear",t),t)return this;const r=this.options.localInterface;r.database[r.removeItem](this.__root),this._trigger("localCleared",this.__root)};const s=r=>{this.status=a,this._trigger("cleared",o,e),t.onceDone&&t.onceDone(r,o)},h=[this.__root,this.__storage],u=()=>{s(!0)},l=(t,e="cloudClearings")=>{s(!1),!0!==this._trigger("error","clear",e)&&o&&console.error(`${n}Failed tfo Clear the cabinet "${this.__root}" on cloud. ${t}`)};try{t.reset&&Reflect.ownKeys(this.__storage).forEach((t=>{delete this.__storage[t]})),r(),o?this.options.outerClear(h,u,l):s(!0)}catch(t){l(t,"unknown")}return this},e.load=function(t={}){const e=i(t.concurrent)?t.concurrent:this.options.concurrent,h=i(t.onCloud)?t.onCloud:!!this.options.outerLoad,u=i(t.deepMerge)?~~t.deepMerge:this.options.deepMerge;this.status=c,this._trigger("beforeLoad");let l=()=>{let i=null;const s=h&&!e;if(this._trigger("beforeLocalLoad",s),s)return this;const n=this.options.localInterface;let a=n.database[n.getItem](this.__root);a=this._trigger("localLoaded",a),i=JSON.parse(a),u?t.disableMerge?o(this.__storage,i):r(this.__storage,i,this.options.customMerge):Object.assign(this.__storage,i)};const _=o=>{this.status=a,this._trigger("loaded",h,e),t.onceDone&&t.onceDone(o,h)},g=[this.__root,this.__storage],d=e=>{if(!i(e)||!s(e))throw new Error(`${n}Load cabinet with empty 'data' which type is ${typeof e}`);u?t.disableMerge?o(this.__storage,e):r(this.__storage,e,this.options.customMerge):Object.assign(this.__storage,e),_(!0)},p=(t,e="cloudLoadings")=>{_(!1),!0!==this._trigger("error","load",e)&&h&&console.error(`${n}Failed to Load the cabinet "${this.__root}" on cloud. ${t}`)};try{l(),h?this.options.outerLoad(g,d,p):_(!0)}catch(t){p(t,"unknown")}return this},e.save=function(t={}){const e=i(t.onCloud)?t.onCloud:!!this.options.outerSave,o=i(t.concurrent)?t.concurrent:this.options.concurrent;let r=this.options.saveMutex&&!this.isVacant();if(this._trigger("beforeSave",r),r)return this._trigger("busy",this.status),this.options.autoLazy&&this.lazySave(e,o),this;this.status="saving";let s=()=>{const t=e&&!o;if(this._trigger("beforeLocalSave",t),t)return this;let r=JSON.stringify(this.__storage);r=this._trigger("localSaved",r);const i=this.options.localInterface;i.database[i.setItem](this.__root,r)};const c=r=>{this.status=a,this._trigger("saved",e,o),t.onceDone&&t.onceDone(r,e)},h=[this.__root,this.__storage],u=()=>{c(!0)},l=(t,o="cloudSavings")=>{c(!1),!0!==this._trigger("error","save",o)&&e&&console.error(`${n}Failed to Save the cabinet "${this.__root}" on cloud. ${t}`)};try{s(),e?this.options.outerSave(h,u,l):c(!0)}catch(t){l(t,"unknown")}return this},e.forEach=function(t){let e;const o=this.__storage;for(let r in o)e=o[r],t(e,r,o);return this},e.map=function(t){let e;const o=this.__storage;for(let r in o)e=o[r],o[r]=t(e,r,o);return this},e.destroy=function(t=!0){t&&(this.clear({reset:!0,onCloud:!1,concurrent:!1}),this.removeStore()),this.status="destroyed",this._trigger("destroyed")}}(h),function(t){var e;t.prototype.lazySave=(e=0,function(...t){var o=(new Date).getTime();let r=o-e>5e3;return this._trigger("lazySave",r),r&&(e=o,this.save(...t)),this}),t.prototype.lazySet=function(t,e,...o){return this.set(t,e).lazySave(...o),this}}(h),function(t){t.prototype._mixins=[],t.mixin=function(e){return t.prototype._mixins.push(e),this},t.prototype.__install=function(...e){e.unshift(this),t.prototype._mixins.forEach((t=>{t.apply(t,e)}))}}(h),function(t){const e=t.prototype;e.delete=e.remove,e.read=e.get,e.storage=e.getCabinet,e.isConsistent=e.isIdentical}(h);const u=h;return e.default})()}));
//# sourceMappingURL=lycabinet.light.min.js.map