UNPKG

@nu-art/thunder

Version:

Thunder - React & Typescript based frontend framework

98 lines 3.69 kB
"use strict"; /* * Thunder is a typescript & react frontend foundation that natively * runs on firebase function and is a part of the Thunderstorm larger project * * Copyright (C) 2018 Adam van der Kruk aka TacB0sS * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); /** * Created by tacb0ss on 27/07/2018. */ var ts_common_1 = require("@nu-art/ts-common"); var StorageModule_Class = /** @class */ (function (_super) { __extends(StorageModule_Class, _super); function StorageModule_Class() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.cache = {}; return _this; } StorageModule_Class.prototype.set = function (key, value) { if (!value) return this.delete(key); this.cache[key] = value; localStorage.setItem(key, JSON.stringify(value)); }; StorageModule_Class.prototype.delete = function (key) { this.clearCache(key); localStorage.removeItem(key); }; StorageModule_Class.prototype.clearCache = function (key) { delete this.cache[key]; }; StorageModule_Class.prototype.get = function (key, defaultValue) { var value = this.cache[key]; if (value) return value; value = localStorage.getItem(key); // this.logDebug(`get: ${key} = ${value}`) if (!value) return defaultValue || null; return this.cache[key] = JSON.parse(value); }; return StorageModule_Class; }(ts_common_1.Module)); exports.StorageModule_Class = StorageModule_Class; exports.StorageModule = new StorageModule_Class(); //TODO Generic Keys like in the tests contexts var StorageKey = /** @class */ (function () { function StorageKey(key) { this.key = key; } StorageKey.prototype.get = function (defaultValue) { // @ts-ignore return exports.StorageModule.get(this.key, defaultValue); }; StorageKey.prototype.patch = function (value) { var previousValue = this.get(); this.set(ts_common_1.merge(previousValue, value)); }; StorageKey.prototype.set = function (value) { // @ts-ignore exports.StorageModule.set(this.key, value); }; StorageKey.prototype.delete = function () { exports.StorageModule.delete(this.key); }; StorageKey.prototype.clearCache = function () { exports.StorageModule.clearCache(this.key); }; return StorageKey; }()); exports.StorageKey = StorageKey; //# sourceMappingURL=storage-module.js.map