Skip to content

Commit a395593

Browse files
Merge pull request #1503 from ably/remove-static-from-Message-classes
Remove `static` stuff from `*Message` classes
2 parents 9ec4247 + 875282c commit a395593

25 files changed

+453
-389
lines changed

src/common/lib/client/realtimechannel.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
import ProtocolMessage from '../types/protocolmessage';
1+
import ProtocolMessage, {
2+
actions,
3+
channelModes,
4+
fromValues as protocolMessageFromValues,
5+
} from '../types/protocolmessage';
26
import EventEmitter from '../util/eventemitter';
37
import * as Utils from '../util/utils';
48
import Logger from '../util/logger';
59
import RealtimePresence from './realtimepresence';
6-
import Message, { CipherOptions } from '../types/message';
10+
import Message, {
11+
fromValues as messageFromValues,
12+
fromValuesArray as messagesFromValuesArray,
13+
encodeArray as encodeMessagesArray,
14+
decode as decodeMessage,
15+
getMessagesSize,
16+
CipherOptions,
17+
} from '../types/message';
718
import ChannelStateChange from './channelstatechange';
819
import ErrorInfo, { IPartialErrorInfo, PartialErrorInfo } from '../types/errorinfo';
9-
import PresenceMessage, { fromValues as presenceMessageFromValues } from '../types/presencemessage';
20+
import PresenceMessage, {
21+
fromValues as presenceMessageFromValues,
22+
fromValuesArray as presenceMessagesFromValuesArray,
23+
decode as decodePresenceMessage,
24+
} from '../types/presencemessage';
1025
import ConnectionErrors from '../transport/connectionerrors';
1126
import * as API from '../../../../ably';
1227
import ConnectionManager from '../transport/connectionmanager';
@@ -25,7 +40,6 @@ interface RealtimeHistoryParams {
2540
from_serial?: string;
2641
}
2742

28-
const actions = ProtocolMessage.Action;
2943
const noop = function () {};
3044

3145
function validateChannelOptions(options?: API.Types.ChannelOptions) {
@@ -41,7 +55,7 @@ function validateChannelOptions(options?: API.Types.ChannelOptions) {
4155
if (
4256
!currentMode ||
4357
typeof currentMode !== 'string' ||
44-
!Utils.arrIn(ProtocolMessage.channelModes, String.prototype.toUpperCase.call(currentMode))
58+
!Utils.arrIn(channelModes, String.prototype.toUpperCase.call(currentMode))
4559
) {
4660
return new ErrorInfo('Invalid channel mode: ' + currentMode, 40000, 400);
4761
}
@@ -230,25 +244,25 @@ class RealtimeChannel extends EventEmitter {
230244
return;
231245
}
232246
if (argCount == 2) {
233-
if (Utils.isObject(messages)) messages = [Message.fromValues(messages)];
234-
else if (Utils.isArray(messages)) messages = Message.fromValuesArray(messages);
247+
if (Utils.isObject(messages)) messages = [messageFromValues(messages)];
248+
else if (Utils.isArray(messages)) messages = messagesFromValuesArray(messages);
235249
else
236250
throw new ErrorInfo(
237251
'The single-argument form of publish() expects a message object or an array of message objects',
238252
40013,
239253
400
240254
);
241255
} else {
242-
messages = [Message.fromValues({ name: args[0], data: args[1] })];
256+
messages = [messageFromValues({ name: args[0], data: args[1] })];
243257
}
244258
const maxMessageSize = this.client.options.maxMessageSize;
245-
Message.encodeArray(messages, this.channelOptions as CipherOptions, (err: Error | null) => {
259+
encodeMessagesArray(messages, this.channelOptions as CipherOptions, (err: Error | null) => {
246260
if (err) {
247261
callback(err);
248262
return;
249263
}
250264
/* RSL1i */
251-
const size = Message.getMessagesSize(messages);
265+
const size = getMessagesSize(messages);
252266
if (size > maxMessageSize) {
253267
callback(
254268
new ErrorInfo(
@@ -354,7 +368,7 @@ class RealtimeChannel extends EventEmitter {
354368

355369
attachImpl(): void {
356370
Logger.logAction(Logger.LOG_MICRO, 'RealtimeChannel.attachImpl()', 'sending ATTACH message');
357-
const attachMsg = ProtocolMessage.fromValues({
371+
const attachMsg = protocolMessageFromValues({
358372
action: actions.ATTACH,
359373
channel: this.name,
360374
params: this.channelOptions.params,
@@ -424,7 +438,7 @@ class RealtimeChannel extends EventEmitter {
424438

425439
detachImpl(callback?: ErrCallback): void {
426440
Logger.logAction(Logger.LOG_MICRO, 'RealtimeChannel.detach()', 'sending DETACH message');
427-
const msg = ProtocolMessage.fromValues({ action: actions.DETACH, channel: this.name });
441+
const msg = protocolMessageFromValues({ action: actions.DETACH, channel: this.name });
428442
this.sendMessage(msg, callback || noop);
429443
}
430444

@@ -479,7 +493,7 @@ class RealtimeChannel extends EventEmitter {
479493
}
480494

481495
/* send sync request */
482-
const syncMessage = ProtocolMessage.fromValues({ action: actions.SYNC, channel: this.name });
496+
const syncMessage = protocolMessageFromValues({ action: actions.SYNC, channel: this.name });
483497
if (this.syncChannelSerial) {
484498
syncMessage.channelSerial = this.syncChannelSerial;
485499
}
@@ -491,11 +505,11 @@ class RealtimeChannel extends EventEmitter {
491505
}
492506

493507
sendPresence(presence: PresenceMessage | PresenceMessage[], callback?: ErrCallback): void {
494-
const msg = ProtocolMessage.fromValues({
508+
const msg = protocolMessageFromValues({
495509
action: actions.PRESENCE,
496510
channel: this.name,
497511
presence: Utils.isArray(presence)
498-
? PresenceMessage.fromValuesArray(presence)
512+
? presenceMessagesFromValuesArray(presence)
499513
: [presenceMessageFromValues(presence)],
500514
});
501515
this.sendMessage(msg, callback);
@@ -579,7 +593,7 @@ class RealtimeChannel extends EventEmitter {
579593
for (let i = 0; i < presence.length; i++) {
580594
try {
581595
presenceMsg = presence[i];
582-
await PresenceMessage.decode(presenceMsg, options);
596+
await decodePresenceMessage(presenceMsg, options);
583597
if (!presenceMsg.connectionId) presenceMsg.connectionId = connectionId;
584598
if (!presenceMsg.timestamp) presenceMsg.timestamp = timestamp;
585599
if (!presenceMsg.id) presenceMsg.id = id + ':' + i;
@@ -635,7 +649,7 @@ class RealtimeChannel extends EventEmitter {
635649
for (let i = 0; i < messages.length; i++) {
636650
const msg = messages[i];
637651
try {
638-
await Message.decode(msg, this._decodingContext);
652+
await decodeMessage(msg, this._decodingContext);
639653
} catch (e) {
640654
/* decrypt failed .. the most likely cause is that we have the wrong key */
641655
Logger.logAction(Logger.LOG_ERROR, 'RealtimeChannel.processMessage()', (e as Error).toString());

src/common/lib/client/realtimepresence.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import * as Utils from '../util/utils';
22
import EventEmitter from '../util/eventemitter';
33
import Logger from '../util/logger';
4-
import PresenceMessage, { fromValues as presenceMessageFromValues } from '../types/presencemessage';
4+
import PresenceMessage, {
5+
fromValues as presenceMessageFromValues,
6+
fromData as presenceMessageFromData,
7+
encode as encodePresenceMessage,
8+
} from '../types/presencemessage';
59
import ErrorInfo, { IPartialErrorInfo, PartialErrorInfo } from '../types/errorinfo';
610
import RealtimeChannel from './realtimechannel';
711
import Multicaster from '../util/multicaster';
@@ -147,7 +151,7 @@ class RealtimePresence extends EventEmitter {
147151
'channel = ' + channel.name + ', id = ' + id + ', client = ' + (clientId || '(implicit) ' + getClientId(this))
148152
);
149153

150-
const presence = PresenceMessage.fromData(data);
154+
const presence = presenceMessageFromData(data);
151155
presence.action = action;
152156
if (id) {
153157
presence.id = id;
@@ -156,7 +160,7 @@ class RealtimePresence extends EventEmitter {
156160
presence.clientId = clientId;
157161
}
158162

159-
PresenceMessage.encode(presence, channel.channelOptions as CipherOptions, (err: IPartialErrorInfo) => {
163+
encodePresenceMessage(presence, channel.channelOptions as CipherOptions, (err: IPartialErrorInfo) => {
160164
if (err) {
161165
callback(err);
162166
return;
@@ -214,7 +218,7 @@ class RealtimePresence extends EventEmitter {
214218
'RealtimePresence.leaveClient()',
215219
'leaving; channel = ' + this.channel.name + ', client = ' + clientId
216220
);
217-
const presence = PresenceMessage.fromData(data);
221+
const presence = presenceMessageFromData(data);
218222
presence.action = 'leave';
219223
if (clientId) {
220224
presence.clientId = clientId;

src/common/lib/client/restchannel.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import * as Utils from '../util/utils';
22
import Logger from '../util/logger';
33
import RestPresence from './restpresence';
4-
import Message, { CipherOptions } from '../types/message';
4+
import Message, {
5+
fromValues as messageFromValues,
6+
fromValuesArray as messagesFromValuesArray,
7+
encodeArray as encodeMessagesArray,
8+
serialize as serializeMessage,
9+
getMessagesSize,
10+
CipherOptions,
11+
} from '../types/message';
512
import ErrorInfo from '../types/errorinfo';
613
import { PaginatedResult } from './paginatedresource';
714
import Resource, { ResourceCallback } from './resource';
@@ -70,13 +77,13 @@ class RestChannel {
7077

7178
if (typeof first === 'string' || first === null) {
7279
/* (name, data, ...) */
73-
messages = [Message.fromValues({ name: first, data: second })];
80+
messages = [messageFromValues({ name: first, data: second })];
7481
params = arguments[2];
7582
} else if (Utils.isObject(first)) {
76-
messages = [Message.fromValues(first)];
83+
messages = [messageFromValues(first)];
7784
params = arguments[1];
7885
} else if (Utils.isArray(first)) {
79-
messages = Message.fromValuesArray(first);
86+
messages = messagesFromValuesArray(first);
8087
params = arguments[1];
8188
} else {
8289
throw new ErrorInfo(
@@ -106,14 +113,14 @@ class RestChannel {
106113
});
107114
}
108115

109-
Message.encodeArray(messages, this.channelOptions as CipherOptions, (err: Error) => {
116+
encodeMessagesArray(messages, this.channelOptions as CipherOptions, (err: Error) => {
110117
if (err) {
111118
callback(err);
112119
return;
113120
}
114121

115122
/* RSL1i */
116-
const size = Message.getMessagesSize(messages),
123+
const size = getMessagesSize(messages),
117124
maxMessageSize = options.maxMessageSize;
118125
if (size > maxMessageSize) {
119126
callback(
@@ -130,7 +137,7 @@ class RestChannel {
130137
return;
131138
}
132139

133-
this._publish(Message.serialize(messages, client._MsgPack, format), headers, params, callback);
140+
this._publish(serializeMessage(messages, client._MsgPack, format), headers, params, callback);
134141
});
135142
}
136143

src/common/lib/client/restchannelmixin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import RestChannel from './restchannel';
33
import RealtimeChannel from './realtimechannel';
44
import * as Utils from '../util/utils';
55
import { PaginatedResultCallback, StandardCallback } from '../../types/utils';
6-
import Message from '../types/message';
6+
import Message, { fromResponseBody as messageFromResponseBody } from '../types/message';
77
import Defaults from '../util/defaults';
88
import PaginatedResource from './paginatedresource';
99
import Resource from './resource';
@@ -40,7 +40,7 @@ export class RestChannelMixin {
4040
headers,
4141
unpacked
4242
) {
43-
return await Message.fromResponseBody(body as Message[], options, client._MsgPack, unpacked ? undefined : format);
43+
return await messageFromResponseBody(body as Message[], options, client._MsgPack, unpacked ? undefined : format);
4444
}).get(params as Record<string, unknown>, callback);
4545
}
4646

src/common/lib/client/restpresence.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as Utils from '../util/utils';
22
import Logger from '../util/logger';
33
import PaginatedResource, { PaginatedResult } from './paginatedresource';
4-
import PresenceMessage from '../types/presencemessage';
4+
import PresenceMessage, { fromResponseBody as presenceMessageFromResponseBody } from '../types/presencemessage';
55
import { CipherOptions } from '../types/message';
66
import { PaginatedResultCallback } from '../../types/utils';
77
import RestChannel from './restchannel';
@@ -39,7 +39,7 @@ class RestPresence {
3939
headers,
4040
envelope,
4141
async function (body, headers, unpacked) {
42-
return await PresenceMessage.fromResponseBody(
42+
return await presenceMessageFromResponseBody(
4343
body as Record<string, unknown>[],
4444
options as CipherOptions,
4545
client._MsgPack,

src/common/lib/client/restpresencemixin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as Utils from '../util/utils';
44
import { PaginatedResultCallback } from '../../types/utils';
55
import Defaults from '../util/defaults';
66
import PaginatedResource, { PaginatedResult } from './paginatedresource';
7-
import PresenceMessage from '../types/presencemessage';
7+
import PresenceMessage, { fromResponseBody as presenceMessageFromResponseBody } from '../types/presencemessage';
88
import { CipherOptions } from '../types/message';
99
import { RestChannelMixin } from './restchannelmixin';
1010

@@ -41,7 +41,7 @@ export class RestPresenceMixin {
4141
headers,
4242
unpacked
4343
) {
44-
return await PresenceMessage.fromResponseBody(
44+
return await presenceMessageFromResponseBody(
4545
body as Record<string, unknown>[],
4646
options as CipherOptions,
4747
client._MsgPack,

src/common/lib/transport/comettransport.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import * as Utils from '../util/utils';
2-
import ProtocolMessage from '../types/protocolmessage';
2+
import ProtocolMessage, {
3+
actions,
4+
fromValues as protocolMessageFromValues,
5+
fromDeserialized as protocolMessageFromDeserialized,
6+
} from '../types/protocolmessage';
37
import Transport from './transport';
48
import Logger from '../util/logger';
59
import Defaults from '../util/defaults';
@@ -29,9 +33,9 @@ function protocolMessageFromRawError(err: ErrorInfo) {
2933
/* err will be either a legacy (non-protocolmessage) comet error response
3034
* (which will have an err.code), or a xhr/network error (which won't). */
3135
if (shouldBeErrorAction(err)) {
32-
return [ProtocolMessage.fromValues({ action: ProtocolMessage.Action.ERROR, error: err })];
36+
return [protocolMessageFromValues({ action: actions.ERROR, error: err })];
3337
} else {
34-
return [ProtocolMessage.fromValues({ action: ProtocolMessage.Action.DISCONNECTED, error: err })];
38+
return [protocolMessageFromValues({ action: actions.DISCONNECTED, error: err })];
3539
}
3640
}
3741

@@ -344,7 +348,7 @@ abstract class CometTransport extends Transport {
344348
try {
345349
const items = this.decodeResponse(responseData);
346350
if (items && items.length)
347-
for (let i = 0; i < items.length; i++) this.onProtocolMessage(ProtocolMessage.fromDeserialized(items[i]));
351+
for (let i = 0; i < items.length; i++) this.onProtocolMessage(protocolMessageFromDeserialized(items[i]));
348352
} catch (e) {
349353
Logger.logAction(
350354
Logger.LOG_ERROR,

src/common/lib/transport/connectionmanager.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import ProtocolMessage from 'common/lib/types/protocolmessage';
1+
import ProtocolMessage, {
2+
actions,
3+
stringify as stringifyProtocolMessage,
4+
fromValues as protocolMessageFromValues,
5+
} from 'common/lib/types/protocolmessage';
26
import * as Utils from 'common/lib/util/utils';
37
import Protocol, { PendingMessage } from './protocol';
48
import Defaults, { getAgentString } from 'common/lib/util/defaults';
@@ -10,7 +14,7 @@ import ConnectionStateChange from 'common/lib/client/connectionstatechange';
1014
import ConnectionErrors, { isRetriable } from './connectionerrors';
1115
import ErrorInfo, { IPartialErrorInfo, PartialErrorInfo } from 'common/lib/types/errorinfo';
1216
import Auth from 'common/lib/client/auth';
13-
import Message from 'common/lib/types/message';
17+
import Message, { getMessagesSize } from 'common/lib/types/message';
1418
import Multicaster, { MulticasterInstance } from 'common/lib/util/multicaster';
1519
import Transport, { TransportCtor } from './transport';
1620
import * as API from '../../../../ably';
@@ -24,7 +28,6 @@ let globalObject = typeof global !== 'undefined' ? global : typeof window !== 'u
2428

2529
const haveWebStorage = () => typeof Platform.WebStorage !== 'undefined' && Platform.WebStorage?.localSupported;
2630
const haveSessionStorage = () => typeof Platform.WebStorage !== 'undefined' && Platform.WebStorage?.sessionSupported;
27-
const actions = ProtocolMessage.Action;
2831
const noop = function () {};
2932
const transportPreferenceName = 'ably-transport-preference';
3033

@@ -62,7 +65,7 @@ function bundleWith(dest: ProtocolMessage, src: ProtocolMessage, maxSize: number
6265
}
6366
const kind = action === actions.PRESENCE ? 'presence' : 'messages',
6467
proposed = (dest as Record<string, any>)[kind].concat((src as Record<string, any>)[kind]),
65-
size = Message.getMessagesSize(proposed);
68+
size = getMessagesSize(proposed);
6669
if (size > maxSize) {
6770
/* RTL6d1 */
6871
return false;
@@ -732,7 +735,7 @@ class ConnectionManager extends EventEmitter {
732735
// Send ACTIVATE to tell the server to make this transport the
733736
// active transport, which suspends channels until we re-attach.
734737
transport.send(
735-
ProtocolMessage.fromValues({
738+
protocolMessageFromValues({
736739
action: actions.ACTIVATE,
737740
})
738741
);
@@ -1777,7 +1780,7 @@ class ConnectionManager extends EventEmitter {
17771780
activeTransport.onAuthUpdated(tokenDetails);
17781781
}
17791782

1780-
const authMsg = ProtocolMessage.fromValues({
1783+
const authMsg = protocolMessageFromValues({
17811784
action: actions.AUTH,
17821785
auth: {
17831786
accessToken: tokenDetails.token,
@@ -1911,7 +1914,7 @@ class ConnectionManager extends EventEmitter {
19111914
return;
19121915
}
19131916
if (Logger.shouldLog(Logger.LOG_MICRO)) {
1914-
Logger.logAction(Logger.LOG_MICRO, 'ConnectionManager.send()', 'queueing msg; ' + ProtocolMessage.stringify(msg));
1917+
Logger.logAction(Logger.LOG_MICRO, 'ConnectionManager.send()', 'queueing msg; ' + stringifyProtocolMessage(msg));
19151918
}
19161919
this.queue(msg, callback);
19171920
}

0 commit comments

Comments
 (0)