UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

158 lines (155 loc) 4.26 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 { AbstractConnector } from './abstract-connector.mjs'; import { ConnectionType } from '../types/pull.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const LONG_POLLING_TIMEOUT = 60; class LongPollingConnector extends AbstractConnector { static { __name(this, "LongPollingConnector"); } _active; _requestTimeout; _failureTimeout; _xhr; _requestAborted; constructor(config) { super(config); this._active = false; this._connectionType = ConnectionType.LongPolling; this._requestTimeout = null; this._failureTimeout = null; this._xhr = this.createXhr(); this._requestAborted = false; } /** * @inheritDoc */ connect() { this._active = true; this.performRequest(); } /** * @inheritDoc * @param code * @param reason */ disconnect(code, reason) { this._active = false; this.clearTimeOut(); if (this._xhr) { this._requestAborted = true; this._xhr.abort(); } this._disconnectCode = code; this._disconnectReason = reason; this.connected = false; } performRequest() { if (!this._active) { return; } if (!this.connectionPath) { throw new Error("Long polling connection path is not defined"); } if (this._xhr.readyState !== 0 && this._xhr.readyState !== 4) { return; } this.clearTimeOut(); this._failureTimeout = setTimeout(() => { this.connected = true; }, 5e3); this._requestTimeout = setTimeout( this.onRequestTimeout.bind(this), LONG_POLLING_TIMEOUT * 1e3 ); this._xhr.open("GET", this.connectionPath); this._xhr.send(); } onRequestTimeout() { this._requestAborted = true; this._xhr.abort(); this.performRequest(); } onXhrReadyStateChange() { if (this._xhr.readyState === 4) { if (!this._requestAborted || this._xhr.status == 200) { this.onResponse(this._xhr.response); } this._requestAborted = false; } } /** * Via http request * @inheritDoc */ send(buffer) { const path = this._parent.getPublicationPath(); if (!path) { this.getLogger().error(`${Text.getDateForLog()}: Pull: publication path is empty`); return false; } const xhr = new XMLHttpRequest(); xhr.open("POST", path); xhr.send(buffer); return true; } onResponse(response) { this.clearTimeOut(); if (this._xhr.status === 200) { this.connected = true; if (Type.isStringFilled(response) || response instanceof ArrayBuffer) { this._callbacks.onMessage(response); } else { this._parent.session.mid = null; } this.performRequest(); } else if (this._xhr.status === 304) { this.connected = true; if (this._xhr.getResponseHeader("Expires") === "Thu, 01 Jan 1973 11:11:01 GMT") { const lastMessageId = this._xhr.getResponseHeader("Last-Message-Id"); if (Type.isStringFilled(lastMessageId)) { this._parent.setLastMessageId(lastMessageId || ""); } } this.performRequest(); } else { this._callbacks.onError(new Error("Could not connect to the server")); this.connected = false; } } // region Tools //// clearTimeOut() { if (this._failureTimeout) { clearTimeout(this._failureTimeout); this._failureTimeout = null; } if (this._requestTimeout) { clearTimeout(this._requestTimeout); this._requestTimeout = null; } } createXhr() { const result = new XMLHttpRequest(); if (this._parent.isProtobufSupported() && !this._parent.isJsonRpc()) { result.responseType = "arraybuffer"; } result.addEventListener( "readystatechange", this.onXhrReadyStateChange.bind(this) ); return result; } // endregion //// } export { LongPollingConnector }; //# sourceMappingURL=long-polling-connector.mjs.map