UNPKG

@pnp/spfx-controls-react

Version:

Reusable React controls for SharePoint Framework solutions

37 lines 1.64 kB
"use strict"; /* eslint-disable @typescript-eslint/no-explicit-any */ Object.defineProperty(exports, "__esModule", { value: true }); exports.useCache = void 0; var tslib_1 = require("tslib"); var addSeconds_1 = tslib_1.__importDefault(require("date-fns/addSeconds")); var isAfter_1 = tslib_1.__importDefault(require("date-fns/isAfter")); var DEFAULT_EXPIRED_IN_SECONDS = 60 * 30; // 30 min var useCache = function (cacheType) { var setCacheValue = function (key, newValue, expiredInSeconds) { var expires = (0, addSeconds_1.default)(new Date(), expiredInSeconds !== null && expiredInSeconds !== void 0 ? expiredInSeconds : DEFAULT_EXPIRED_IN_SECONDS); if (cacheType === "session") { sessionStorage.setItem(key, JSON.stringify({ value: newValue, expires: expires.getTime() })); } else { localStorage.setItem(key, JSON.stringify({ value: newValue, expires: expires.getTime() })); } }; var getCacheValue = function (key) { var storage = {}; if (cacheType === "session") { storage = JSON.parse(sessionStorage.getItem(key) || "{}"); } else { storage = JSON.parse(localStorage.getItem(key) || "{}"); } // getting stored value var _a = storage || {}, value = _a.value, expires = _a.expires; if (expires && (0, isAfter_1.default)(new Date(expires), new Date())) { return value; } return undefined; }; return { getCacheValue: getCacheValue, setCacheValue: setCacheValue }; }; exports.useCache = useCache; //# sourceMappingURL=useLocalStorage.js.map