@sheto/storage
Version:
你见过这么方便的Storage操作库吗?我见过,就是这个!
163 lines (162 loc) • 6.46 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _BasicStore_core, _BasicStore_current;
import { _generateStorageName, _batchInitStore } from './utils';
import { SUFFIX, DEFAULT } from './magic';
export class BasicStore {
// 初始化
constructor(core) {
_BasicStore_core.set(this, void 0);
// 当前使用库名
_BasicStore_current.set(this, void 0);
__classPrivateFieldSet(this, _BasicStore_core, core || sessionStorage, "f");
}
get storage() {
return JSON.parse(__classPrivateFieldGet(this, _BasicStore_core, "f").getItem(__classPrivateFieldGet(this, _BasicStore_current, "f")) || '{}');
}
set storage(value) {
this.create(__classPrivateFieldGet(this, _BasicStore_current, "f"), value);
}
set name(name) {
__classPrivateFieldSet(this, _BasicStore_current, _generateStorageName(name), "f");
// 看看是否存在对应数据库,如果不存在则初始化
// 避免第1次赋值出现问题
if (__classPrivateFieldGet(this, _BasicStore_core, "f").getItem(__classPrivateFieldGet(this, _BasicStore_current, "f")) === null) {
// 初始化仓库
this.create(__classPrivateFieldGet(this, _BasicStore_current, "f"));
}
}
get name() {
return __classPrivateFieldGet(this, _BasicStore_current, "f");
}
/**
* @description 切换storage
* @description 如果不传则切回默认storage
* @param name 要切换的storage名字
*/
use(name = DEFAULT) {
this.name = name;
}
/**
* @description 创建storage
* @param name 要创建的storage名字
* @param _InitData storage初始值,默认一个空storage
*/
create(name, value = {}) {
__classPrivateFieldGet(this, _BasicStore_core, "f").setItem(name, JSON.stringify(value));
}
/**
* @description 对于对象和数组是追加模式,如果要清空或者重新赋值对象和数组,请使用reset
* @param key 存储键
* @param value 要设置的值
* @param expire 过期时效,默认为0永不过期
*/
set(key, value, expire = 0) {
const currentStorage = this.storage;
if (currentStorage[key]) {
const old = currentStorage[key];
if (Array.isArray(old.value)) {
// 如果传入的也是数组,则融合两个数组
if (Array.isArray(value)) {
for (const item of value) {
old.value.push(item);
}
}
else {
old.value.push(value);
}
}
// fix20210223:之前对象遍历的是旧值,如果旧值不存在新增的键,则新增的值永远不会赋值qvq~~。
else if (typeof old.value === 'object') {
Object.keys(value).forEach(function (key) {
old.value[key] = value[key];
});
}
else {
old.value = value;
}
currentStorage[key] = old;
}
else {
currentStorage[key] = {
value: value,
time: Number((new Date().getTime() / 1000).toFixed(0)),
expire: expire
};
}
// 更新储存库
this.storage = currentStorage;
}
get(key) {
if (!this.storage[key]) {
return undefined;
}
const storageValue = this.storage[key];
if (storageValue.expire > 0) {
// 检查缓存是否失效
if (Number((new Date().getTime() / 1000).toFixed(0)) - storageValue.time > storageValue.expire) {
// 使当前storage失效
return this.reset(key);
}
}
try {
return JSON.parse(storageValue.value);
}
catch {
return storageValue.value;
}
}
/**
* @description 删除storage
* @description 不传名字删除当前storage
* @description 指定名字删除指定storage
*/
drop(name) {
__classPrivateFieldGet(this, _BasicStore_core, "f").removeItem(name ? `${name}${SUFFIX}` : __classPrivateFieldGet(this, _BasicStore_current, "f"));
}
/**
* @description 重置storage,如果重置不传值则默认是删除这个值
* @param key
* @param value
*/
reset(key, value) {
const storage = this.storage;
const data = Object.prototype.toString.call(value) === '[object Function]' ? value(this.get(key)) : value ? value : undefined;
// 如果有值,则用值重置项
if (data) {
storage[key].value = data;
}
// 如果无值,则删除项
else {
Reflect.deleteProperty(storage, key);
}
// 更新库
this.storage = storage;
}
}
_BasicStore_core = new WeakMap(), _BasicStore_current = new WeakMap();
export let currentStore = {};
export let currentCache = {};
export class StoragePlus extends BasicStore {
constructor(name) {
super(localStorage);
_batchInitStore.call(this, name);
currentStore = this;
}
}
export class CachePlus extends BasicStore {
constructor(name) {
super();
_batchInitStore.call(this, name);
currentCache = this;
}
}