UNPKG

browser-session-tabs-tracker

Version:

A light-weight library for tracking multiple browser tabs in a web session.

60 lines (59 loc) 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Cookies = require("js-cookie"); var StorageService = /** @class */ (function () { function StorageService() { } /** * Sets a given value identified by a given key, in session storage. * The value will be stored as a JSON string. */ StorageService.prototype.sessionStorageSet = function (key, value) { var raw = JSON.stringify(value); window.sessionStorage.setItem(key, raw); }; /** * Get a value from session storage, by a given key. * @returns The unserialised object if found, otherwise the provided default value. * If no default value is provided, it returns `null`. */ StorageService.prototype.sessionStorageGet = function (key, defaultValue) { try { var raw = window.sessionStorage.getItem(key); if (raw) { return JSON.parse(raw); } } catch (_a) { // do nothing } return defaultValue === undefined ? null : defaultValue; }; /** * Sets a given value identified by a given key, as a session cookie. * The value will be stored as a JSON string. */ StorageService.prototype.sessionCookieSet = function (key, value) { var raw = JSON.stringify(value); Cookies.set(key, raw); }; /** * Get a value from session cookies, by a given key. * @returns The unserialised object if found, otherwise the provided default value. * If no default value is provided, it returns `null`. */ StorageService.prototype.sessionCookieGet = function (key, defaultValue) { try { var raw = Cookies.get(key); if (raw) { return JSON.parse(raw); } } catch (_a) { // do nothing } return defaultValue === undefined ? null : defaultValue; }; return StorageService; }()); exports.StorageService = StorageService;