@grafana/faro-web-sdk
Version:
Faro instrumentations, metas, transports for web.
49 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VolatileSessionsManager = void 0;
var faro_core_1 = require("@grafana/faro-core");
var utils_1 = require("../../../utils");
var webStorage_1 = require("../../../utils/webStorage");
var sessionConstants_1 = require("./sessionConstants");
var sessionManagerUtils_1 = require("./sessionManagerUtils");
var VolatileSessionsManager = /** @class */ (function () {
function VolatileSessionsManager() {
var _this = this;
this.updateSession = (0, utils_1.throttle)(function () { return _this.updateUserSession(); }, sessionConstants_1.STORAGE_UPDATE_DELAY);
this.updateUserSession = (0, sessionManagerUtils_1.getUserSessionUpdater)({
fetchUserSession: VolatileSessionsManager.fetchUserSession,
storeUserSession: VolatileSessionsManager.storeUserSession,
});
this.init();
}
VolatileSessionsManager.removeUserSession = function () {
(0, webStorage_1.removeItem)(sessionConstants_1.STORAGE_KEY, VolatileSessionsManager.storageTypeSession);
};
VolatileSessionsManager.storeUserSession = function (session) {
(0, webStorage_1.setItem)(sessionConstants_1.STORAGE_KEY, (0, faro_core_1.stringifyExternalJson)(session), VolatileSessionsManager.storageTypeSession);
};
VolatileSessionsManager.fetchUserSession = function () {
var storedSession = (0, webStorage_1.getItem)(sessionConstants_1.STORAGE_KEY, VolatileSessionsManager.storageTypeSession);
if (storedSession) {
return JSON.parse(storedSession);
}
return null;
};
VolatileSessionsManager.prototype.init = function () {
var _this = this;
document.addEventListener('visibilitychange', function () {
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,
}));
};
VolatileSessionsManager.storageTypeSession = webStorage_1.webStorageType.session;
return VolatileSessionsManager;
}());
exports.VolatileSessionsManager = VolatileSessionsManager;
//# sourceMappingURL=VolatileSessionManager.js.map