UNPKG

@bbc/sofie-server-core-integration

Version:
166 lines 5.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DDPConnector = void 0; const events_1 = require("events"); const ddpClient_js_1 = require("./ddpClient.js"); class DDPConnector extends events_1.EventEmitter { ddpClient; _options; _connected = false; _connecting = false; _connectionId = undefined; ddpIsOpen = false; _monitorDDPConnectionInterval = null; constructor(options) { super(); this._options = options; } async createClient() { const o = { host: this._options.host, port: this._options.port, headers: this._options.headers, path: this._options.path || '', ssl: this._options.ssl || false, tlsOpts: this._options.tlsOpts || {}, useSockJs: true, autoReconnect: false, // we'll handle reconnections ourselves autoReconnectTimer: 1000, maintainCollections: true, ddpVersion: '1', }; let doConnect = false; if (!this.ddpClient) { this.ddpClient = new ddpClient_js_1.DDPClient(o); this.ddpClient.on('socket-close', () => { this._onclientConnectionChange(false); }); this.ddpClient.on('message', (message) => this._onClientMessage(message)); this.ddpClient.on('socket-error', (error) => this._onClientError(error)); } else { if (this.ddpClient.socket) { this.ddpClient.close(); } this.ddpClient.resetOptions(o); doConnect = true; } this._setupDDPEvents(); if (doConnect) { return new Promise((resolve, reject) => { this.ddpClient?.connect((err) => { // connected if (err) reject(err); else resolve(); }); }); } else { return Promise.resolve(); } } async connect() { return (!this.ddpClient ? this.createClient() : Promise.resolve()) .then(async () => { return new Promise((resolve, reject) => { if (this.ddpClient && !this._connecting) { if (this.ddpClient.socket) { this.ddpClient.close(); } this._setupDDPEvents(); this._connecting = true; this.ddpClient.connect((error /*, isReconnecting: boolean*/) => { this._connecting = false; if (error) { reject(error); } else { this._connected = true; resolve(); this.ddpIsOpen = true; this._monitorDDPConnection(); } }); } }); }) .then(() => { return; }); } close() { this.ddpIsOpen = false; if (this.ddpClient) { this.ddpClient.close(); delete this.ddpClient; } this._onclientConnectionChange(false); } get connected() { return this._connected; } async forceReconnect() { return this.createClient(); } get connectionId() { return this._connectionId; } _setupDDPEvents() { this.ddpClient?.on('connected', () => this._onclientConnectionChange(true)); this.ddpClient?.on('failed', (error) => this._onClientConnectionFailed(error)); } _monitorDDPConnection() { if (this._monitorDDPConnectionInterval) clearInterval(this._monitorDDPConnectionInterval); this._monitorDDPConnectionInterval = setInterval(() => { if (this.ddpClient && !this.connected && this.ddpIsOpen && this._options.autoReconnect !== false) { // Time to reconnect this.createClient().catch((e) => { this.emit('error', e); }); } else { // stop monitoring: if (this._monitorDDPConnectionInterval) clearInterval(this._monitorDDPConnectionInterval); } }, this._options.autoReconnectTimer || 1000); } _onclientConnectionChange(connected) { if (connected !== this._connected) { this._connected = connected; if (connected) { this._connectionId = this.ddpClient?.session; } // log.debug("DDP: _onclientConnectionChange "+connected); this.emit('connectionChanged', this._connected); if (this._connected) this.emit('connected'); else this.emit('disconnected'); if (!this._connected) this._monitorDDPConnection(); } } _onClientConnectionFailed(error) { if (this.listenerCount('failed') > 0) { this.emit('failed', error); } else { console.log('Failed', error); } this._monitorDDPConnection(); } _onClientMessage(message) { // message this.emit('message', message); } _onClientError(error) { this.emit('error', error); this._monitorDDPConnection(); } } exports.DDPConnector = DDPConnector; //# sourceMappingURL=ddpConnector.js.map