strict-async-storage
Version:
141 lines (109 loc) • 3.39 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var eachSeries = require('p-each-series');
var autoBind = require('auto-bind');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var eachSeries__default = /*#__PURE__*/_interopDefaultLegacy(eachSeries);
var autoBind__default = /*#__PURE__*/_interopDefaultLegacy(autoBind);
class StrictAsyncStorage {
get initialized() {
return this._initialized;
}
get disposed() {
return this._disposed;
}
setInitialized() {
this._initialized = true;
}
constructor(defaults, driver = localStorage) {
this._defaults = void 0;
this._driver = void 0;
this._map = new Map();
this._initialized = false;
this._disposed = false;
this._defaults = defaults;
this._driver = driver;
autoBind__default['default'](this);
}
async initialize() {
if (this._initialized) {
throw new Error('[strict-async-storage] Invalid operation. This has been initialized.');
}
await eachSeries__default['default'](Object.keys(this._defaults), async key => {
const value = await this._driver.getItem(key);
this.setMap(key, value === null ? Jclone(this._defaults[key]) : value);
});
this.setInitialized();
}
getItem(key) {
this.enable();
this.valid(key);
return this._map.get(key);
}
setMap(key, value) {
this._map.set(key, value);
}
batchMap(map) {
map.forEach((value, key) => {
this._map.set(key, value);
});
}
async setItem(key, value) {
this.enable();
this.valid(key);
if ((await this.getItem(key)) === value) return;
if (value === null) value = this._defaults[key];
await this._driver.setItem(key, value);
this.setMap(key, value);
}
async resetItem(key) {
this.enable();
this.valid(key);
await this.setItem(key, this._defaults[key]);
return this.getItem(key);
}
async resetAll() {
this.enable();
const map = new Map();
await eachSeries__default['default'](Object.keys(this._defaults), async key => {
await this._driver.setItem(key, this._defaults[key]);
map.set(key, this._defaults[key]);
});
this.batchMap(map);
map.clear();
}
get length() {
this.enable();
return this._map.size;
}
dispose() {
if (this._disposed) {
throw new Error('[strict-async-storage] Invalid operation. This has been disposed.');
} //@ts-expect-error
this._defaults = undefined;
this._map.clear(); //@ts-expect-error
this._map = undefined; //@ts-expect-error
this._driver = undefined;
this._disposed = true;
}
get defaults() {
return this._defaults;
}
hasItem(key) {
this.enable();
return this._map.has(key);
}
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(key) {
if (!this.hasItem(key)) throw new RangeError('[strict-async-storage] The key parameter is an invalid value. Not initialized yet, failed to initialize or has been disposed.');
}
}
function Jclone(src) {
return JSON.parse(JSON.stringify(src));
}
exports.StrictAsyncStorage = StrictAsyncStorage;
//# sourceMappingURL=strict-async-storage.cjs.js.map