From 7cb13b94229d278f451409a7a857d5180b950864 Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Thu, 24 Aug 2023 10:33:53 -0300 Subject: [PATCH] Move supportedTransports storage into ConnectionManager instance Preparation for #1394 (making transports tree-shakable). --- src/common/lib/transport/connectionmanager.ts | 31 ++++++++++++------- .../lib/transport/websockettransport.ts | 6 ++-- .../lib/transport/nodecomettransport.d.ts | 4 +-- .../lib/transport/nodecomettransport.js | 4 +-- .../web/lib/transport/xhrpollingtransport.ts | 6 ++-- .../lib/transport/xhrstreamingtransport.ts | 6 ++-- test/common/modules/shared_helper.js | 1 - 7 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/common/lib/transport/connectionmanager.ts b/src/common/lib/transport/connectionmanager.ts index cc6b1d6637..5e5845bcb5 100644 --- a/src/common/lib/transport/connectionmanager.ts +++ b/src/common/lib/transport/connectionmanager.ts @@ -99,8 +99,6 @@ function decodeRecoveryKey(recoveryKey: NormalisedClientOptions['recover']): Rec } } -const supportedTransports: Partial> = {}; - export class TransportParams { options: NormalisedClientOptions; host: string | null; @@ -190,6 +188,7 @@ type ConnectionState = { }; class ConnectionManager extends EventEmitter { + supportedTransports: Partial> = {}; realtime: BaseRealtime; options: NormalisedClientOptions; states: Record; @@ -228,7 +227,7 @@ class ConnectionManager extends EventEmitter { constructor(realtime: BaseRealtime, options: NormalisedClientOptions) { super(); - ConnectionManager.initTransports(); + this.initTransports(); this.realtime = realtime; this.options = options; const timeouts = options.timeouts; @@ -305,10 +304,7 @@ class ConnectionManager extends EventEmitter { this.connectionStateTtl = timeouts.connectionStateTtl; this.maxIdleInterval = null; - this.transports = Utils.intersect( - options.transports || Defaults.defaultTransports, - ConnectionManager.supportedTransports - ); + this.transports = Utils.intersect(options.transports || Defaults.defaultTransports, this.supportedTransports); /* baseTransports selects the leftmost transport in the Defaults.baseTransportOrder list * that's both requested and supported. */ this.baseTransport = Utils.intersect(Defaults.baseTransportOrder, this.transports)[0]; @@ -404,18 +400,25 @@ class ConnectionManager extends EventEmitter { * transport management *********************/ + // Used by tests static get supportedTransports() { - return supportedTransports; + const storage: TransportStorage = { supportedTransports: {} }; + this.initTransports(storage); + return storage.supportedTransports; } - static initTransports() { - WebSocketTransport(ConnectionManager); + private static initTransports(storage: TransportStorage) { + WebSocketTransport(storage); Utils.arrForEach(Platform.Transports.order, function (transportName) { const initFn = Platform.Transports.implementations[transportName]!; initFn(ConnectionManager); }); } + initTransports() { + ConnectionManager.initTransports(this); + } + createTransportParams(host: string | null, mode: string): TransportParams { return new TransportParams(this.options, host, mode, this.connectionKey); } @@ -486,7 +489,7 @@ class ConnectionManager extends EventEmitter { Logger.logAction(Logger.LOG_MICRO, 'ConnectionManager.tryATransport()', 'trying ' + candidate); Transport.tryConnect( - ConnectionManager.supportedTransports[candidate]!, + this.supportedTransports[candidate]!, this, this.realtime.auth, transportParams, @@ -2171,4 +2174,8 @@ class ConnectionManager extends EventEmitter { export default ConnectionManager; -export type TransportInitialiser = (connectionManager: typeof ConnectionManager) => typeof Transport; +export interface TransportStorage { + supportedTransports: Partial>; +} + +export type TransportInitialiser = (transportStorage: TransportStorage) => typeof Transport; diff --git a/src/common/lib/transport/websockettransport.ts b/src/common/lib/transport/websockettransport.ts index 2324fc3021..cef583bd59 100644 --- a/src/common/lib/transport/websockettransport.ts +++ b/src/common/lib/transport/websockettransport.ts @@ -6,7 +6,7 @@ import Logger from '../util/logger'; import ProtocolMessage from '../types/protocolmessage'; import ErrorInfo from '../types/errorinfo'; import NodeWebSocket from 'ws'; -import ConnectionManager, { TransportParams } from './connectionmanager'; +import ConnectionManager, { TransportParams, TransportStorage } from './connectionmanager'; import Auth from '../client/auth'; import { TransportNames } from 'common/constants/TransportName'; @@ -196,8 +196,8 @@ class WebSocketTransport extends Transport { } } -function initialiseTransport(connectionManager: typeof ConnectionManager): typeof WebSocketTransport { - if (WebSocketTransport.isAvailable()) connectionManager.supportedTransports[shortName] = WebSocketTransport; +function initialiseTransport(transportStorage: TransportStorage): typeof WebSocketTransport { + if (WebSocketTransport.isAvailable()) transportStorage.supportedTransports[shortName] = WebSocketTransport; return WebSocketTransport; } diff --git a/src/platform/nodejs/lib/transport/nodecomettransport.d.ts b/src/platform/nodejs/lib/transport/nodecomettransport.d.ts index 9067155ab9..f90fa468b0 100644 --- a/src/platform/nodejs/lib/transport/nodecomettransport.d.ts +++ b/src/platform/nodejs/lib/transport/nodecomettransport.d.ts @@ -1,5 +1,5 @@ -import ConnectionManager from '../../../../common/lib/transport/connectionmanager'; +import { TransportStorage } from '../../../../common/lib/transport/connectionmanager'; import Transport from '../../../../common/lib/transport/transport'; -declare function initialiseNodeCometTransport(connectionManager: typeof ConnectionManager): typeof Transport; +declare function initialiseNodeCometTransport(transportStorage: TransportStorage): typeof Transport; export default initialiseNodeCometTransport; diff --git a/src/platform/nodejs/lib/transport/nodecomettransport.js b/src/platform/nodejs/lib/transport/nodecomettransport.js index 6e6655bb0d..9a1c29411d 100644 --- a/src/platform/nodejs/lib/transport/nodecomettransport.js +++ b/src/platform/nodejs/lib/transport/nodecomettransport.js @@ -12,7 +12,7 @@ import url from 'url'; import util from 'util'; import { TransportNames } from '../../../../common/constants/TransportName'; -var NodeCometTransport = function (connectionManager) { +var NodeCometTransport = function (transportStorage) { var noop = function () {}; var shortName = TransportNames.Comet; @@ -32,7 +32,7 @@ var NodeCometTransport = function (connectionManager) { NodeCometTransport.isAvailable = function () { return true; }; - connectionManager.supportedTransports[shortName] = NodeCometTransport; + transportStorage.supportedTransports[shortName] = NodeCometTransport; NodeCometTransport.prototype.toString = function () { return ( diff --git a/src/platform/web/lib/transport/xhrpollingtransport.ts b/src/platform/web/lib/transport/xhrpollingtransport.ts index c063397198..4d3b2110d3 100644 --- a/src/platform/web/lib/transport/xhrpollingtransport.ts +++ b/src/platform/web/lib/transport/xhrpollingtransport.ts @@ -1,7 +1,7 @@ import Platform from '../../../../common/platform'; import CometTransport from '../../../../common/lib/transport/comettransport'; import XHRRequest from './xhrrequest'; -import ConnectionManager, { TransportParams } from 'common/lib/transport/connectionmanager'; +import ConnectionManager, { TransportParams, TransportStorage } from 'common/lib/transport/connectionmanager'; import Auth from 'common/lib/client/auth'; import { RequestParams } from 'common/types/http'; import { TransportNames } from 'common/constants/TransportName'; @@ -34,8 +34,8 @@ class XHRPollingTransport extends CometTransport { } } -function initialiseTransport(connectionManager: typeof ConnectionManager): typeof XHRPollingTransport { - if (XHRPollingTransport.isAvailable()) connectionManager.supportedTransports[shortName] = XHRPollingTransport; +function initialiseTransport(transportStorage: TransportStorage): typeof XHRPollingTransport { + if (XHRPollingTransport.isAvailable()) transportStorage.supportedTransports[shortName] = XHRPollingTransport; return XHRPollingTransport; } diff --git a/src/platform/web/lib/transport/xhrstreamingtransport.ts b/src/platform/web/lib/transport/xhrstreamingtransport.ts index 57cf30e7b7..9ef8631fec 100644 --- a/src/platform/web/lib/transport/xhrstreamingtransport.ts +++ b/src/platform/web/lib/transport/xhrstreamingtransport.ts @@ -1,7 +1,7 @@ import CometTransport from '../../../../common/lib/transport/comettransport'; import Platform from '../../../../common/platform'; import XHRRequest from './xhrrequest'; -import ConnectionManager, { TransportParams } from 'common/lib/transport/connectionmanager'; +import ConnectionManager, { TransportParams, TransportStorage } from 'common/lib/transport/connectionmanager'; import Auth from 'common/lib/client/auth'; import { RequestParams } from 'common/types/http'; import { TransportNames } from 'common/constants/TransportName'; @@ -32,8 +32,8 @@ class XHRStreamingTransport extends CometTransport { } } -function initialiseTransport(connectionManager: typeof ConnectionManager): typeof XHRStreamingTransport { - if (XHRStreamingTransport.isAvailable()) connectionManager.supportedTransports[shortName] = XHRStreamingTransport; +function initialiseTransport(transportStorage: TransportStorage): typeof XHRStreamingTransport { + if (XHRStreamingTransport.isAvailable()) transportStorage.supportedTransports[shortName] = XHRStreamingTransport; return XHRStreamingTransport; } diff --git a/test/common/modules/shared_helper.js b/test/common/modules/shared_helper.js index 58ad02989e..8c3eb6c16d 100644 --- a/test/common/modules/shared_helper.js +++ b/test/common/modules/shared_helper.js @@ -14,7 +14,6 @@ define([ var platform = clientModule.Ably.Realtime.Platform; var BufferUtils = platform.BufferUtils; var expect = chai.expect; - clientModule.Ably.Realtime.ConnectionManager.initTransports(); var availableTransports = utils.keysArray(clientModule.Ably.Realtime.ConnectionManager.supportedTransports), bestTransport = availableTransports[0], /* IANA reserved; requests to it will hang forever */