UNPKG

strict-async-storage

Version:
2 lines (1 loc) 2.81 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).strictAsyncStorage={})}(this,(function(t){"use strict";const e=async(t,e)=>{let i=0;for(const s of t)await e(await s,i++);return t};var i=e,s=e;i.default=s;var a=(t,{include:e,exclude:i}={})=>{const s=t=>{const s=e=>"string"==typeof e?t===e:e.test(t);return e?e.some(s):!i||!i.some(s)};for(const[e,i]of(t=>{const e=new Set;do{for(const i of Reflect.ownKeys(t))e.add([t,i])}while((t=Reflect.getPrototypeOf(t))&&t!==Object.prototype);return e})(t.constructor.prototype)){if("constructor"===i||!s(i))continue;const a=Reflect.getOwnPropertyDescriptor(e,i);a&&"function"==typeof a.value&&(t[i]=t[i].bind(t))}return t};t.StrictAsyncStorage=class{get initialized(){return this._initialized}get disposed(){return this._disposed}setInitialized(){this._initialized=!0}constructor(t,e=localStorage){this._defaults=void 0,this._driver=void 0,this._map=new Map,this._initialized=!1,this._disposed=!1,this._defaults=t,this._driver=e,a(this)}async initialize(){if(this._initialized)throw new Error("[strict-async-storage] Invalid operation. This has been initialized.");await i(Object.keys(this._defaults),(async t=>{const e=await this._driver.getItem(t);var i;this.setMap(t,null===e?(i=this._defaults[t],JSON.parse(JSON.stringify(i))):e)})),this.setInitialized()}getItem(t){return this.enable(),this.valid(t),this._map.get(t)}setMap(t,e){this._map.set(t,e)}batchMap(t){t.forEach(((t,e)=>{this._map.set(e,t)}))}async setItem(t,e){this.enable(),this.valid(t),await this.getItem(t)!==e&&(null===e&&(e=this._defaults[t]),await this._driver.setItem(t,e),this.setMap(t,e))}async resetItem(t){return this.enable(),this.valid(t),await this.setItem(t,this._defaults[t]),this.getItem(t)}async resetAll(){this.enable();const t=new Map;await i(Object.keys(this._defaults),(async e=>{await this._driver.setItem(e,this._defaults[e]),t.set(e,this._defaults[e])})),this.batchMap(t),t.clear()}get length(){return this.enable(),this._map.size}dispose(){if(this._disposed)throw new Error("[strict-async-storage] Invalid operation. This has been disposed.");this._defaults=void 0,this._map.clear(),this._map=void 0,this._driver=void 0,this._disposed=!0}get defaults(){return this._defaults}hasItem(t){return this.enable(),this._map.has(t)}enable(){if(!this._initialized||this._disposed)throw new Error("[strict-async-storage] Invalid operation. Not initialized yet, failed to initialize or has been disposed.")}valid(t){if(!this.hasItem(t))throw new RangeError("[strict-async-storage] The key parameter is an invalid value. Not initialized yet, failed to initialize or has been disposed.")}},Object.defineProperty(t,"__esModule",{value:!0})}));