Skip to content

Commit

Permalink
Decouple Platform.Transport’s transport order from implementations
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 5, 2023
1 parent d29233f commit a62db08
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/common/lib/transport/connectionmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ class ConnectionManager extends EventEmitter {

static initTransports() {
WebSocketTransport(ConnectionManager);
Utils.arrForEach(Platform.Transports, function (initFn) {
Utils.arrForEach(Platform.Transports.order, function (transportName) {
const initFn = Platform.Transports.implementations[transportName]!;
initFn(ConnectionManager);
});
}
Expand Down
8 changes: 7 additions & 1 deletion src/common/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import IBufferUtils from './types/IBufferUtils';
import * as WebBufferUtils from '../platform/web/lib/util/bufferutils';
import * as NodeBufferUtils from '../platform/nodejs/lib/util/bufferutils';
import { IUntypedCryptoStatic } from '../common/types/ICryptoStatic';
import TransportName from './constants/TransportName';

type Bufferlike = WebBufferUtils.Bufferlike | NodeBufferUtils.Bufferlike;
type BufferUtilsOutput = WebBufferUtils.Output | NodeBufferUtils.Output;
type ToBufferOutput = WebBufferUtils.ToBufferOutput | NodeBufferUtils.ToBufferOutput;

export type TransportImplementations = Partial<Record<TransportName, TransportInitialiser>>;

export default class Platform {
static Config: IPlatformConfig;
/*
Expand All @@ -29,7 +32,10 @@ export default class Platform {
*/
static Crypto: IUntypedCryptoStatic | null;
static Http: typeof IHttp;
static Transports: TransportInitialiser[];
static Transports: {
order: TransportName[];
implementations: TransportImplementations;
};
static Defaults: IDefaults;
static WebStorage: IWebStorage | null;
}
6 changes: 5 additions & 1 deletion src/platform/nodejs/lib/transport/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import TransportName from 'common/constants/TransportName';
import initialiseNodeCometTransport from './nodecomettransport';

export default [initialiseNodeCometTransport];
export default {
order: [TransportName.Comet],
implementations: { [TransportName.Comet]: initialiseNodeCometTransport },
};
9 changes: 8 additions & 1 deletion src/platform/web/lib/transport/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import TransportName from 'common/constants/TransportName';
import initialiseXHRPollingTransport from './xhrpollingtransport';
import initialiseXHRStreamingTransport from './xhrstreamingtransport';

export default [initialiseXHRPollingTransport, initialiseXHRStreamingTransport];
export default {
order: [TransportName.XhrPolling, TransportName.XhrStreaming],
implementations: {
[TransportName.XhrPolling]: initialiseXHRPollingTransport,
[TransportName.XhrStreaming]: initialiseXHRStreamingTransport,
},
};

0 comments on commit a62db08

Please sign in to comment.