Skip to content

Commit 9239dea

Browse files
Move supportedTransports storage into ConnectionManager instance
Preparation for #1394 (making transports tree-shakable).
1 parent a698ab8 commit 9239dea

File tree

7 files changed

+32
-26
lines changed

7 files changed

+32
-26
lines changed

src/common/lib/transport/connectionmanager.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ function decodeRecoveryKey(recoveryKey: NormalisedClientOptions['recover']): Rec
9999
}
100100
}
101101

102-
const supportedTransports: Partial<Record<TransportName, TransportCtor>> = {};
103-
104102
export class TransportParams {
105103
options: NormalisedClientOptions;
106104
host: string | null;
@@ -190,6 +188,7 @@ type ConnectionState = {
190188
};
191189

192190
class ConnectionManager extends EventEmitter {
191+
supportedTransports: Partial<Record<TransportName, TransportCtor>> = {};
193192
realtime: BaseRealtime;
194193
options: NormalisedClientOptions;
195194
states: Record<string, ConnectionState>;
@@ -228,7 +227,7 @@ class ConnectionManager extends EventEmitter {
228227

229228
constructor(realtime: BaseRealtime, options: NormalisedClientOptions) {
230229
super();
231-
ConnectionManager.initTransports();
230+
this.initTransports();
232231
this.realtime = realtime;
233232
this.options = options;
234233
const timeouts = options.timeouts;
@@ -305,10 +304,7 @@ class ConnectionManager extends EventEmitter {
305304
this.connectionStateTtl = timeouts.connectionStateTtl;
306305
this.maxIdleInterval = null;
307306

308-
this.transports = Utils.intersect(
309-
options.transports || Defaults.defaultTransports,
310-
ConnectionManager.supportedTransports
311-
);
307+
this.transports = Utils.intersect(options.transports || Defaults.defaultTransports, this.supportedTransports);
312308
/* baseTransports selects the leftmost transport in the Defaults.baseTransportOrder list
313309
* that's both requested and supported. */
314310
this.baseTransport = Utils.intersect(Defaults.baseTransportOrder, this.transports)[0];
@@ -404,18 +400,25 @@ class ConnectionManager extends EventEmitter {
404400
* transport management
405401
*********************/
406402

403+
// Used by tests
407404
static get supportedTransports() {
408-
return supportedTransports;
405+
const storage: TransportStorage = { supportedTransports: {} };
406+
this.initTransports(storage);
407+
return storage.supportedTransports;
409408
}
410409

411-
static initTransports() {
412-
WebSocketTransport(ConnectionManager);
410+
private static initTransports(storage: TransportStorage) {
411+
WebSocketTransport(storage);
413412
Utils.arrForEach(Platform.Transports.order, function (transportName) {
414413
const initFn = Platform.Transports.implementations[transportName]!;
415414
initFn(ConnectionManager);
416415
});
417416
}
418417

418+
initTransports() {
419+
ConnectionManager.initTransports(this);
420+
}
421+
419422
createTransportParams(host: string | null, mode: string): TransportParams {
420423
return new TransportParams(this.options, host, mode, this.connectionKey);
421424
}
@@ -486,7 +489,7 @@ class ConnectionManager extends EventEmitter {
486489
Logger.logAction(Logger.LOG_MICRO, 'ConnectionManager.tryATransport()', 'trying ' + candidate);
487490

488491
Transport.tryConnect(
489-
ConnectionManager.supportedTransports[candidate]!,
492+
this.supportedTransports[candidate]!,
490493
this,
491494
this.realtime.auth,
492495
transportParams,
@@ -2171,4 +2174,8 @@ class ConnectionManager extends EventEmitter {
21712174

21722175
export default ConnectionManager;
21732176

2174-
export type TransportInitialiser = (connectionManager: typeof ConnectionManager) => typeof Transport;
2177+
export interface TransportStorage {
2178+
supportedTransports: Partial<Record<TransportName, TransportCtor>>;
2179+
}
2180+
2181+
export type TransportInitialiser = (transportStorage: TransportStorage) => typeof Transport;

src/common/lib/transport/websockettransport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Logger from '../util/logger';
66
import ProtocolMessage from '../types/protocolmessage';
77
import ErrorInfo from '../types/errorinfo';
88
import NodeWebSocket from 'ws';
9-
import ConnectionManager, { TransportParams } from './connectionmanager';
9+
import ConnectionManager, { TransportParams, TransportStorage } from './connectionmanager';
1010
import Auth from '../client/auth';
1111
import { TransportNames } from 'common/constants/TransportName';
1212

@@ -196,8 +196,8 @@ class WebSocketTransport extends Transport {
196196
}
197197
}
198198

199-
function initialiseTransport(connectionManager: typeof ConnectionManager): typeof WebSocketTransport {
200-
if (WebSocketTransport.isAvailable()) connectionManager.supportedTransports[shortName] = WebSocketTransport;
199+
function initialiseTransport(transportStorage: TransportStorage): typeof WebSocketTransport {
200+
if (WebSocketTransport.isAvailable()) transportStorage.supportedTransports[shortName] = WebSocketTransport;
201201

202202
return WebSocketTransport;
203203
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import ConnectionManager from '../../../../common/lib/transport/connectionmanager';
1+
import { TransportStorage } from '../../../../common/lib/transport/connectionmanager';
22
import Transport from '../../../../common/lib/transport/transport';
33

4-
declare function initialiseNodeCometTransport(connectionManager: typeof ConnectionManager): typeof Transport;
4+
declare function initialiseNodeCometTransport(transportStorage: TransportStorage): typeof Transport;
55
export default initialiseNodeCometTransport;

src/platform/nodejs/lib/transport/nodecomettransport.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import url from 'url';
1212
import util from 'util';
1313
import { TransportNames } from '../../../../common/constants/TransportName';
1414

15-
var NodeCometTransport = function (connectionManager) {
15+
var NodeCometTransport = function (transportStorage) {
1616
var noop = function () {};
1717
var shortName = TransportNames.Comet;
1818

@@ -32,7 +32,7 @@ var NodeCometTransport = function (connectionManager) {
3232
NodeCometTransport.isAvailable = function () {
3333
return true;
3434
};
35-
connectionManager.supportedTransports[shortName] = NodeCometTransport;
35+
transportStorage.supportedTransports[shortName] = NodeCometTransport;
3636

3737
NodeCometTransport.prototype.toString = function () {
3838
return (

src/platform/web/lib/transport/xhrpollingtransport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Platform from '../../../../common/platform';
22
import CometTransport from '../../../../common/lib/transport/comettransport';
33
import XHRRequest from './xhrrequest';
4-
import ConnectionManager, { TransportParams } from 'common/lib/transport/connectionmanager';
4+
import ConnectionManager, { TransportParams, TransportStorage } from 'common/lib/transport/connectionmanager';
55
import Auth from 'common/lib/client/auth';
66
import { RequestParams } from 'common/types/http';
77
import { TransportNames } from 'common/constants/TransportName';
@@ -34,8 +34,8 @@ class XHRPollingTransport extends CometTransport {
3434
}
3535
}
3636

37-
function initialiseTransport(connectionManager: typeof ConnectionManager): typeof XHRPollingTransport {
38-
if (XHRPollingTransport.isAvailable()) connectionManager.supportedTransports[shortName] = XHRPollingTransport;
37+
function initialiseTransport(transportStorage: TransportStorage): typeof XHRPollingTransport {
38+
if (XHRPollingTransport.isAvailable()) transportStorage.supportedTransports[shortName] = XHRPollingTransport;
3939

4040
return XHRPollingTransport;
4141
}

src/platform/web/lib/transport/xhrstreamingtransport.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import CometTransport from '../../../../common/lib/transport/comettransport';
22
import Platform from '../../../../common/platform';
33
import XHRRequest from './xhrrequest';
4-
import ConnectionManager, { TransportParams } from 'common/lib/transport/connectionmanager';
4+
import ConnectionManager, { TransportParams, TransportStorage } from 'common/lib/transport/connectionmanager';
55
import Auth from 'common/lib/client/auth';
66
import { RequestParams } from 'common/types/http';
77
import { TransportNames } from 'common/constants/TransportName';
@@ -32,8 +32,8 @@ class XHRStreamingTransport extends CometTransport {
3232
}
3333
}
3434

35-
function initialiseTransport(connectionManager: typeof ConnectionManager): typeof XHRStreamingTransport {
36-
if (XHRStreamingTransport.isAvailable()) connectionManager.supportedTransports[shortName] = XHRStreamingTransport;
35+
function initialiseTransport(transportStorage: TransportStorage): typeof XHRStreamingTransport {
36+
if (XHRStreamingTransport.isAvailable()) transportStorage.supportedTransports[shortName] = XHRStreamingTransport;
3737

3838
return XHRStreamingTransport;
3939
}

test/common/modules/shared_helper.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ define([
1414
var platform = clientModule.Ably.Realtime.Platform;
1515
var BufferUtils = platform.BufferUtils;
1616
var expect = chai.expect;
17-
clientModule.Ably.Realtime.ConnectionManager.initTransports();
1817
var availableTransports = utils.keysArray(clientModule.Ably.Realtime.ConnectionManager.supportedTransports),
1918
bestTransport = availableTransports[0],
2019
/* IANA reserved; requests to it will hang forever */

0 commit comments

Comments
 (0)