UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

134 lines (131 loc) 3.46 kB
/** * @package @bitrix24/b24jssdk * @version 1.0.1 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ import { Type } from '../tools/type.mjs'; import { Text } from '../tools/text.mjs'; import { StorageManager } from './storage-manager.mjs'; import { LsKeys } from '../types/pull.mjs'; import { LoggerFactory } from '../logger/logger-factory.mjs'; 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.createNullLogger(); params = params || {}; this._storage = params.storage || new StorageManager(); this._callbacks = { onWebSocketBlockChanged: 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 || "", LsKeys.WebsocketBlocked ) && params.newValue !== params.oldValue) { this._callbacks.onWebSocketBlockChanged({ isWebSocketBlocked: this.isWebSocketBlocked() }); } } isWebSocketBlocked() { if (!this._storage) { return false; } return this._storage.get(LsKeys.WebsocketBlocked, 0) > Date.now(); } setWebSocketBlocked(isWebSocketBlocked) { if (!this._storage) { return false; } try { this._storage.set( LsKeys.WebsocketBlocked, isWebSocketBlocked ? Date.now() + this._ttl : 0 ); } catch (error) { this.getLogger().error( `${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(LsKeys.LongPollingBlocked, 0) > Date.now(); } setLongPollingBlocked(isLongPollingBlocked) { if (!this._storage) { return false; } try { this._storage.set( LsKeys.LongPollingBlocked, isLongPollingBlocked ? Date.now() + this._ttl : 0 ); } catch (error) { this.getLogger().error( `${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(LsKeys.LoggingEnabled, 0) > this.getTimestamp(); } setLoggingEnabled(isLoggingEnabled) { if (!this._storage) { return false; } try { this._storage.set( LsKeys.LoggingEnabled, isLoggingEnabled ? this.getTimestamp() + this._ttl : 0 ); } catch (error) { this.getLogger().error( `${Text.getDateForLog()}: LocalStorage error.`, { error } ); return false; } return true; } // region Tools //// getTimestamp() { return Date.now(); } // endregion //// } export { SharedConfig }; //# sourceMappingURL=shared-config.mjs.map