Source
xxxxxxxxxx
/*
** Zabbix
** Copyright (C) 2001-2022 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* Amount of seconds for keep-alive interval.
*/
ZBX_BrowserTab.keep_alive_interval = 30;
/**
* This object is representing a browser tab. Implements singleton pattern. It ensures there are only non-crashed tabs
* in store.
*
* @param {ZBX_LocalStorage} store A localStorage wrapper.
*/
function ZBX_BrowserTab(store) {
if (ZBX_BrowserTab.instance) {
return ZBX_BrowserTab.instance;
}
if (!(store instanceof ZBX_LocalStorage)) {
throw 'Unmatched signature!';
}
ZBX_BrowserTab.instance = this;
this.uid = (Math.random() % 9e6).toString(36).substr(2);
this.store = store;
var ctx_lastseen = this.store.readKey('tabs.lastseen', {});
ctx_lastseen[this.uid] = Math.floor(+new Date / 1000);
this.lastseen = ctx_lastseen;
this.on_focus_cbs = [];
this.on_blur_cbs = [];
this.on_unload_cbs = [];
this.on_crashed_cbs = [];
this.bindEventHandlers();
this.pushLastseen();
}
/**
* @param {object} lastseen
*/
ZBX_BrowserTab.prototype.handlePushedLastseen = function(lastseen) {
this.lastseen = lastseen;
};
/**
* Checks and updates current lastseen object.
*/
ZBX_BrowserTab.prototype.handleKeepAliveTick = function() {
this.lastseen[this.uid] = Math.floor(+new Date / 1000);
this.findCrashedTabs().forEach(function(tabid) {
delete this.lastseen[tabid];
this.handleCrashed(tabid);
}.bind(this));
this.pushLastseen();
};
/**
* Writes own ID in `store.tabs` object. Registers unload event to remove own ID from `store.tabs.lastseen`.
* Registers focus event. Begins a loop to see if any tab of tabs has crashed.
*/
ZBX_BrowserTab.prototype.bindEventHandlers = function() {
this.handleKeepAliveTick();
setInterval(this.handleKeepAliveTick.bind(this), ZBX_BrowserTab.keep_alive_interval * 1000);
window.addEventListener('beforeunload', this.handleBeforeUnload.bind(this));
window.addEventListener('focus', this.handleFocus.bind(this));
this.store.onKeyUpdate('tabs.lastseen', this.handlePushedLastseen.bind(this));