UNPKG

@grafana/faro-web-sdk

Version:

Faro instrumentations, metas, transports for web.

61 lines 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PersistentSessionsManager = void 0; const faro_core_1 = require("@grafana/faro-core"); const utils_1 = require("../../../utils"); const webStorage_1 = require("../../../utils/webStorage"); const sessionConstants_1 = require("./sessionConstants"); const sessionManagerUtils_1 = require("./sessionManagerUtils"); class PersistentSessionsManager { constructor() { // Set only for the synchronous span of an adopting setSession(); the session // instrumentation reads isAdopting() to suppress its lifecycle event. this.adopting = false; this.isAdopting = () => this.adopting; this.adoptSession = (sessionMeta) => { var _a; this.adopting = true; try { (_a = faro_core_1.faro.api) === null || _a === void 0 ? void 0 : _a.setSession(sessionMeta); } finally { this.adopting = false; } }; this.updateSession = (0, utils_1.throttle)(() => this.updateUserSession(), sessionConstants_1.STORAGE_UPDATE_DELAY); this.updateUserSession = (0, sessionManagerUtils_1.getUserSessionUpdater)({ fetchUserSession: PersistentSessionsManager.fetchUserSession, storeUserSession: PersistentSessionsManager.storeUserSession, adoptSession: this.adoptSession, }); this.init(); } static removeUserSession() { (0, webStorage_1.removeItem)(sessionConstants_1.STORAGE_KEY, PersistentSessionsManager.storageTypeLocal); } static storeUserSession(session) { (0, webStorage_1.setItem)(sessionConstants_1.STORAGE_KEY, (0, faro_core_1.stringifyExternalJson)(session), PersistentSessionsManager.storageTypeLocal); } static fetchUserSession() { const storedSession = (0, webStorage_1.getItem)(sessionConstants_1.STORAGE_KEY, PersistentSessionsManager.storageTypeLocal); if (storedSession) { return JSON.parse(storedSession); } return null; } init() { document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'visible') { this.updateSession(); } }); // Users can call the setSession() method, so we need to sync this with the local storage session faro_core_1.faro.metas.addListener((0, sessionManagerUtils_1.getSessionMetaUpdateHandler)({ fetchUserSession: PersistentSessionsManager.fetchUserSession, storeUserSession: PersistentSessionsManager.storeUserSession, })); } } exports.PersistentSessionsManager = PersistentSessionsManager; PersistentSessionsManager.storageTypeLocal = webStorage_1.webStorageType.local; //# sourceMappingURL=PersistentSessionsManager.js.map