Skip to content

Commit

Permalink
Move supportedTransports storage into ConnectionManager instance
Browse files Browse the repository at this point in the history
Preparation for #1394 (making transports tree-shakable).
  • Loading branch information
lawrence-forooghian committed Sep 14, 2023
1 parent 8a9faaa commit 7cb13b9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 26 deletions.
31 changes: 19 additions & 12 deletions src/common/lib/transport/connectionmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ function decodeRecoveryKey(recoveryKey: NormalisedClientOptions['recover']): Rec
}
}

const supportedTransports: Partial<Record<TransportName, TransportCtor>> = {};

export class TransportParams {
options: NormalisedClientOptions;
host: string | null;
Expand Down Expand Up @@ -190,6 +188,7 @@ type ConnectionState = {
};

class ConnectionManager extends EventEmitter {
supportedTransports: Partial<Record<TransportName, TransportCtor>> = {};
realtime: BaseRealtime;
options: NormalisedClientOptions;
states: Record<string, ConnectionState>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2171,4 +2174,8 @@ class ConnectionManager extends EventEmitter {

export default ConnectionManager;

export type TransportInitialiser = (connectionManager: typeof ConnectionManager) => typeof Transport;
export interface TransportStorage {
supportedTransports: Partial<Record<TransportName, TransportCtor>>;
}

export type TransportInitialiser = (transportStorage: TransportStorage) => typeof Transport;
6 changes: 3 additions & 3 deletions src/common/lib/transport/websockettransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/nodejs/lib/transport/nodecomettransport.d.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions src/platform/nodejs/lib/transport/nodecomettransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 (
Expand Down
6 changes: 3 additions & 3 deletions src/platform/web/lib/transport/xhrpollingtransport.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/platform/web/lib/transport/xhrstreamingtransport.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion test/common/modules/shared_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 7cb13b9

Please sign in to comment.