UNPKG

miniapp-web-jsapi

Version:

JSAPI/View adapter for miniprogram running on the web

62 lines 1.82 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import { getAppIdSync } from '../misc'; var StorageImpl = /*#__PURE__*/function () { function StorageImpl() { _classCallCheck(this, StorageImpl); } _createClass(StorageImpl, null, [{ key: "getKey", value: function getKey() { return getAppIdSync() || 'global'; } }, { key: "storageValue", get: function get() { var getInternal = function getInternal() { var content = localStorage.getItem(StorageImpl.getKey()); if (content) { try { return JSON.parse(content); } catch (e) { // ignore } } return undefined; }; if (!this._storageValue) { this._storageValue = getInternal(); } return this._storageValue || {}; } }, { key: "setStorage", value: function setStorage(key, value) { this.storageValue[key] = value; // update localStorage.setItem(StorageImpl.getKey(), JSON.stringify(this.storageValue)); } }, { key: "getStorage", value: function getStorage(key) { return this.storageValue[key]; } }, { key: "removeStorage", value: function removeStorage(key) { this.storageValue[key] = undefined; // update localStorage.setItem(StorageImpl.getKey(), JSON.stringify(this.storageValue)); } }, { key: "clearStorage", value: function clearStorage() { this._storageValue = undefined; localStorage.setItem(StorageImpl.getKey(), '{}'); } }]); return StorageImpl; }(); _defineProperty(StorageImpl, "_storageValue", void 0); export { StorageImpl as default };