UNPKG

@daysnap/utils

Version:
68 lines (58 loc) 1.78 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _class; var _chunk5PB5B4HHcjs = require('./chunk-5PB5B4HH.cjs'); var _chunkOSEQ7XR6cjs = require('./chunk-OSEQ7XR6.cjs'); // src/storage/storage.ts var Storage = (_class = class { __init() {this.value = null} constructor(key, storage) {;_class.prototype.__init.call(this); this.key = key; this.storage = storage; } /** * 设置值 */ setItem(val) { this.value = null; this.storage.setItem(this.key, JSON.stringify(val)); return val; } getItem(defaultVal) { const val = this.storage.getItem(this.key); if (val === null) { return _nullishCoalesce(defaultVal, () => ( null)); } return JSON.parse(val); } /** * 删除值 */ removeItem() { this.value = null; this.storage.removeItem(this.key); } /** * 更新值 */ updateItem(val) { this.value = null; const oldVal = this.getItem(); if (_chunk5PB5B4HHcjs.isArray.call(void 0, val) && _chunk5PB5B4HHcjs.isArray.call(void 0, oldVal)) { val = [...oldVal, ...val]; } else if (_chunkOSEQ7XR6cjs.isObject.call(void 0, val) && _chunkOSEQ7XR6cjs.isObject.call(void 0, oldVal)) { val = { ...oldVal, ...val }; } return this.setItem(val); } getItemOnce(defaultVal) { const val = this.storage.getItem(this.key); if (val === null) { return _nullishCoalesce(defaultVal, () => ( null)); } this.removeItem(); return JSON.parse(val); } getItemWithCache(defaultVal) { return _nullishCoalesce(this.value, () => ( (this.value = this.getItem(defaultVal)))); } }, _class); exports.Storage = Storage;