-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathbaserealtime.ts
More file actions
238 lines (214 loc) · 8.63 KB
/
baserealtime.ts
File metadata and controls
238 lines (214 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import * as Utils from '../util/utils';
import BaseClient from './baseclient';
import EventEmitter from '../util/eventemitter';
import Logger from '../util/logger';
import Connection from './connection';
import RealtimeChannel from './realtimechannel';
import ErrorInfo from '../types/errorinfo';
import ProtocolMessage from '../types/protocolmessage';
import { ChannelOptions } from '../../types/channel';
import ClientOptions from '../../types/ClientOptions';
import * as API from '../../../../ably';
import { ModularPlugins, RealtimePresencePlugin } from './modularplugins';
import { TransportNames } from 'common/constants/TransportName';
import { TransportImplementations } from 'common/platform';
import Defaults from '../util/defaults';
/**
`BaseRealtime` is an export of the tree-shakable version of the SDK, and acts as the base class for the `DefaultRealtime` class exported by the non tree-shakable version.
*/
class BaseRealtime extends BaseClient {
readonly _RealtimePresence: RealtimePresencePlugin | null;
// Extra transport implementations available to this client, in addition to those in Platform.Transports.bundledImplementations
readonly _additionalTransportImplementations: TransportImplementations;
_channels: any;
connection: Connection;
// internal API to make EventEmitter usable in other SDKs
static readonly EventEmitter = EventEmitter;
/*
* The public typings declare that this only accepts an object, but since we want to emit a good error message in the case where a non-TypeScript user does one of these things:
*
* 1. passes a string (which is quite likely if they’re e.g. migrating from the default variant to the modular variant)
* 2. passes no argument at all
*
* tell the compiler that these cases are possible so that it forces us to handle them.
*/
constructor(options?: ClientOptions | string) {
super(Defaults.objectifyOptions(options, false, 'BaseRealtime', Logger.defaultLogger));
Logger.logAction(this.logger, Logger.LOG_MINOR, 'Realtime()', '');
// currently we cannot support using Ably.Realtime instances in Vercel Edge runtime.
// this error can be removed after fixing https://github.com/ably/ably-js/issues/1731,
// and https://github.com/ably/ably-js/issues/1732
// @ts-ignore
if (typeof EdgeRuntime === 'string') {
throw new ErrorInfo(
`Ably.Realtime instance cannot be used in Vercel Edge runtime.` +
` If you are running Vercel Edge functions, please replace your` +
` "new Ably.Realtime()" with "new Ably.Rest()" and use Ably Rest API` +
` instead of the Realtime API. If you are server-rendering your application` +
` in the Vercel Edge runtime, please use the condition "if (typeof EdgeRuntime === 'string')"` +
` to prevent instantiating Ably.Realtime instance during SSR in the Vercel Edge runtime.`,
40000,
400,
);
}
this._additionalTransportImplementations = BaseRealtime.transportImplementationsFromPlugins(this.options.plugins);
this._RealtimePresence = this.options.plugins?.RealtimePresence ?? null;
this.connection = new Connection(this, this.options);
this._channels = new Channels(this);
if (this.options.autoConnect !== false) this.connect();
}
private static transportImplementationsFromPlugins(plugins?: ModularPlugins) {
const transports: TransportImplementations = {};
if (plugins?.WebSocketTransport) {
transports[TransportNames.WebSocket] = plugins.WebSocketTransport;
}
if (plugins?.XHRPolling) {
transports[TransportNames.XhrPolling] = plugins.XHRPolling;
}
return transports;
}
get channels() {
return this._channels;
}
get clientId() {
//RTC17
return this.auth.clientId;
}
connect(): void {
Logger.logAction(this.logger, Logger.LOG_MINOR, 'Realtime.connect()', '');
this.connection.connect();
}
close(): void {
Logger.logAction(this.logger, Logger.LOG_MINOR, 'Realtime.close()', '');
this.connection.close();
}
}
class Channels extends EventEmitter {
realtime: BaseRealtime;
// RSN2
all: Record<string, RealtimeChannel>;
constructor(realtime: BaseRealtime) {
super(realtime.logger);
this.realtime = realtime;
this.all = Object.create(null);
realtime.connection.connectionManager.on('transport.active', () => {
this.onTransportActive();
});
}
channelSerials(): { [name: string]: string } {
let serials: { [name: string]: string } = {};
for (const name of Utils.keysArray(this.all, true)) {
const channel = this.all[name];
if (channel.properties.channelSerial) {
serials[name] = channel.properties.channelSerial;
}
}
return serials;
}
// recoverChannels gets the given channels and sets their channel serials.
recoverChannels(channelSerials: { [name: string]: string }) {
for (const name of Utils.keysArray(channelSerials, true)) {
const channel = this.get(name);
channel.properties.channelSerial = channelSerials[name];
}
}
// Access to this method is synchronised by ConnectionManager#processChannelMessage.
async processChannelMessage(msg: ProtocolMessage) {
const channelName = msg.channel;
if (channelName === undefined) {
Logger.logAction(
this.logger,
Logger.LOG_ERROR,
'Channels.processChannelMessage()',
'received event unspecified channel, action = ' + msg.action,
);
return;
}
const channel = this.all[channelName];
if (!channel) {
Logger.logAction(
this.logger,
Logger.LOG_ERROR,
'Channels.processChannelMessage()',
'received event for non-existent channel: ' + channelName,
);
return;
}
await channel.processMessage(msg);
}
/* called when a transport becomes connected; reattempt attach/detach
* for channels that are attaching or detaching. */
onTransportActive() {
for (const channelName in this.all) {
const channel = this.all[channelName];
if (channel.state === 'attaching' || channel.state === 'detaching') {
channel.checkPendingState();
} else if (channel.state === 'suspended') {
channel._attach(false, null);
} else if (channel.state === 'attached') {
// Note explicity request the state, channel.attach() would do nothing
// as its already attached.
channel.requestState('attaching');
}
}
}
/* Connection interruptions (ie when the connection will no longer queue
* events) imply connection state changes for any channel which is either
* attached, pending, or will attempt to become attached in the future */
propogateConnectionInterruption(connectionState: string, reason: ErrorInfo) {
const connectionStateToChannelState: Record<string, API.ChannelState> = {
closing: 'detached',
closed: 'detached',
failed: 'failed',
suspended: 'suspended',
};
const fromChannelStates = ['attaching', 'attached', 'detaching', 'suspended'];
const toChannelState = connectionStateToChannelState[connectionState];
for (const channelId in this.all) {
const channel = this.all[channelId];
if (fromChannelStates.includes(channel.state)) {
channel.notifyState(toChannelState, reason);
}
}
}
get(name: string, channelOptions?: ChannelOptions) {
name = String(name);
let channel = this.all[name];
if (!channel) {
channel = this.all[name] = new RealtimeChannel(this.realtime, name, channelOptions);
} else if (channelOptions) {
if (channel._shouldReattachToSetOptions(channelOptions, channel.channelOptions)) {
throw new ErrorInfo(
'Channels.get() cannot be used to set channel options that would cause the channel to reattach. Please, use RealtimeChannel.setOptions() instead.',
40000,
400,
);
}
channel.setOptions(channelOptions);
}
return channel;
}
getDerived(name: string, deriveOptions: API.DeriveOptions, channelOptions?: ChannelOptions) {
if (deriveOptions.filter) {
const filter = Utils.toBase64(deriveOptions.filter);
const match = Utils.matchDerivedChannel(name);
name = `[filter=${filter}${match.qualifierParam}]${match.channelName}`;
}
return this.get(name, channelOptions);
}
/* Included to support certain niche use-cases; most users should ignore this.
* Please do not use this unless you know what you're doing */
release(name: string) {
name = String(name);
const channel = this.all[name];
if (!channel) {
return;
}
const releaseErr = channel.getReleaseErr();
if (releaseErr) {
throw releaseErr;
}
delete this.all[name];
}
}
export default BaseRealtime;