strict-async-storage
Version:
197 lines (153 loc) • 4.8 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.strictAsyncStorage = {}));
}(this, (function (exports) { 'use strict';
const pEachSeries = async (iterable, iterator) => {
let index = 0;
for (const value of iterable) {
// eslint-disable-next-line no-await-in-loop
await iterator(await value, index++);
}
return iterable;
};
var pEachSeries_1 = pEachSeries;
// TODO: Remove this for the next major release
var _default = pEachSeries;
pEachSeries_1.default = _default;
// Gets all non-builtin properties up the prototype chain
const getAllProperties = object => {
const properties = new Set();
do {
for (const key of Reflect.ownKeys(object)) {
properties.add([object, key]);
}
} while ((object = Reflect.getPrototypeOf(object)) && object !== Object.prototype);
return properties;
};
var autoBind = (self, {include, exclude} = {}) => {
const filter = key => {
const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);
if (include) {
return include.some(match);
}
if (exclude) {
return !exclude.some(match);
}
return true;
};
for (const [object, key] of getAllProperties(self.constructor.prototype)) {
if (key === 'constructor' || !filter(key)) {
continue;
}
const descriptor = Reflect.getOwnPropertyDescriptor(object, key);
if (descriptor && typeof descriptor.value === 'function') {
self[key] = self[key].bind(self);
}
}
return self;
};
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(this);
}
async initialize() {
if (this._initialized) {
throw new Error('[strict-async-storage] Invalid operation. This has been initialized.');
}
await pEachSeries_1(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 pEachSeries_1(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;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=strict-async-storage.umd.js.map