@grafana/faro-web-sdk
Version:
Faro instrumentations, metas, transports for web.
46 lines • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VolatileSessionsManager = 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 VolatileSessionsManager {
constructor() {
this.updateSession = (0, utils_1.throttle)(() => this.updateUserSession(), sessionConstants_1.STORAGE_UPDATE_DELAY);
this.updateUserSession = (0, sessionManagerUtils_1.getUserSessionUpdater)({
fetchUserSession: VolatileSessionsManager.fetchUserSession,
storeUserSession: VolatileSessionsManager.storeUserSession,
});
this.init();
}
static removeUserSession() {
(0, webStorage_1.removeItem)(sessionConstants_1.STORAGE_KEY, VolatileSessionsManager.storageTypeSession);
}
static storeUserSession(session) {
(0, webStorage_1.setItem)(sessionConstants_1.STORAGE_KEY, (0, faro_core_1.stringifyExternalJson)(session), VolatileSessionsManager.storageTypeSession);
}
static fetchUserSession() {
const storedSession = (0, webStorage_1.getItem)(sessionConstants_1.STORAGE_KEY, VolatileSessionsManager.storageTypeSession);
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: VolatileSessionsManager.fetchUserSession,
storeUserSession: VolatileSessionsManager.storeUserSession,
}));
}
}
exports.VolatileSessionsManager = VolatileSessionsManager;
VolatileSessionsManager.storageTypeSession = webStorage_1.webStorageType.session;
//# sourceMappingURL=VolatileSessionManager.js.map