Source
* Synchronization tick. It checks if any of keys have been written outside the window object where this instance runs.
/*
** 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.
**/
ZBX_LocalStorage.defines = {
PREFIX_SEPARATOR: ':',
KEEP_ALIVE_INTERVAL: 30,
SYNC_INTERVAL_MS: 500,
KEY_SESSIONS: 'sessions',
KEY_LAST_WRITE: 'key_last_write'
};
/**
* Local storage wrapper. Implements singleton.
*
* @param {string} version Mandatory parameter.
* @param {string} prefix Used to distinct keys between sessions within same domain.
*/
function ZBX_LocalStorage(version, prefix) {
if (!version || !prefix) {
throw 'Local storage instantiation must be versioned, and prefixed.';
}
if (ZBX_LocalStorage.instance) {
return ZBX_LocalStorage.instance;
}
ZBX_LocalStorage.sessionid = prefix;
ZBX_LocalStorage.prefix = prefix + ZBX_LocalStorage.defines.PREFIX_SEPARATOR;
ZBX_LocalStorage.instance = this;
ZBX_LocalStorage.signature = (Math.random() % 9e6).toString(36).substr(2);
this.abs_to_rel_keymap = {};
this.key_last_write = {};
this.rel_keys = {};
this.abs_keys = {};
this.addKey('version');
this.addKey('tabs.lastseen');
this.addKey('notifications.list');
this.addKey('notifications.active_tabid');
this.addKey('notifications.user_settings');
this.addKey('notifications.alarm_state');
this.addKey('dashboard.copied_widget');
this.addKey('web.notifications.pos');
if (this.readKey('version') != version) {
this.truncate();
this.writeKey('version', version);
}
this.register();
}
/**
* @param {string} key Relative key.
* @param {callable} callback When out of sync is observed, then new value is read, and passed into callback.
*
* @return {string}
*/
ZBX_LocalStorage.prototype.onKeySync = function(key, callback) {
this.rel_keys[key].subscribeSync(callback);
};
/**
* @param {string} key Relative key.
* @param {callable} callback Executed on storage event for this key.
*
* @return {string}
*/
ZBX_LocalStorage.prototype.onKeyUpdate = function(key, callback) {
this.rel_keys[key].subscribe(callback);
};
/**