UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

136 lines (132 loc) 3.58 kB
/** * @package @bitrix24/b24jssdk * @version 2.0.0 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ 'use strict'; const type = require('../tools/type.cjs'); const text = require('../tools/text.cjs'); const storageManager = require('./storage-manager.cjs'); const pull = require('../types/pull.cjs'); const loggerFactory = require('../logger/logger-factory.cjs'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class SharedConfig { static { __name(this, "SharedConfig"); } _logger; _storage; _ttl = 24 * 60 * 60; _callbacks; constructor(params = {}) { this._logger = loggerFactory.LoggerFactory.createNullLogger(); params = params || {}; this._storage = params.storage || new storageManager.StorageManager(); this._callbacks = { onWebSocketBlockChanged: type.Type.isFunction(params.onWebSocketBlockChanged) ? params.onWebSocketBlockChanged : () => { } }; if (this._storage) { window.addEventListener("storage", this.onLocalStorageSet.bind(this)); } } setLogger(logger) { this._logger = logger; } getLogger() { return this._logger; } onLocalStorageSet(params) { if (this._storage.compareKey( params.key || "", pull.LsKeys.WebsocketBlocked ) && params.newValue !== params.oldValue) { this._callbacks.onWebSocketBlockChanged({ isWebSocketBlocked: this.isWebSocketBlocked() }); } } isWebSocketBlocked() { if (!this._storage) { return false; } return this._storage.get(pull.LsKeys.WebsocketBlocked, 0) > Date.now(); } setWebSocketBlocked(isWebSocketBlocked) { if (!this._storage) { return false; } try { this._storage.set( pull.LsKeys.WebsocketBlocked, isWebSocketBlocked ? Date.now() + this._ttl : 0 ); } catch (error) { this.getLogger().error( `${text.Text.getDateForLog()}: Pull: Could not save WS_blocked flag in local storage`, { error } ); return false; } return true; } isLongPollingBlocked() { if (!this._storage) { return false; } return this._storage.get(pull.LsKeys.LongPollingBlocked, 0) > Date.now(); } setLongPollingBlocked(isLongPollingBlocked) { if (!this._storage) { return false; } try { this._storage.set( pull.LsKeys.LongPollingBlocked, isLongPollingBlocked ? Date.now() + this._ttl : 0 ); } catch (error) { this.getLogger().error( `${text.Text.getDateForLog()}: Pull: Could not save LP_blocked flag in local storage.`, { error } ); return false; } return true; } isLoggingEnabled() { if (!this._storage) { return false; } return this._storage.get(pull.LsKeys.LoggingEnabled, 0) > this.getTimestamp(); } setLoggingEnabled(isLoggingEnabled) { if (!this._storage) { return false; } try { this._storage.set( pull.LsKeys.LoggingEnabled, isLoggingEnabled ? this.getTimestamp() + this._ttl : 0 ); } catch (error) { this.getLogger().error( `${text.Text.getDateForLog()}: LocalStorage error.`, { error } ); return false; } return true; } // region Tools //// getTimestamp() { return Date.now(); } // endregion //// } exports.SharedConfig = SharedConfig; //# sourceMappingURL=shared-config.cjs.map