browser-session-tabs-tracker
Version:
A light-weight library for tracking multiple browser tabs in a web session.
134 lines (133 loc) • 5.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var DEFAULT_STORAGE_KEY = 'browser-session-info';
var REGEX_EMPTY_STRING = /^\s*$/;
var INVALID_STORAGE_KEY_ERROR = 'Invalid storage key';
var SESSION_ID_GENERATOR_NOT_SET_ERROR = 'Session ID generator not set';
var BrowserTabTracker = /** @class */ (function () {
function BrowserTabTracker(storageService) {
this.storageService = storageService;
this.storageKeyName = DEFAULT_STORAGE_KEY;
this.sessionInfo = null;
this.sessionIdGenerator = null;
this.sessionStartedCallback = null;
this.newTabOpenedCallback = null;
this.newSessionCreated = false;
}
Object.defineProperty(BrowserTabTracker.prototype, "tabId", {
/**
* The current tab ID.
* The tab ID starts from 1, and increments for every tab opened in the session.
* @returns a number
*/
get: function () {
return this.sessionInfo.tab;
},
enumerable: true,
configurable: true
});
Object.defineProperty(BrowserTabTracker.prototype, "sessionId", {
/**
* The current session ID.
* The session ID is shared across multiple browser tabs for a given session.
* @returns a value returned by the SessionIdGenerator
*/
get: function () {
return this.sessionInfo.id;
},
enumerable: true,
configurable: true
});
Object.defineProperty(BrowserTabTracker.prototype, "storageKey", {
/**
* The name used for the `session storage` item and `cookie`
*/
get: function () {
return this.storageKeyName;
},
enumerable: true,
configurable: true
});
/**
* This should be called only after the following`sessionIdGenerator` and `storageKey` are set.
*/
BrowserTabTracker.prototype.initialize = function (options) {
var _this = this;
if (!this.sessionInfo) {
// set options
this.validateSessionIdGenerator(options.sessionIdGenerator);
this.sessionIdGenerator = options.sessionIdGenerator;
this.sessionStartedCallback = options.sessionStartedCallback;
this.newTabOpenedCallback = options.newTabOpenedCallback;
if (options.storageKey) {
this.validateKey(options.storageKey);
this.storageKeyName = options.storageKey;
}
// fill in session info
this.sessionInfo = this.generateSessionInfo();
// new session? then trigger the callback
if (this.newSessionCreated && this.sessionStartedCallback) {
this.sessionStartedCallback(this.sessionId, this.tabId);
}
}
window.addEventListener('beforeunload', function (event) {
delete event.returnValue;
_this.onTabClosed();
});
};
BrowserTabTracker.prototype.generateSessionInfo = function () {
// if the page has been refreshed in the same tab,
// we expect the info to be in session storage already
var sessionInfo = this.storageService.sessionStorageGet(this.storageKeyName);
// first time ever on this browser tab?
// we expect the session storage to not have the info
if (!sessionInfo) {
sessionInfo = this.generateNewTabId();
}
return sessionInfo;
};
BrowserTabTracker.prototype.onTabClosed = function () {
var sessionInfo = this.storageService.sessionStorageGet(this.storageKeyName);
sessionInfo.tab = sessionInfo.tab - 1;
// save cookie, to be shared amongst other tabs in the same session
this.storageService.sessionCookieSet(this.storageKeyName, sessionInfo);
// save in session storage, just so it can be accessed within this tab
this.storageService.sessionStorageSet(this.storageKeyName, sessionInfo);
this.sessionInfo = sessionInfo;
};
BrowserTabTracker.prototype.generateNewTabId = function () {
// check if there's already a session in a different tab
var sessionInfo = this.storageService.sessionCookieGet(this.storageKeyName);
if (!sessionInfo) {
sessionInfo = this.startNewSession();
}
sessionInfo.tab = sessionInfo.tab + 1; // increase count
if (this.newTabOpenedCallback) {
this.newTabOpenedCallback(sessionInfo.tab);
}
// save cookie, to be shared amongst other tabs in the same session
this.storageService.sessionCookieSet(this.storageKeyName, sessionInfo);
// save in session storage, just so it can be accessed within this tab
this.storageService.sessionStorageSet(this.storageKeyName, sessionInfo);
return sessionInfo;
};
BrowserTabTracker.prototype.validateKey = function (key) {
if (!key || REGEX_EMPTY_STRING.test(key)) {
throw new Error(INVALID_STORAGE_KEY_ERROR);
}
};
BrowserTabTracker.prototype.startNewSession = function () {
this.newSessionCreated = true;
return {
id: this.sessionIdGenerator(),
tab: 0
};
};
BrowserTabTracker.prototype.validateSessionIdGenerator = function (generator) {
if (!generator) {
throw new Error(SESSION_ID_GENERATOR_NOT_SET_ERROR);
}
};
return BrowserTabTracker;
}());
exports.BrowserTabTracker = BrowserTabTracker;