@supabase/phoenix 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +22 -0
- package/README.md +122 -0
- package/assets/js/phoenix/ajax.js +116 -0
- package/assets/js/phoenix/channel.js +331 -0
- package/assets/js/phoenix/constants.js +35 -0
- package/assets/js/phoenix/index.js +212 -0
- package/assets/js/phoenix/longpoll.js +192 -0
- package/assets/js/phoenix/presence.js +208 -0
- package/assets/js/phoenix/push.js +134 -0
- package/assets/js/phoenix/serializer.js +133 -0
- package/assets/js/phoenix/socket.js +747 -0
- package/assets/js/phoenix/timer.js +48 -0
- package/assets/js/phoenix/types.js +184 -0
- package/assets/js/phoenix/utils.js +16 -0
- package/package.json +58 -0
- package/priv/static/favicon.ico +0 -0
- package/priv/static/phoenix-orange.png +0 -0
- package/priv/static/phoenix.cjs.js +1812 -0
- package/priv/static/phoenix.cjs.js.map +7 -0
- package/priv/static/phoenix.js +1834 -0
- package/priv/static/phoenix.min.js +2 -0
- package/priv/static/phoenix.mjs +1789 -0
- package/priv/static/phoenix.mjs.map +7 -0
- package/priv/static/phoenix.png +0 -0
- package/priv/static/types/ajax.d.ts +10 -0
- package/priv/static/types/ajax.d.ts.map +1 -0
- package/priv/static/types/channel.d.ts +167 -0
- package/priv/static/types/channel.d.ts.map +1 -0
- package/priv/static/types/constants.d.ts +36 -0
- package/priv/static/types/constants.d.ts.map +1 -0
- package/priv/static/types/index.d.ts +10 -0
- package/priv/static/types/index.d.ts.map +1 -0
- package/priv/static/types/longpoll.d.ts +29 -0
- package/priv/static/types/longpoll.d.ts.map +1 -0
- package/priv/static/types/presence.d.ts +107 -0
- package/priv/static/types/presence.d.ts.map +1 -0
- package/priv/static/types/push.d.ts +70 -0
- package/priv/static/types/push.d.ts.map +1 -0
- package/priv/static/types/serializer.d.ts +74 -0
- package/priv/static/types/serializer.d.ts.map +1 -0
- package/priv/static/types/socket.d.ts +284 -0
- package/priv/static/types/socket.d.ts.map +1 -0
- package/priv/static/types/timer.d.ts +36 -0
- package/priv/static/types/timer.d.ts.map +1 -0
- package/priv/static/types/types.d.ts +280 -0
- package/priv/static/types/types.d.ts.map +1 -0
- package/priv/static/types/utils.d.ts +2 -0
- package/priv/static/types/utils.d.ts.map +1 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,1812 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// js/phoenix/index.js
|
|
21
|
+
var phoenix_exports = {};
|
|
22
|
+
__export(phoenix_exports, {
|
|
23
|
+
Channel: () => Channel,
|
|
24
|
+
LongPoll: () => LongPoll,
|
|
25
|
+
Presence: () => Presence,
|
|
26
|
+
Push: () => Push,
|
|
27
|
+
Serializer: () => serializer_default,
|
|
28
|
+
Socket: () => Socket,
|
|
29
|
+
Timer: () => Timer
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(phoenix_exports);
|
|
32
|
+
|
|
33
|
+
// js/phoenix/utils.js
|
|
34
|
+
var closure = (value) => {
|
|
35
|
+
if (typeof value === "function") {
|
|
36
|
+
return (
|
|
37
|
+
/** @type {() => T} */
|
|
38
|
+
value
|
|
39
|
+
);
|
|
40
|
+
} else {
|
|
41
|
+
let closure2 = function() {
|
|
42
|
+
return value;
|
|
43
|
+
};
|
|
44
|
+
return closure2;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// js/phoenix/constants.js
|
|
49
|
+
var globalSelf = typeof self !== "undefined" ? self : null;
|
|
50
|
+
var phxWindow = typeof window !== "undefined" ? window : null;
|
|
51
|
+
var global = globalSelf || phxWindow || globalThis;
|
|
52
|
+
var DEFAULT_VSN = "2.0.0";
|
|
53
|
+
var DEFAULT_TIMEOUT = 1e4;
|
|
54
|
+
var WS_CLOSE_NORMAL = 1e3;
|
|
55
|
+
var SOCKET_STATES = (
|
|
56
|
+
/** @type {const} */
|
|
57
|
+
{ connecting: 0, open: 1, closing: 2, closed: 3 }
|
|
58
|
+
);
|
|
59
|
+
var CHANNEL_STATES = (
|
|
60
|
+
/** @type {const} */
|
|
61
|
+
{
|
|
62
|
+
closed: "closed",
|
|
63
|
+
errored: "errored",
|
|
64
|
+
joined: "joined",
|
|
65
|
+
joining: "joining",
|
|
66
|
+
leaving: "leaving"
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
var CHANNEL_EVENTS = (
|
|
70
|
+
/** @type {const} */
|
|
71
|
+
{
|
|
72
|
+
close: "phx_close",
|
|
73
|
+
error: "phx_error",
|
|
74
|
+
join: "phx_join",
|
|
75
|
+
reply: "phx_reply",
|
|
76
|
+
leave: "phx_leave"
|
|
77
|
+
}
|
|
78
|
+
);
|
|
79
|
+
var TRANSPORTS = (
|
|
80
|
+
/** @type {const} */
|
|
81
|
+
{
|
|
82
|
+
longpoll: "longpoll",
|
|
83
|
+
websocket: "websocket"
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
var XHR_STATES = (
|
|
87
|
+
/** @type {const} */
|
|
88
|
+
{
|
|
89
|
+
complete: 4
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
var AUTH_TOKEN_PREFIX = "base64url.bearer.phx.";
|
|
93
|
+
|
|
94
|
+
// js/phoenix/push.js
|
|
95
|
+
var Push = class {
|
|
96
|
+
/**
|
|
97
|
+
* Initializes the Push
|
|
98
|
+
* @param {Channel} channel - The Channel
|
|
99
|
+
* @param {ChannelEvent} event - The event, for example `"phx_join"`
|
|
100
|
+
* @param {() => Record<string, unknown>} payload - The payload, for example `{user_id: 123}`
|
|
101
|
+
* @param {number} timeout - The push timeout in milliseconds
|
|
102
|
+
*/
|
|
103
|
+
constructor(channel, event, payload, timeout) {
|
|
104
|
+
this.channel = channel;
|
|
105
|
+
this.event = event;
|
|
106
|
+
this.payload = payload || function() {
|
|
107
|
+
return {};
|
|
108
|
+
};
|
|
109
|
+
this.receivedResp = null;
|
|
110
|
+
this.timeout = timeout;
|
|
111
|
+
this.timeoutTimer = null;
|
|
112
|
+
this.recHooks = [];
|
|
113
|
+
this.sent = false;
|
|
114
|
+
this.ref = void 0;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
*
|
|
118
|
+
* @param {number} timeout
|
|
119
|
+
*/
|
|
120
|
+
resend(timeout) {
|
|
121
|
+
this.timeout = timeout;
|
|
122
|
+
this.reset();
|
|
123
|
+
this.send();
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
*
|
|
127
|
+
*/
|
|
128
|
+
send() {
|
|
129
|
+
if (this.hasReceived("timeout")) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.startTimeout();
|
|
133
|
+
this.sent = true;
|
|
134
|
+
this.channel.socket.push({
|
|
135
|
+
topic: this.channel.topic,
|
|
136
|
+
event: this.event,
|
|
137
|
+
payload: this.payload(),
|
|
138
|
+
ref: this.ref,
|
|
139
|
+
join_ref: this.channel.joinRef()
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
*
|
|
144
|
+
* @param {string} status
|
|
145
|
+
* @param {(response: any) => void} callback
|
|
146
|
+
*/
|
|
147
|
+
receive(status, callback) {
|
|
148
|
+
if (this.hasReceived(status)) {
|
|
149
|
+
callback(this.receivedResp.response);
|
|
150
|
+
}
|
|
151
|
+
this.recHooks.push({ status, callback });
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
reset() {
|
|
155
|
+
this.cancelRefEvent();
|
|
156
|
+
this.ref = null;
|
|
157
|
+
this.refEvent = null;
|
|
158
|
+
this.receivedResp = null;
|
|
159
|
+
this.sent = false;
|
|
160
|
+
}
|
|
161
|
+
destroy() {
|
|
162
|
+
this.cancelRefEvent();
|
|
163
|
+
this.cancelTimeout();
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* @private
|
|
167
|
+
*/
|
|
168
|
+
matchReceive({ status, response, _ref }) {
|
|
169
|
+
this.recHooks.filter((h) => h.status === status).forEach((h) => h.callback(response));
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* @private
|
|
173
|
+
*/
|
|
174
|
+
cancelRefEvent() {
|
|
175
|
+
if (!this.refEvent) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
this.channel.off(this.refEvent);
|
|
179
|
+
}
|
|
180
|
+
cancelTimeout() {
|
|
181
|
+
clearTimeout(this.timeoutTimer);
|
|
182
|
+
this.timeoutTimer = null;
|
|
183
|
+
}
|
|
184
|
+
startTimeout() {
|
|
185
|
+
if (this.timeoutTimer) {
|
|
186
|
+
this.cancelTimeout();
|
|
187
|
+
}
|
|
188
|
+
this.ref = this.channel.socket.makeRef();
|
|
189
|
+
this.refEvent = this.channel.replyEventName(this.ref);
|
|
190
|
+
this.channel.on(this.refEvent, (payload) => {
|
|
191
|
+
this.cancelRefEvent();
|
|
192
|
+
this.cancelTimeout();
|
|
193
|
+
this.receivedResp = payload;
|
|
194
|
+
this.matchReceive(payload);
|
|
195
|
+
});
|
|
196
|
+
this.timeoutTimer = setTimeout(() => {
|
|
197
|
+
this.trigger("timeout", {});
|
|
198
|
+
}, this.timeout);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* @private
|
|
202
|
+
*/
|
|
203
|
+
hasReceived(status) {
|
|
204
|
+
return this.receivedResp && this.receivedResp.status === status;
|
|
205
|
+
}
|
|
206
|
+
trigger(status, response) {
|
|
207
|
+
this.channel.trigger(this.refEvent, { status, response });
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// js/phoenix/timer.js
|
|
212
|
+
var Timer = class {
|
|
213
|
+
/**
|
|
214
|
+
* @param {() => void} callback
|
|
215
|
+
* @param {(tries: number) => number} timerCalc
|
|
216
|
+
*/
|
|
217
|
+
constructor(callback, timerCalc) {
|
|
218
|
+
this.callback = callback;
|
|
219
|
+
this.timerCalc = timerCalc;
|
|
220
|
+
this.timer = void 0;
|
|
221
|
+
this.tries = 0;
|
|
222
|
+
}
|
|
223
|
+
reset() {
|
|
224
|
+
this.tries = 0;
|
|
225
|
+
clearTimeout(this.timer);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Cancels any previous scheduleTimeout and schedules callback
|
|
229
|
+
*/
|
|
230
|
+
scheduleTimeout() {
|
|
231
|
+
clearTimeout(this.timer);
|
|
232
|
+
this.timer = setTimeout(() => {
|
|
233
|
+
this.tries = this.tries + 1;
|
|
234
|
+
this.callback();
|
|
235
|
+
}, this.timerCalc(this.tries + 1));
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
// js/phoenix/channel.js
|
|
240
|
+
var Channel = class {
|
|
241
|
+
/**
|
|
242
|
+
* @param {string} topic
|
|
243
|
+
* @param {Params | (() => Params)} params
|
|
244
|
+
* @param {Socket} socket
|
|
245
|
+
*/
|
|
246
|
+
constructor(topic, params, socket) {
|
|
247
|
+
this.state = CHANNEL_STATES.closed;
|
|
248
|
+
this.topic = topic;
|
|
249
|
+
this.params = closure(params || {});
|
|
250
|
+
this.socket = socket;
|
|
251
|
+
this.bindings = [];
|
|
252
|
+
this.bindingRef = 0;
|
|
253
|
+
this.timeout = this.socket.timeout;
|
|
254
|
+
this.joinedOnce = false;
|
|
255
|
+
this.joinPush = new Push(this, CHANNEL_EVENTS.join, this.params, this.timeout);
|
|
256
|
+
this.pushBuffer = [];
|
|
257
|
+
this.stateChangeRefs = [];
|
|
258
|
+
this.rejoinTimer = new Timer(() => {
|
|
259
|
+
if (this.socket.isConnected()) {
|
|
260
|
+
this.rejoin();
|
|
261
|
+
}
|
|
262
|
+
}, this.socket.rejoinAfterMs);
|
|
263
|
+
this.stateChangeRefs.push(this.socket.onError(() => this.rejoinTimer.reset()));
|
|
264
|
+
this.stateChangeRefs.push(
|
|
265
|
+
this.socket.onOpen(() => {
|
|
266
|
+
this.rejoinTimer.reset();
|
|
267
|
+
if (this.isErrored()) {
|
|
268
|
+
this.rejoin();
|
|
269
|
+
}
|
|
270
|
+
})
|
|
271
|
+
);
|
|
272
|
+
this.joinPush.receive("ok", () => {
|
|
273
|
+
this.state = CHANNEL_STATES.joined;
|
|
274
|
+
this.rejoinTimer.reset();
|
|
275
|
+
this.pushBuffer.forEach((pushEvent) => pushEvent.send());
|
|
276
|
+
this.pushBuffer = [];
|
|
277
|
+
});
|
|
278
|
+
this.joinPush.receive("error", (reason) => {
|
|
279
|
+
this.state = CHANNEL_STATES.errored;
|
|
280
|
+
if (this.socket.hasLogger()) this.socket.log("channel", `error ${this.topic}`, reason);
|
|
281
|
+
if (this.socket.isConnected()) {
|
|
282
|
+
this.rejoinTimer.scheduleTimeout();
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
this.onClose(() => {
|
|
286
|
+
this.rejoinTimer.reset();
|
|
287
|
+
if (this.socket.hasLogger()) this.socket.log("channel", `close ${this.topic}`);
|
|
288
|
+
this.state = CHANNEL_STATES.closed;
|
|
289
|
+
this.socket.remove(this);
|
|
290
|
+
});
|
|
291
|
+
this.onError((reason) => {
|
|
292
|
+
if (this.socket.hasLogger()) this.socket.log("channel", `error ${this.topic}`, reason);
|
|
293
|
+
if (this.isJoining()) {
|
|
294
|
+
this.joinPush.reset();
|
|
295
|
+
}
|
|
296
|
+
this.state = CHANNEL_STATES.errored;
|
|
297
|
+
if (this.socket.isConnected()) {
|
|
298
|
+
this.rejoinTimer.scheduleTimeout();
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
this.joinPush.receive("timeout", () => {
|
|
302
|
+
if (this.socket.hasLogger()) this.socket.log("channel", `timeout ${this.topic}`, this.joinPush.timeout);
|
|
303
|
+
let leavePush = new Push(this, CHANNEL_EVENTS.leave, closure({}), this.timeout);
|
|
304
|
+
leavePush.send();
|
|
305
|
+
this.state = CHANNEL_STATES.errored;
|
|
306
|
+
this.joinPush.reset();
|
|
307
|
+
if (this.socket.isConnected()) {
|
|
308
|
+
this.rejoinTimer.scheduleTimeout();
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
this.on(CHANNEL_EVENTS.reply, (payload, ref) => {
|
|
312
|
+
this.trigger(this.replyEventName(ref), payload);
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Join the channel
|
|
317
|
+
* @param {number} timeout
|
|
318
|
+
* @returns {Push}
|
|
319
|
+
*/
|
|
320
|
+
join(timeout = this.timeout) {
|
|
321
|
+
if (this.joinedOnce) {
|
|
322
|
+
throw new Error("tried to join multiple times. 'join' can only be called a single time per channel instance");
|
|
323
|
+
} else {
|
|
324
|
+
this.timeout = timeout;
|
|
325
|
+
this.joinedOnce = true;
|
|
326
|
+
this.rejoin();
|
|
327
|
+
return this.joinPush;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Teardown the channel.
|
|
332
|
+
*
|
|
333
|
+
* Destroys and stops related timers.
|
|
334
|
+
*/
|
|
335
|
+
teardown() {
|
|
336
|
+
this.pushBuffer.forEach((push) => push.destroy());
|
|
337
|
+
this.pushBuffer = [];
|
|
338
|
+
this.rejoinTimer.reset();
|
|
339
|
+
this.joinPush.destroy();
|
|
340
|
+
this.state = CHANNEL_STATES.closed;
|
|
341
|
+
this.bindings = {};
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Hook into channel close
|
|
345
|
+
* @param {ChannelBindingCallback} callback
|
|
346
|
+
*/
|
|
347
|
+
onClose(callback) {
|
|
348
|
+
this.on(CHANNEL_EVENTS.close, callback);
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Hook into channel errors
|
|
352
|
+
* @param {ChannelOnErrorCallback} callback
|
|
353
|
+
* @return {number}
|
|
354
|
+
*/
|
|
355
|
+
onError(callback) {
|
|
356
|
+
return this.on(CHANNEL_EVENTS.error, (reason) => callback(reason));
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Subscribes on channel events
|
|
360
|
+
*
|
|
361
|
+
* Subscription returns a ref counter, which can be used later to
|
|
362
|
+
* unsubscribe the exact event listener
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* const ref1 = channel.on("event", do_stuff)
|
|
366
|
+
* const ref2 = channel.on("event", do_other_stuff)
|
|
367
|
+
* channel.off("event", ref1)
|
|
368
|
+
* // Since unsubscription, do_stuff won't fire,
|
|
369
|
+
* // while do_other_stuff will keep firing on the "event"
|
|
370
|
+
*
|
|
371
|
+
* @param {string} event
|
|
372
|
+
* @param {ChannelBindingCallback} callback
|
|
373
|
+
* @returns {number} ref
|
|
374
|
+
*/
|
|
375
|
+
on(event, callback) {
|
|
376
|
+
let ref = this.bindingRef++;
|
|
377
|
+
this.bindings.push({ event, ref, callback });
|
|
378
|
+
return ref;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Unsubscribes off of channel events
|
|
382
|
+
*
|
|
383
|
+
* Use the ref returned from a channel.on() to unsubscribe one
|
|
384
|
+
* handler, or pass nothing for the ref to unsubscribe all
|
|
385
|
+
* handlers for the given event.
|
|
386
|
+
*
|
|
387
|
+
* @example
|
|
388
|
+
* // Unsubscribe the do_stuff handler
|
|
389
|
+
* const ref1 = channel.on("event", do_stuff)
|
|
390
|
+
* channel.off("event", ref1)
|
|
391
|
+
*
|
|
392
|
+
* // Unsubscribe all handlers from event
|
|
393
|
+
* channel.off("event")
|
|
394
|
+
*
|
|
395
|
+
* @param {string} event
|
|
396
|
+
* @param {number} [ref]
|
|
397
|
+
*/
|
|
398
|
+
off(event, ref) {
|
|
399
|
+
this.bindings = this.bindings.filter((bind) => {
|
|
400
|
+
return !(bind.event === event && (typeof ref === "undefined" || ref === bind.ref));
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* @private
|
|
405
|
+
*/
|
|
406
|
+
canPush() {
|
|
407
|
+
return this.socket.isConnected() && this.isJoined();
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Sends a message `event` to phoenix with the payload `payload`.
|
|
411
|
+
* Phoenix receives this in the `handle_in(event, payload, socket)`
|
|
412
|
+
* function. if phoenix replies or it times out (default 10000ms),
|
|
413
|
+
* then optionally the reply can be received.
|
|
414
|
+
*
|
|
415
|
+
* @example
|
|
416
|
+
* channel.push("event")
|
|
417
|
+
* .receive("ok", payload => console.log("phoenix replied:", payload))
|
|
418
|
+
* .receive("error", err => console.log("phoenix errored", err))
|
|
419
|
+
* .receive("timeout", () => console.log("timed out pushing"))
|
|
420
|
+
* @param {string} event
|
|
421
|
+
* @param {Object} payload
|
|
422
|
+
* @param {number} [timeout]
|
|
423
|
+
* @returns {Push}
|
|
424
|
+
*/
|
|
425
|
+
push(event, payload, timeout = this.timeout) {
|
|
426
|
+
payload = payload || {};
|
|
427
|
+
if (!this.joinedOnce) {
|
|
428
|
+
throw new Error(`tried to push '${event}' to '${this.topic}' before joining. Use channel.join() before pushing events`);
|
|
429
|
+
}
|
|
430
|
+
let pushEvent = new Push(this, event, function() {
|
|
431
|
+
return payload;
|
|
432
|
+
}, timeout);
|
|
433
|
+
if (this.canPush()) {
|
|
434
|
+
pushEvent.send();
|
|
435
|
+
} else {
|
|
436
|
+
pushEvent.startTimeout();
|
|
437
|
+
this.pushBuffer.push(pushEvent);
|
|
438
|
+
}
|
|
439
|
+
return pushEvent;
|
|
440
|
+
}
|
|
441
|
+
/** Leaves the channel
|
|
442
|
+
*
|
|
443
|
+
* Unsubscribes from server events, and
|
|
444
|
+
* instructs channel to terminate on server
|
|
445
|
+
*
|
|
446
|
+
* Triggers onClose() hooks
|
|
447
|
+
*
|
|
448
|
+
* To receive leave acknowledgements, use the `receive`
|
|
449
|
+
* hook to bind to the server ack, ie:
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* channel.leave().receive("ok", () => alert("left!") )
|
|
453
|
+
*
|
|
454
|
+
* @param {number} timeout
|
|
455
|
+
* @returns {Push}
|
|
456
|
+
*/
|
|
457
|
+
leave(timeout = this.timeout) {
|
|
458
|
+
this.rejoinTimer.reset();
|
|
459
|
+
this.joinPush.cancelTimeout();
|
|
460
|
+
this.state = CHANNEL_STATES.leaving;
|
|
461
|
+
let onClose = () => {
|
|
462
|
+
if (this.socket.hasLogger()) this.socket.log("channel", `leave ${this.topic}`);
|
|
463
|
+
this.trigger(CHANNEL_EVENTS.close, "leave");
|
|
464
|
+
};
|
|
465
|
+
let leavePush = new Push(this, CHANNEL_EVENTS.leave, closure({}), timeout);
|
|
466
|
+
leavePush.receive("ok", () => onClose()).receive("timeout", () => onClose());
|
|
467
|
+
leavePush.send();
|
|
468
|
+
if (!this.canPush()) {
|
|
469
|
+
leavePush.trigger("ok", {});
|
|
470
|
+
}
|
|
471
|
+
return leavePush;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Overridable message hook
|
|
475
|
+
*
|
|
476
|
+
* Receives all events for specialized message handling
|
|
477
|
+
* before dispatching to the channel callbacks.
|
|
478
|
+
*
|
|
479
|
+
* Must return the payload, modified or unmodified
|
|
480
|
+
* @type{ChannelOnMessage}
|
|
481
|
+
*/
|
|
482
|
+
onMessage(_event, payload, _ref) {
|
|
483
|
+
return payload;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Overridable filter hook
|
|
487
|
+
*
|
|
488
|
+
* If this function returns `true`, `binding`'s callback will be called.
|
|
489
|
+
*
|
|
490
|
+
* @type{ChannelFilterBindings}
|
|
491
|
+
*/
|
|
492
|
+
filterBindings(_binding, _payload, _ref) {
|
|
493
|
+
return true;
|
|
494
|
+
}
|
|
495
|
+
isMember(topic, event, payload, joinRef) {
|
|
496
|
+
if (this.topic !== topic) {
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
if (joinRef && joinRef !== this.joinRef()) {
|
|
500
|
+
if (this.socket.hasLogger()) this.socket.log("channel", "dropping outdated message", { topic, event, payload, joinRef });
|
|
501
|
+
return false;
|
|
502
|
+
} else {
|
|
503
|
+
return true;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
joinRef() {
|
|
507
|
+
return this.joinPush.ref;
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* @private
|
|
511
|
+
*/
|
|
512
|
+
rejoin(timeout = this.timeout) {
|
|
513
|
+
if (this.isLeaving()) {
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
this.socket.leaveOpenTopic(this.topic);
|
|
517
|
+
this.state = CHANNEL_STATES.joining;
|
|
518
|
+
this.joinPush.resend(timeout);
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* @param {string} event
|
|
522
|
+
* @param {unknown} [payload]
|
|
523
|
+
* @param {?string} [ref]
|
|
524
|
+
* @param {?string} [joinRef]
|
|
525
|
+
*/
|
|
526
|
+
trigger(event, payload, ref, joinRef) {
|
|
527
|
+
let handledPayload = this.onMessage(event, payload, ref, joinRef);
|
|
528
|
+
if (payload && !handledPayload) {
|
|
529
|
+
throw new Error("channel onMessage callbacks must return the payload, modified or unmodified");
|
|
530
|
+
}
|
|
531
|
+
let eventBindings = this.bindings.filter((bind) => bind.event === event && this.filterBindings(bind, payload, ref));
|
|
532
|
+
for (let i = 0; i < eventBindings.length; i++) {
|
|
533
|
+
let bind = eventBindings[i];
|
|
534
|
+
bind.callback(handledPayload, ref, joinRef || this.joinRef());
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* @param {string} ref
|
|
539
|
+
*/
|
|
540
|
+
replyEventName(ref) {
|
|
541
|
+
return `chan_reply_${ref}`;
|
|
542
|
+
}
|
|
543
|
+
isClosed() {
|
|
544
|
+
return this.state === CHANNEL_STATES.closed;
|
|
545
|
+
}
|
|
546
|
+
isErrored() {
|
|
547
|
+
return this.state === CHANNEL_STATES.errored;
|
|
548
|
+
}
|
|
549
|
+
isJoined() {
|
|
550
|
+
return this.state === CHANNEL_STATES.joined;
|
|
551
|
+
}
|
|
552
|
+
isJoining() {
|
|
553
|
+
return this.state === CHANNEL_STATES.joining;
|
|
554
|
+
}
|
|
555
|
+
isLeaving() {
|
|
556
|
+
return this.state === CHANNEL_STATES.leaving;
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
// js/phoenix/ajax.js
|
|
561
|
+
var Ajax = class {
|
|
562
|
+
static request(method, endPoint, headers, body, timeout, ontimeout, callback) {
|
|
563
|
+
if (global.XDomainRequest) {
|
|
564
|
+
let req = new global.XDomainRequest();
|
|
565
|
+
return this.xdomainRequest(req, method, endPoint, body, timeout, ontimeout, callback);
|
|
566
|
+
} else if (global.XMLHttpRequest) {
|
|
567
|
+
let req = new global.XMLHttpRequest();
|
|
568
|
+
return this.xhrRequest(req, method, endPoint, headers, body, timeout, ontimeout, callback);
|
|
569
|
+
} else if (global.fetch && global.AbortController) {
|
|
570
|
+
return this.fetchRequest(method, endPoint, headers, body, timeout, ontimeout, callback);
|
|
571
|
+
} else {
|
|
572
|
+
throw new Error("No suitable XMLHttpRequest implementation found");
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
static fetchRequest(method, endPoint, headers, body, timeout, ontimeout, callback) {
|
|
576
|
+
let options = {
|
|
577
|
+
method,
|
|
578
|
+
headers,
|
|
579
|
+
body
|
|
580
|
+
};
|
|
581
|
+
let controller = null;
|
|
582
|
+
if (timeout) {
|
|
583
|
+
controller = new AbortController();
|
|
584
|
+
const _timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
585
|
+
options.signal = controller.signal;
|
|
586
|
+
}
|
|
587
|
+
global.fetch(endPoint, options).then((response) => response.text()).then((data) => this.parseJSON(data)).then((data) => callback && callback(data)).catch((err) => {
|
|
588
|
+
if (err.name === "AbortError" && ontimeout) {
|
|
589
|
+
ontimeout();
|
|
590
|
+
} else {
|
|
591
|
+
callback && callback(null);
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
return controller;
|
|
595
|
+
}
|
|
596
|
+
static xdomainRequest(req, method, endPoint, body, timeout, ontimeout, callback) {
|
|
597
|
+
req.timeout = timeout;
|
|
598
|
+
req.open(method, endPoint);
|
|
599
|
+
req.onload = () => {
|
|
600
|
+
let response = this.parseJSON(req.responseText);
|
|
601
|
+
callback && callback(response);
|
|
602
|
+
};
|
|
603
|
+
if (ontimeout) {
|
|
604
|
+
req.ontimeout = ontimeout;
|
|
605
|
+
}
|
|
606
|
+
req.onprogress = () => {
|
|
607
|
+
};
|
|
608
|
+
req.send(body);
|
|
609
|
+
return req;
|
|
610
|
+
}
|
|
611
|
+
static xhrRequest(req, method, endPoint, headers, body, timeout, ontimeout, callback) {
|
|
612
|
+
req.open(method, endPoint, true);
|
|
613
|
+
req.timeout = timeout;
|
|
614
|
+
for (let [key, value] of Object.entries(headers)) {
|
|
615
|
+
req.setRequestHeader(key, value);
|
|
616
|
+
}
|
|
617
|
+
req.onerror = () => callback && callback(null);
|
|
618
|
+
req.onreadystatechange = () => {
|
|
619
|
+
if (req.readyState === XHR_STATES.complete && callback) {
|
|
620
|
+
let response = this.parseJSON(req.responseText);
|
|
621
|
+
callback(response);
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
if (ontimeout) {
|
|
625
|
+
req.ontimeout = ontimeout;
|
|
626
|
+
}
|
|
627
|
+
req.send(body);
|
|
628
|
+
return req;
|
|
629
|
+
}
|
|
630
|
+
static parseJSON(resp) {
|
|
631
|
+
if (!resp || resp === "") {
|
|
632
|
+
return null;
|
|
633
|
+
}
|
|
634
|
+
try {
|
|
635
|
+
return JSON.parse(resp);
|
|
636
|
+
} catch {
|
|
637
|
+
console && console.log("failed to parse JSON response", resp);
|
|
638
|
+
return null;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
static serialize(obj, parentKey) {
|
|
642
|
+
let queryStr = [];
|
|
643
|
+
for (var key in obj) {
|
|
644
|
+
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
let paramKey = parentKey ? `${parentKey}[${key}]` : key;
|
|
648
|
+
let paramVal = obj[key];
|
|
649
|
+
if (typeof paramVal === "object") {
|
|
650
|
+
queryStr.push(this.serialize(paramVal, paramKey));
|
|
651
|
+
} else {
|
|
652
|
+
queryStr.push(encodeURIComponent(paramKey) + "=" + encodeURIComponent(paramVal));
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
return queryStr.join("&");
|
|
656
|
+
}
|
|
657
|
+
static appendParams(url, params) {
|
|
658
|
+
if (Object.keys(params).length === 0) {
|
|
659
|
+
return url;
|
|
660
|
+
}
|
|
661
|
+
let prefix = url.match(/\?/) ? "&" : "?";
|
|
662
|
+
return `${url}${prefix}${this.serialize(params)}`;
|
|
663
|
+
}
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
// js/phoenix/longpoll.js
|
|
667
|
+
var arrayBufferToBase64 = (buffer) => {
|
|
668
|
+
let binary = "";
|
|
669
|
+
let bytes = new Uint8Array(buffer);
|
|
670
|
+
let len = bytes.byteLength;
|
|
671
|
+
for (let i = 0; i < len; i++) {
|
|
672
|
+
binary += String.fromCharCode(bytes[i]);
|
|
673
|
+
}
|
|
674
|
+
return btoa(binary);
|
|
675
|
+
};
|
|
676
|
+
var LongPoll = class {
|
|
677
|
+
constructor(endPoint, protocols) {
|
|
678
|
+
if (protocols && protocols.length === 2 && protocols[1].startsWith(AUTH_TOKEN_PREFIX)) {
|
|
679
|
+
this.authToken = atob(protocols[1].slice(AUTH_TOKEN_PREFIX.length));
|
|
680
|
+
}
|
|
681
|
+
this.endPoint = null;
|
|
682
|
+
this.token = null;
|
|
683
|
+
this.skipHeartbeat = true;
|
|
684
|
+
this.reqs = /* @__PURE__ */ new Set();
|
|
685
|
+
this.awaitingBatchAck = false;
|
|
686
|
+
this.currentBatch = null;
|
|
687
|
+
this.currentBatchTimer = null;
|
|
688
|
+
this.batchBuffer = [];
|
|
689
|
+
this.onopen = function() {
|
|
690
|
+
};
|
|
691
|
+
this.onerror = function() {
|
|
692
|
+
};
|
|
693
|
+
this.onmessage = function() {
|
|
694
|
+
};
|
|
695
|
+
this.onclose = function() {
|
|
696
|
+
};
|
|
697
|
+
this.pollEndpoint = this.normalizeEndpoint(endPoint);
|
|
698
|
+
this.readyState = SOCKET_STATES.connecting;
|
|
699
|
+
setTimeout(() => this.poll(), 0);
|
|
700
|
+
}
|
|
701
|
+
normalizeEndpoint(endPoint) {
|
|
702
|
+
return endPoint.replace("ws://", "http://").replace("wss://", "https://").replace(new RegExp("(.*)/" + TRANSPORTS.websocket), "$1/" + TRANSPORTS.longpoll);
|
|
703
|
+
}
|
|
704
|
+
endpointURL() {
|
|
705
|
+
return Ajax.appendParams(this.pollEndpoint, { token: this.token });
|
|
706
|
+
}
|
|
707
|
+
closeAndRetry(code, reason, wasClean) {
|
|
708
|
+
this.close(code, reason, wasClean);
|
|
709
|
+
this.readyState = SOCKET_STATES.connecting;
|
|
710
|
+
}
|
|
711
|
+
ontimeout() {
|
|
712
|
+
this.onerror("timeout");
|
|
713
|
+
this.closeAndRetry(1005, "timeout", false);
|
|
714
|
+
}
|
|
715
|
+
isActive() {
|
|
716
|
+
return this.readyState === SOCKET_STATES.open || this.readyState === SOCKET_STATES.connecting;
|
|
717
|
+
}
|
|
718
|
+
poll() {
|
|
719
|
+
const headers = { "Accept": "application/json" };
|
|
720
|
+
if (this.authToken) {
|
|
721
|
+
headers["X-Phoenix-AuthToken"] = this.authToken;
|
|
722
|
+
}
|
|
723
|
+
this.ajax("GET", headers, null, () => this.ontimeout(), (resp) => {
|
|
724
|
+
if (resp) {
|
|
725
|
+
var { status, token, messages } = resp;
|
|
726
|
+
if (status === 410 && this.token !== null) {
|
|
727
|
+
this.onerror(410);
|
|
728
|
+
this.closeAndRetry(3410, "session_gone", false);
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
this.token = token;
|
|
732
|
+
} else {
|
|
733
|
+
status = 0;
|
|
734
|
+
}
|
|
735
|
+
switch (status) {
|
|
736
|
+
case 200:
|
|
737
|
+
messages.forEach((msg) => {
|
|
738
|
+
setTimeout(() => this.onmessage({ data: msg }), 0);
|
|
739
|
+
});
|
|
740
|
+
this.poll();
|
|
741
|
+
break;
|
|
742
|
+
case 204:
|
|
743
|
+
this.poll();
|
|
744
|
+
break;
|
|
745
|
+
case 410:
|
|
746
|
+
this.readyState = SOCKET_STATES.open;
|
|
747
|
+
this.onopen({});
|
|
748
|
+
this.poll();
|
|
749
|
+
break;
|
|
750
|
+
case 403:
|
|
751
|
+
this.onerror(403);
|
|
752
|
+
this.close(1008, "forbidden", false);
|
|
753
|
+
break;
|
|
754
|
+
case 0:
|
|
755
|
+
case 500:
|
|
756
|
+
this.onerror(500);
|
|
757
|
+
this.closeAndRetry(1011, "internal server error", 500);
|
|
758
|
+
break;
|
|
759
|
+
default:
|
|
760
|
+
throw new Error(`unhandled poll status ${status}`);
|
|
761
|
+
}
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
// we collect all pushes within the current event loop by
|
|
765
|
+
// setTimeout 0, which optimizes back-to-back procedural
|
|
766
|
+
// pushes against an empty buffer
|
|
767
|
+
send(body) {
|
|
768
|
+
if (typeof body !== "string") {
|
|
769
|
+
body = arrayBufferToBase64(body);
|
|
770
|
+
}
|
|
771
|
+
if (this.currentBatch) {
|
|
772
|
+
this.currentBatch.push(body);
|
|
773
|
+
} else if (this.awaitingBatchAck) {
|
|
774
|
+
this.batchBuffer.push(body);
|
|
775
|
+
} else {
|
|
776
|
+
this.currentBatch = [body];
|
|
777
|
+
this.currentBatchTimer = setTimeout(() => {
|
|
778
|
+
this.batchSend(this.currentBatch);
|
|
779
|
+
this.currentBatch = null;
|
|
780
|
+
}, 0);
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
batchSend(messages) {
|
|
784
|
+
this.awaitingBatchAck = true;
|
|
785
|
+
this.ajax("POST", { "Content-Type": "application/x-ndjson" }, messages.join("\n"), () => this.onerror("timeout"), (resp) => {
|
|
786
|
+
this.awaitingBatchAck = false;
|
|
787
|
+
if (!resp || resp.status !== 200) {
|
|
788
|
+
this.onerror(resp && resp.status);
|
|
789
|
+
this.closeAndRetry(1011, "internal server error", false);
|
|
790
|
+
} else if (this.batchBuffer.length > 0) {
|
|
791
|
+
this.batchSend(this.batchBuffer);
|
|
792
|
+
this.batchBuffer = [];
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
close(code, reason, wasClean) {
|
|
797
|
+
for (let req of this.reqs) {
|
|
798
|
+
req.abort();
|
|
799
|
+
}
|
|
800
|
+
this.readyState = SOCKET_STATES.closed;
|
|
801
|
+
let opts = Object.assign({ code: 1e3, reason: void 0, wasClean: true }, { code, reason, wasClean });
|
|
802
|
+
this.batchBuffer = [];
|
|
803
|
+
clearTimeout(this.currentBatchTimer);
|
|
804
|
+
this.currentBatchTimer = null;
|
|
805
|
+
if (typeof CloseEvent !== "undefined") {
|
|
806
|
+
this.onclose(new CloseEvent("close", opts));
|
|
807
|
+
} else {
|
|
808
|
+
this.onclose(opts);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
ajax(method, headers, body, onCallerTimeout, callback) {
|
|
812
|
+
let req;
|
|
813
|
+
let ontimeout = () => {
|
|
814
|
+
this.reqs.delete(req);
|
|
815
|
+
onCallerTimeout();
|
|
816
|
+
};
|
|
817
|
+
req = Ajax.request(method, this.endpointURL(), headers, body, this.timeout, ontimeout, (resp) => {
|
|
818
|
+
this.reqs.delete(req);
|
|
819
|
+
if (this.isActive()) {
|
|
820
|
+
callback(resp);
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
this.reqs.add(req);
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
// js/phoenix/presence.js
|
|
828
|
+
var Presence = class _Presence {
|
|
829
|
+
/**
|
|
830
|
+
* Initializes the Presence
|
|
831
|
+
* @param {Channel} channel - The Channel
|
|
832
|
+
* @param {PresenceOptions} [opts] - The options, for example `{events: {state: "state", diff: "diff"}}`
|
|
833
|
+
*/
|
|
834
|
+
constructor(channel, opts = {}) {
|
|
835
|
+
let events = opts.events || /** @type {PresenceEvents} */
|
|
836
|
+
{ state: "presence_state", diff: "presence_diff" };
|
|
837
|
+
this.state = {};
|
|
838
|
+
this.pendingDiffs = [];
|
|
839
|
+
this.channel = channel;
|
|
840
|
+
this.joinRef = null;
|
|
841
|
+
this.caller = {
|
|
842
|
+
onJoin: function() {
|
|
843
|
+
},
|
|
844
|
+
onLeave: function() {
|
|
845
|
+
},
|
|
846
|
+
onSync: function() {
|
|
847
|
+
}
|
|
848
|
+
};
|
|
849
|
+
this.channel.on(events.state, (newState) => {
|
|
850
|
+
let { onJoin, onLeave, onSync } = this.caller;
|
|
851
|
+
this.joinRef = this.channel.joinRef();
|
|
852
|
+
this.state = _Presence.syncState(this.state, newState, onJoin, onLeave);
|
|
853
|
+
this.pendingDiffs.forEach((diff) => {
|
|
854
|
+
this.state = _Presence.syncDiff(this.state, diff, onJoin, onLeave);
|
|
855
|
+
});
|
|
856
|
+
this.pendingDiffs = [];
|
|
857
|
+
onSync();
|
|
858
|
+
});
|
|
859
|
+
this.channel.on(events.diff, (diff) => {
|
|
860
|
+
let { onJoin, onLeave, onSync } = this.caller;
|
|
861
|
+
if (this.inPendingSyncState()) {
|
|
862
|
+
this.pendingDiffs.push(diff);
|
|
863
|
+
} else {
|
|
864
|
+
this.state = _Presence.syncDiff(this.state, diff, onJoin, onLeave);
|
|
865
|
+
onSync();
|
|
866
|
+
}
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* @param {PresenceOnJoin} callback
|
|
871
|
+
*/
|
|
872
|
+
onJoin(callback) {
|
|
873
|
+
this.caller.onJoin = callback;
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* @param {PresenceOnLeave} callback
|
|
877
|
+
*/
|
|
878
|
+
onLeave(callback) {
|
|
879
|
+
this.caller.onLeave = callback;
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* @param {PresenceOnSync} callback
|
|
883
|
+
*/
|
|
884
|
+
onSync(callback) {
|
|
885
|
+
this.caller.onSync = callback;
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* Returns the array of presences, with selected metadata.
|
|
889
|
+
*
|
|
890
|
+
* @template [T=PresenceState]
|
|
891
|
+
* @param {((key: string, obj: PresenceState) => T)} [by]
|
|
892
|
+
*
|
|
893
|
+
* @returns {T[]}
|
|
894
|
+
*/
|
|
895
|
+
list(by) {
|
|
896
|
+
return _Presence.list(this.state, by);
|
|
897
|
+
}
|
|
898
|
+
inPendingSyncState() {
|
|
899
|
+
return !this.joinRef || this.joinRef !== this.channel.joinRef();
|
|
900
|
+
}
|
|
901
|
+
// lower-level public static API
|
|
902
|
+
/**
|
|
903
|
+
* Used to sync the list of presences on the server
|
|
904
|
+
* with the client's state. An optional `onJoin` and `onLeave` callback can
|
|
905
|
+
* be provided to react to changes in the client's local presences across
|
|
906
|
+
* disconnects and reconnects with the server.
|
|
907
|
+
*
|
|
908
|
+
* @param {Record<string, PresenceState>} currentState
|
|
909
|
+
* @param {Record<string, PresenceState>} newState
|
|
910
|
+
* @param {PresenceOnJoin} onJoin
|
|
911
|
+
* @param {PresenceOnLeave} onLeave
|
|
912
|
+
*
|
|
913
|
+
* @returns {Record<string, PresenceState>}
|
|
914
|
+
*/
|
|
915
|
+
static syncState(currentState, newState, onJoin, onLeave) {
|
|
916
|
+
let state = this.clone(currentState);
|
|
917
|
+
let joins = {};
|
|
918
|
+
let leaves = {};
|
|
919
|
+
this.map(state, (key, presence) => {
|
|
920
|
+
if (!newState[key]) {
|
|
921
|
+
leaves[key] = presence;
|
|
922
|
+
}
|
|
923
|
+
});
|
|
924
|
+
this.map(newState, (key, newPresence) => {
|
|
925
|
+
let currentPresence = state[key];
|
|
926
|
+
if (currentPresence) {
|
|
927
|
+
let newRefs = newPresence.metas.map((m) => m.phx_ref);
|
|
928
|
+
let curRefs = currentPresence.metas.map((m) => m.phx_ref);
|
|
929
|
+
let joinedMetas = newPresence.metas.filter((m) => curRefs.indexOf(m.phx_ref) < 0);
|
|
930
|
+
let leftMetas = currentPresence.metas.filter((m) => newRefs.indexOf(m.phx_ref) < 0);
|
|
931
|
+
if (joinedMetas.length > 0) {
|
|
932
|
+
joins[key] = newPresence;
|
|
933
|
+
joins[key].metas = joinedMetas;
|
|
934
|
+
}
|
|
935
|
+
if (leftMetas.length > 0) {
|
|
936
|
+
leaves[key] = this.clone(currentPresence);
|
|
937
|
+
leaves[key].metas = leftMetas;
|
|
938
|
+
}
|
|
939
|
+
} else {
|
|
940
|
+
joins[key] = newPresence;
|
|
941
|
+
}
|
|
942
|
+
});
|
|
943
|
+
return this.syncDiff(state, { joins, leaves }, onJoin, onLeave);
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
*
|
|
947
|
+
* Used to sync a diff of presence join and leave
|
|
948
|
+
* events from the server, as they happen. Like `syncState`, `syncDiff`
|
|
949
|
+
* accepts optional `onJoin` and `onLeave` callbacks to react to a user
|
|
950
|
+
* joining or leaving from a device.
|
|
951
|
+
*
|
|
952
|
+
* @param {Record<string, PresenceState>} state
|
|
953
|
+
* @param {PresenceDiff} diff
|
|
954
|
+
* @param {PresenceOnJoin} onJoin
|
|
955
|
+
* @param {PresenceOnLeave} onLeave
|
|
956
|
+
*
|
|
957
|
+
* @returns {Record<string, PresenceState>}
|
|
958
|
+
*/
|
|
959
|
+
static syncDiff(state, diff, onJoin, onLeave) {
|
|
960
|
+
let { joins, leaves } = this.clone(diff);
|
|
961
|
+
if (!onJoin) {
|
|
962
|
+
onJoin = function() {
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
if (!onLeave) {
|
|
966
|
+
onLeave = function() {
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
this.map(joins, (key, newPresence) => {
|
|
970
|
+
let currentPresence = state[key];
|
|
971
|
+
state[key] = this.clone(newPresence);
|
|
972
|
+
if (currentPresence) {
|
|
973
|
+
let joinedRefs = state[key].metas.map((m) => m.phx_ref);
|
|
974
|
+
let curMetas = currentPresence.metas.filter((m) => joinedRefs.indexOf(m.phx_ref) < 0);
|
|
975
|
+
state[key].metas.unshift(...curMetas);
|
|
976
|
+
}
|
|
977
|
+
onJoin(key, currentPresence, newPresence);
|
|
978
|
+
});
|
|
979
|
+
this.map(leaves, (key, leftPresence) => {
|
|
980
|
+
let currentPresence = state[key];
|
|
981
|
+
if (!currentPresence) {
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
let refsToRemove = leftPresence.metas.map((m) => m.phx_ref);
|
|
985
|
+
currentPresence.metas = currentPresence.metas.filter((p) => {
|
|
986
|
+
return refsToRemove.indexOf(p.phx_ref) < 0;
|
|
987
|
+
});
|
|
988
|
+
onLeave(key, currentPresence, leftPresence);
|
|
989
|
+
if (currentPresence.metas.length === 0) {
|
|
990
|
+
delete state[key];
|
|
991
|
+
}
|
|
992
|
+
});
|
|
993
|
+
return state;
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Returns the array of presences, with selected metadata.
|
|
997
|
+
*
|
|
998
|
+
* @template [T=PresenceState]
|
|
999
|
+
* @param {Record<string, PresenceState>} presences
|
|
1000
|
+
* @param {((key: string, obj: PresenceState) => T)} [chooser]
|
|
1001
|
+
*
|
|
1002
|
+
* @returns {T[]}
|
|
1003
|
+
*/
|
|
1004
|
+
static list(presences, chooser) {
|
|
1005
|
+
if (!chooser) {
|
|
1006
|
+
chooser = function(key, pres) {
|
|
1007
|
+
return pres;
|
|
1008
|
+
};
|
|
1009
|
+
}
|
|
1010
|
+
return this.map(presences, (key, presence) => {
|
|
1011
|
+
return chooser(key, presence);
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
// private
|
|
1015
|
+
/**
|
|
1016
|
+
* @template T
|
|
1017
|
+
* @param {Record<string, PresenceState>} obj
|
|
1018
|
+
* @param {(key: string, obj: PresenceState) => T} func
|
|
1019
|
+
*/
|
|
1020
|
+
static map(obj, func) {
|
|
1021
|
+
return Object.getOwnPropertyNames(obj).map((key) => func(key, obj[key]));
|
|
1022
|
+
}
|
|
1023
|
+
/**
|
|
1024
|
+
* @template T
|
|
1025
|
+
* @param {T} obj
|
|
1026
|
+
* @returns {T}
|
|
1027
|
+
*/
|
|
1028
|
+
static clone(obj) {
|
|
1029
|
+
return JSON.parse(JSON.stringify(obj));
|
|
1030
|
+
}
|
|
1031
|
+
};
|
|
1032
|
+
|
|
1033
|
+
// js/phoenix/serializer.js
|
|
1034
|
+
var serializer_default = {
|
|
1035
|
+
HEADER_LENGTH: 1,
|
|
1036
|
+
META_LENGTH: 4,
|
|
1037
|
+
KINDS: { push: 0, reply: 1, broadcast: 2 },
|
|
1038
|
+
/**
|
|
1039
|
+
* @template T
|
|
1040
|
+
* @param {Message<Record<string, any>>} msg
|
|
1041
|
+
* @param {(msg: ArrayBuffer | string) => T} callback
|
|
1042
|
+
* @returns {T}
|
|
1043
|
+
*/
|
|
1044
|
+
encode(msg, callback) {
|
|
1045
|
+
if (msg.payload.constructor === ArrayBuffer) {
|
|
1046
|
+
return callback(this.binaryEncode(msg));
|
|
1047
|
+
} else {
|
|
1048
|
+
let payload = [msg.join_ref, msg.ref, msg.topic, msg.event, msg.payload];
|
|
1049
|
+
return callback(JSON.stringify(payload));
|
|
1050
|
+
}
|
|
1051
|
+
},
|
|
1052
|
+
/**
|
|
1053
|
+
* @template T
|
|
1054
|
+
* @param {ArrayBuffer | string} rawPayload
|
|
1055
|
+
* @param {(msg: Message<unknown>) => T} callback
|
|
1056
|
+
* @returns {T}
|
|
1057
|
+
*/
|
|
1058
|
+
decode(rawPayload, callback) {
|
|
1059
|
+
if (rawPayload.constructor === ArrayBuffer) {
|
|
1060
|
+
return callback(this.binaryDecode(rawPayload));
|
|
1061
|
+
} else {
|
|
1062
|
+
let [join_ref, ref, topic, event, payload] = JSON.parse(rawPayload);
|
|
1063
|
+
return callback({ join_ref, ref, topic, event, payload });
|
|
1064
|
+
}
|
|
1065
|
+
},
|
|
1066
|
+
/** @private */
|
|
1067
|
+
binaryEncode(message) {
|
|
1068
|
+
let { join_ref, ref, event, topic, payload } = message;
|
|
1069
|
+
let metaLength = this.META_LENGTH + join_ref.length + ref.length + topic.length + event.length;
|
|
1070
|
+
let header = new ArrayBuffer(this.HEADER_LENGTH + metaLength);
|
|
1071
|
+
let view = new DataView(header);
|
|
1072
|
+
let offset = 0;
|
|
1073
|
+
view.setUint8(offset++, this.KINDS.push);
|
|
1074
|
+
view.setUint8(offset++, join_ref.length);
|
|
1075
|
+
view.setUint8(offset++, ref.length);
|
|
1076
|
+
view.setUint8(offset++, topic.length);
|
|
1077
|
+
view.setUint8(offset++, event.length);
|
|
1078
|
+
Array.from(join_ref, (char) => view.setUint8(offset++, char.charCodeAt(0)));
|
|
1079
|
+
Array.from(ref, (char) => view.setUint8(offset++, char.charCodeAt(0)));
|
|
1080
|
+
Array.from(topic, (char) => view.setUint8(offset++, char.charCodeAt(0)));
|
|
1081
|
+
Array.from(event, (char) => view.setUint8(offset++, char.charCodeAt(0)));
|
|
1082
|
+
var combined = new Uint8Array(header.byteLength + payload.byteLength);
|
|
1083
|
+
combined.set(new Uint8Array(header), 0);
|
|
1084
|
+
combined.set(new Uint8Array(payload), header.byteLength);
|
|
1085
|
+
return combined.buffer;
|
|
1086
|
+
},
|
|
1087
|
+
/**
|
|
1088
|
+
* @private
|
|
1089
|
+
*/
|
|
1090
|
+
binaryDecode(buffer) {
|
|
1091
|
+
let view = new DataView(buffer);
|
|
1092
|
+
let kind = view.getUint8(0);
|
|
1093
|
+
let decoder = new TextDecoder();
|
|
1094
|
+
switch (kind) {
|
|
1095
|
+
case this.KINDS.push:
|
|
1096
|
+
return this.decodePush(buffer, view, decoder);
|
|
1097
|
+
case this.KINDS.reply:
|
|
1098
|
+
return this.decodeReply(buffer, view, decoder);
|
|
1099
|
+
case this.KINDS.broadcast:
|
|
1100
|
+
return this.decodeBroadcast(buffer, view, decoder);
|
|
1101
|
+
}
|
|
1102
|
+
},
|
|
1103
|
+
/** @private */
|
|
1104
|
+
decodePush(buffer, view, decoder) {
|
|
1105
|
+
let joinRefSize = view.getUint8(1);
|
|
1106
|
+
let topicSize = view.getUint8(2);
|
|
1107
|
+
let eventSize = view.getUint8(3);
|
|
1108
|
+
let offset = this.HEADER_LENGTH + this.META_LENGTH - 1;
|
|
1109
|
+
let joinRef = decoder.decode(buffer.slice(offset, offset + joinRefSize));
|
|
1110
|
+
offset = offset + joinRefSize;
|
|
1111
|
+
let topic = decoder.decode(buffer.slice(offset, offset + topicSize));
|
|
1112
|
+
offset = offset + topicSize;
|
|
1113
|
+
let event = decoder.decode(buffer.slice(offset, offset + eventSize));
|
|
1114
|
+
offset = offset + eventSize;
|
|
1115
|
+
let data = buffer.slice(offset, buffer.byteLength);
|
|
1116
|
+
return { join_ref: joinRef, ref: null, topic, event, payload: data };
|
|
1117
|
+
},
|
|
1118
|
+
/** @private */
|
|
1119
|
+
decodeReply(buffer, view, decoder) {
|
|
1120
|
+
let joinRefSize = view.getUint8(1);
|
|
1121
|
+
let refSize = view.getUint8(2);
|
|
1122
|
+
let topicSize = view.getUint8(3);
|
|
1123
|
+
let eventSize = view.getUint8(4);
|
|
1124
|
+
let offset = this.HEADER_LENGTH + this.META_LENGTH;
|
|
1125
|
+
let joinRef = decoder.decode(buffer.slice(offset, offset + joinRefSize));
|
|
1126
|
+
offset = offset + joinRefSize;
|
|
1127
|
+
let ref = decoder.decode(buffer.slice(offset, offset + refSize));
|
|
1128
|
+
offset = offset + refSize;
|
|
1129
|
+
let topic = decoder.decode(buffer.slice(offset, offset + topicSize));
|
|
1130
|
+
offset = offset + topicSize;
|
|
1131
|
+
let event = decoder.decode(buffer.slice(offset, offset + eventSize));
|
|
1132
|
+
offset = offset + eventSize;
|
|
1133
|
+
let data = buffer.slice(offset, buffer.byteLength);
|
|
1134
|
+
let payload = { status: event, response: data };
|
|
1135
|
+
return { join_ref: joinRef, ref, topic, event: CHANNEL_EVENTS.reply, payload };
|
|
1136
|
+
},
|
|
1137
|
+
/** @private */
|
|
1138
|
+
decodeBroadcast(buffer, view, decoder) {
|
|
1139
|
+
let topicSize = view.getUint8(1);
|
|
1140
|
+
let eventSize = view.getUint8(2);
|
|
1141
|
+
let offset = this.HEADER_LENGTH + 2;
|
|
1142
|
+
let topic = decoder.decode(buffer.slice(offset, offset + topicSize));
|
|
1143
|
+
offset = offset + topicSize;
|
|
1144
|
+
let event = decoder.decode(buffer.slice(offset, offset + eventSize));
|
|
1145
|
+
offset = offset + eventSize;
|
|
1146
|
+
let data = buffer.slice(offset, buffer.byteLength);
|
|
1147
|
+
return { join_ref: null, ref: null, topic, event, payload: data };
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1150
|
+
|
|
1151
|
+
// js/phoenix/socket.js
|
|
1152
|
+
var Socket = class {
|
|
1153
|
+
/** Initializes the Socket *
|
|
1154
|
+
*
|
|
1155
|
+
* For IE8 support use an ES5-shim (https://github.com/es-shims/es5-shim)
|
|
1156
|
+
*
|
|
1157
|
+
* @constructor
|
|
1158
|
+
* @param {string} endPoint - The string WebSocket endpoint, ie, `"ws://example.com/socket"`,
|
|
1159
|
+
* `"wss://example.com"`
|
|
1160
|
+
* `"/socket"` (inherited host & protocol)
|
|
1161
|
+
* @param {SocketOptions} [opts] - Optional configuration
|
|
1162
|
+
*/
|
|
1163
|
+
constructor(endPoint, opts = {}) {
|
|
1164
|
+
this.stateChangeCallbacks = { open: [], close: [], error: [], message: [] };
|
|
1165
|
+
this.channels = [];
|
|
1166
|
+
this.sendBuffer = [];
|
|
1167
|
+
this.ref = 0;
|
|
1168
|
+
this.fallbackRef = null;
|
|
1169
|
+
this.timeout = opts.timeout || DEFAULT_TIMEOUT;
|
|
1170
|
+
this.transport = opts.transport || global.WebSocket || LongPoll;
|
|
1171
|
+
this.conn = void 0;
|
|
1172
|
+
this.primaryPassedHealthCheck = false;
|
|
1173
|
+
this.longPollFallbackMs = opts.longPollFallbackMs;
|
|
1174
|
+
this.fallbackTimer = null;
|
|
1175
|
+
this.sessionStore = opts.sessionStorage || global && global.sessionStorage;
|
|
1176
|
+
this.establishedConnections = 0;
|
|
1177
|
+
this.defaultEncoder = serializer_default.encode.bind(serializer_default);
|
|
1178
|
+
this.defaultDecoder = serializer_default.decode.bind(serializer_default);
|
|
1179
|
+
this.closeWasClean = false;
|
|
1180
|
+
this.disconnecting = false;
|
|
1181
|
+
this.binaryType = opts.binaryType || "arraybuffer";
|
|
1182
|
+
this.connectClock = 1;
|
|
1183
|
+
this.pageHidden = false;
|
|
1184
|
+
this.encode = void 0;
|
|
1185
|
+
this.decode = void 0;
|
|
1186
|
+
if (this.transport !== LongPoll) {
|
|
1187
|
+
this.encode = opts.encode || this.defaultEncoder;
|
|
1188
|
+
this.decode = opts.decode || this.defaultDecoder;
|
|
1189
|
+
} else {
|
|
1190
|
+
this.encode = this.defaultEncoder;
|
|
1191
|
+
this.decode = this.defaultDecoder;
|
|
1192
|
+
}
|
|
1193
|
+
let awaitingConnectionOnPageShow = null;
|
|
1194
|
+
if (phxWindow && phxWindow.addEventListener) {
|
|
1195
|
+
phxWindow.addEventListener("pagehide", (_e) => {
|
|
1196
|
+
if (this.conn) {
|
|
1197
|
+
this.disconnect();
|
|
1198
|
+
awaitingConnectionOnPageShow = this.connectClock;
|
|
1199
|
+
}
|
|
1200
|
+
});
|
|
1201
|
+
phxWindow.addEventListener("pageshow", (_e) => {
|
|
1202
|
+
if (awaitingConnectionOnPageShow === this.connectClock) {
|
|
1203
|
+
awaitingConnectionOnPageShow = null;
|
|
1204
|
+
this.connect();
|
|
1205
|
+
}
|
|
1206
|
+
});
|
|
1207
|
+
phxWindow.addEventListener("visibilitychange", () => {
|
|
1208
|
+
if (document.visibilityState === "hidden") {
|
|
1209
|
+
this.pageHidden = true;
|
|
1210
|
+
} else {
|
|
1211
|
+
this.pageHidden = false;
|
|
1212
|
+
if (!this.isConnected()) {
|
|
1213
|
+
this.teardown(() => this.connect());
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
});
|
|
1217
|
+
}
|
|
1218
|
+
this.heartbeatIntervalMs = opts.heartbeatIntervalMs || 3e4;
|
|
1219
|
+
this.autoSendHeartbeat = opts.autoSendHeartbeat ?? true;
|
|
1220
|
+
this.heartbeatCallback = opts.heartbeatCallback ?? (() => {
|
|
1221
|
+
});
|
|
1222
|
+
this.rejoinAfterMs = (tries) => {
|
|
1223
|
+
if (opts.rejoinAfterMs) {
|
|
1224
|
+
return opts.rejoinAfterMs(tries);
|
|
1225
|
+
} else {
|
|
1226
|
+
return [1e3, 2e3, 5e3][tries - 1] || 1e4;
|
|
1227
|
+
}
|
|
1228
|
+
};
|
|
1229
|
+
this.reconnectAfterMs = (tries) => {
|
|
1230
|
+
if (opts.reconnectAfterMs) {
|
|
1231
|
+
return opts.reconnectAfterMs(tries);
|
|
1232
|
+
} else {
|
|
1233
|
+
return [10, 50, 100, 150, 200, 250, 500, 1e3, 2e3][tries - 1] || 5e3;
|
|
1234
|
+
}
|
|
1235
|
+
};
|
|
1236
|
+
this.logger = opts.logger || null;
|
|
1237
|
+
if (!this.logger && opts.debug) {
|
|
1238
|
+
this.logger = (kind, msg, data) => {
|
|
1239
|
+
console.log(`${kind}: ${msg}`, data);
|
|
1240
|
+
};
|
|
1241
|
+
}
|
|
1242
|
+
this.longpollerTimeout = opts.longpollerTimeout || 2e4;
|
|
1243
|
+
this.params = closure(opts.params || {});
|
|
1244
|
+
this.endPoint = `${endPoint}/${TRANSPORTS.websocket}`;
|
|
1245
|
+
this.vsn = opts.vsn || DEFAULT_VSN;
|
|
1246
|
+
this.heartbeatTimeoutTimer = null;
|
|
1247
|
+
this.heartbeatTimer = null;
|
|
1248
|
+
this.heartbeatSentAt = null;
|
|
1249
|
+
this.pendingHeartbeatRef = null;
|
|
1250
|
+
this.reconnectTimer = new Timer(() => {
|
|
1251
|
+
if (this.pageHidden) {
|
|
1252
|
+
this.log("Not reconnecting as page is hidden!");
|
|
1253
|
+
this.teardown();
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
this.teardown(async () => {
|
|
1257
|
+
if (opts.beforeReconnect) await opts.beforeReconnect();
|
|
1258
|
+
this.connect();
|
|
1259
|
+
});
|
|
1260
|
+
}, this.reconnectAfterMs);
|
|
1261
|
+
this.authToken = opts.authToken;
|
|
1262
|
+
}
|
|
1263
|
+
/**
|
|
1264
|
+
* Returns the LongPoll transport reference
|
|
1265
|
+
*/
|
|
1266
|
+
getLongPollTransport() {
|
|
1267
|
+
return LongPoll;
|
|
1268
|
+
}
|
|
1269
|
+
/**
|
|
1270
|
+
* Disconnects and replaces the active transport
|
|
1271
|
+
*
|
|
1272
|
+
* @param {SocketTransport} newTransport - The new transport class to instantiate
|
|
1273
|
+
*
|
|
1274
|
+
*/
|
|
1275
|
+
replaceTransport(newTransport) {
|
|
1276
|
+
this.connectClock++;
|
|
1277
|
+
this.closeWasClean = true;
|
|
1278
|
+
clearTimeout(this.fallbackTimer);
|
|
1279
|
+
this.reconnectTimer.reset();
|
|
1280
|
+
if (this.conn) {
|
|
1281
|
+
this.conn.close();
|
|
1282
|
+
this.conn = null;
|
|
1283
|
+
}
|
|
1284
|
+
this.transport = newTransport;
|
|
1285
|
+
}
|
|
1286
|
+
/**
|
|
1287
|
+
* Returns the socket protocol
|
|
1288
|
+
*
|
|
1289
|
+
* @returns {"wss" | "ws"}
|
|
1290
|
+
*/
|
|
1291
|
+
protocol() {
|
|
1292
|
+
return location.protocol.match(/^https/) ? "wss" : "ws";
|
|
1293
|
+
}
|
|
1294
|
+
/**
|
|
1295
|
+
* The fully qualified socket url
|
|
1296
|
+
*
|
|
1297
|
+
* @returns {string}
|
|
1298
|
+
*/
|
|
1299
|
+
endPointURL() {
|
|
1300
|
+
let uri = Ajax.appendParams(
|
|
1301
|
+
Ajax.appendParams(this.endPoint, this.params()),
|
|
1302
|
+
{ vsn: this.vsn }
|
|
1303
|
+
);
|
|
1304
|
+
if (uri.charAt(0) !== "/") {
|
|
1305
|
+
return uri;
|
|
1306
|
+
}
|
|
1307
|
+
if (uri.charAt(1) === "/") {
|
|
1308
|
+
return `${this.protocol()}:${uri}`;
|
|
1309
|
+
}
|
|
1310
|
+
return `${this.protocol()}://${location.host}${uri}`;
|
|
1311
|
+
}
|
|
1312
|
+
/**
|
|
1313
|
+
* Disconnects the socket
|
|
1314
|
+
*
|
|
1315
|
+
* See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes for valid status codes.
|
|
1316
|
+
*
|
|
1317
|
+
* @param {() => void} [callback] - Optional callback which is called after socket is disconnected.
|
|
1318
|
+
* @param {number} [code] - A status code for disconnection (Optional).
|
|
1319
|
+
* @param {string} [reason] - A textual description of the reason to disconnect. (Optional)
|
|
1320
|
+
*/
|
|
1321
|
+
disconnect(callback, code, reason) {
|
|
1322
|
+
this.connectClock++;
|
|
1323
|
+
this.disconnecting = true;
|
|
1324
|
+
this.closeWasClean = true;
|
|
1325
|
+
clearTimeout(this.fallbackTimer);
|
|
1326
|
+
this.reconnectTimer.reset();
|
|
1327
|
+
this.teardown(() => {
|
|
1328
|
+
this.disconnecting = false;
|
|
1329
|
+
callback && callback();
|
|
1330
|
+
}, code, reason);
|
|
1331
|
+
}
|
|
1332
|
+
/**
|
|
1333
|
+
* @param {Params} [params] - [DEPRECATED] The params to send when connecting, for example `{user_id: userToken}`
|
|
1334
|
+
*
|
|
1335
|
+
* Passing params to connect is deprecated; pass them in the Socket constructor instead:
|
|
1336
|
+
* `new Socket("/socket", {params: {user_id: userToken}})`.
|
|
1337
|
+
*/
|
|
1338
|
+
connect(params) {
|
|
1339
|
+
if (params) {
|
|
1340
|
+
console && console.log("passing params to connect is deprecated. Instead pass :params to the Socket constructor");
|
|
1341
|
+
this.params = closure(params);
|
|
1342
|
+
}
|
|
1343
|
+
if (this.conn && !this.disconnecting) {
|
|
1344
|
+
return;
|
|
1345
|
+
}
|
|
1346
|
+
if (this.longPollFallbackMs && this.transport !== LongPoll) {
|
|
1347
|
+
this.connectWithFallback(LongPoll, this.longPollFallbackMs);
|
|
1348
|
+
} else {
|
|
1349
|
+
this.transportConnect();
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Logs the message. Override `this.logger` for specialized logging. noops by default
|
|
1354
|
+
* @param {string} kind
|
|
1355
|
+
* @param {string} msg
|
|
1356
|
+
* @param {Object} data
|
|
1357
|
+
*/
|
|
1358
|
+
log(kind, msg, data) {
|
|
1359
|
+
this.logger && this.logger(kind, msg, data);
|
|
1360
|
+
}
|
|
1361
|
+
/**
|
|
1362
|
+
* Returns true if a logger has been set on this socket.
|
|
1363
|
+
*/
|
|
1364
|
+
hasLogger() {
|
|
1365
|
+
return this.logger !== null;
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Registers callbacks for connection open events
|
|
1369
|
+
*
|
|
1370
|
+
* @example socket.onOpen(function(){ console.info("the socket was opened") })
|
|
1371
|
+
*
|
|
1372
|
+
* @param {SocketOnOpen} callback
|
|
1373
|
+
*/
|
|
1374
|
+
onOpen(callback) {
|
|
1375
|
+
let ref = this.makeRef();
|
|
1376
|
+
this.stateChangeCallbacks.open.push([ref, callback]);
|
|
1377
|
+
return ref;
|
|
1378
|
+
}
|
|
1379
|
+
/**
|
|
1380
|
+
* Registers callbacks for connection close events
|
|
1381
|
+
* @param {SocketOnClose} callback
|
|
1382
|
+
* @returns {string}
|
|
1383
|
+
*/
|
|
1384
|
+
onClose(callback) {
|
|
1385
|
+
let ref = this.makeRef();
|
|
1386
|
+
this.stateChangeCallbacks.close.push([ref, callback]);
|
|
1387
|
+
return ref;
|
|
1388
|
+
}
|
|
1389
|
+
/**
|
|
1390
|
+
* Registers callbacks for connection error events
|
|
1391
|
+
*
|
|
1392
|
+
* @example socket.onError(function(error){ alert("An error occurred") })
|
|
1393
|
+
*
|
|
1394
|
+
* @param {SocketOnError} callback
|
|
1395
|
+
* @returns {string}
|
|
1396
|
+
*/
|
|
1397
|
+
onError(callback) {
|
|
1398
|
+
let ref = this.makeRef();
|
|
1399
|
+
this.stateChangeCallbacks.error.push([ref, callback]);
|
|
1400
|
+
return ref;
|
|
1401
|
+
}
|
|
1402
|
+
/**
|
|
1403
|
+
* Registers callbacks for connection message events
|
|
1404
|
+
* @param {SocketOnMessage} callback
|
|
1405
|
+
* @returns {string}
|
|
1406
|
+
*/
|
|
1407
|
+
onMessage(callback) {
|
|
1408
|
+
let ref = this.makeRef();
|
|
1409
|
+
this.stateChangeCallbacks.message.push([ref, callback]);
|
|
1410
|
+
return ref;
|
|
1411
|
+
}
|
|
1412
|
+
/**
|
|
1413
|
+
* Sets a callback that receives lifecycle events for internal heartbeat messages.
|
|
1414
|
+
* Useful for instrumenting connection health (e.g. sent/ok/timeout/disconnected).
|
|
1415
|
+
* @param {HeartbeatCallback} callback
|
|
1416
|
+
*/
|
|
1417
|
+
onHeartbeat(callback) {
|
|
1418
|
+
this.heartbeatCallback = callback;
|
|
1419
|
+
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Pings the server and invokes the callback with the RTT in milliseconds
|
|
1422
|
+
* @param {(timeDelta: number) => void} callback
|
|
1423
|
+
*
|
|
1424
|
+
* Returns true if the ping was pushed or false if unable to be pushed.
|
|
1425
|
+
*/
|
|
1426
|
+
ping(callback) {
|
|
1427
|
+
if (!this.isConnected()) {
|
|
1428
|
+
return false;
|
|
1429
|
+
}
|
|
1430
|
+
let ref = this.makeRef();
|
|
1431
|
+
let startTime = Date.now();
|
|
1432
|
+
this.push({ topic: "phoenix", event: "heartbeat", payload: {}, ref });
|
|
1433
|
+
let onMsgRef = this.onMessage((msg) => {
|
|
1434
|
+
if (msg.ref === ref) {
|
|
1435
|
+
this.off([onMsgRef]);
|
|
1436
|
+
callback(Date.now() - startTime);
|
|
1437
|
+
}
|
|
1438
|
+
});
|
|
1439
|
+
return true;
|
|
1440
|
+
}
|
|
1441
|
+
/**
|
|
1442
|
+
* @private
|
|
1443
|
+
*/
|
|
1444
|
+
transportConnect() {
|
|
1445
|
+
this.connectClock++;
|
|
1446
|
+
this.closeWasClean = false;
|
|
1447
|
+
let protocols = void 0;
|
|
1448
|
+
if (this.authToken) {
|
|
1449
|
+
protocols = ["phoenix", `${AUTH_TOKEN_PREFIX}${btoa(this.authToken).replace(/=/g, "")}`];
|
|
1450
|
+
}
|
|
1451
|
+
this.conn = new this.transport(this.endPointURL(), protocols);
|
|
1452
|
+
this.conn.binaryType = this.binaryType;
|
|
1453
|
+
this.conn.timeout = this.longpollerTimeout;
|
|
1454
|
+
this.conn.onopen = () => this.onConnOpen();
|
|
1455
|
+
this.conn.onerror = (error) => this.onConnError(error);
|
|
1456
|
+
this.conn.onmessage = (event) => this.onConnMessage(event);
|
|
1457
|
+
this.conn.onclose = (event) => this.onConnClose(event);
|
|
1458
|
+
}
|
|
1459
|
+
getSession(key) {
|
|
1460
|
+
return this.sessionStore && this.sessionStore.getItem(key);
|
|
1461
|
+
}
|
|
1462
|
+
storeSession(key, val) {
|
|
1463
|
+
this.sessionStore && this.sessionStore.setItem(key, val);
|
|
1464
|
+
}
|
|
1465
|
+
connectWithFallback(fallbackTransport, fallbackThreshold = 2500) {
|
|
1466
|
+
clearTimeout(this.fallbackTimer);
|
|
1467
|
+
let established = false;
|
|
1468
|
+
let primaryTransport = true;
|
|
1469
|
+
let openRef, errorRef;
|
|
1470
|
+
let fallback = (reason) => {
|
|
1471
|
+
this.log("transport", `falling back to ${fallbackTransport.name}...`, reason);
|
|
1472
|
+
this.off([openRef, errorRef]);
|
|
1473
|
+
primaryTransport = false;
|
|
1474
|
+
this.replaceTransport(fallbackTransport);
|
|
1475
|
+
this.transportConnect();
|
|
1476
|
+
};
|
|
1477
|
+
if (this.getSession(`phx:fallback:${fallbackTransport.name}`)) {
|
|
1478
|
+
return fallback("memorized");
|
|
1479
|
+
}
|
|
1480
|
+
this.fallbackTimer = setTimeout(fallback, fallbackThreshold);
|
|
1481
|
+
errorRef = this.onError((reason) => {
|
|
1482
|
+
this.log("transport", "error", reason);
|
|
1483
|
+
if (primaryTransport && !established) {
|
|
1484
|
+
clearTimeout(this.fallbackTimer);
|
|
1485
|
+
fallback(reason);
|
|
1486
|
+
}
|
|
1487
|
+
});
|
|
1488
|
+
if (this.fallbackRef) {
|
|
1489
|
+
this.off([this.fallbackRef]);
|
|
1490
|
+
}
|
|
1491
|
+
this.fallbackRef = this.onOpen(() => {
|
|
1492
|
+
established = true;
|
|
1493
|
+
if (!primaryTransport) {
|
|
1494
|
+
if (!this.primaryPassedHealthCheck) {
|
|
1495
|
+
this.storeSession(`phx:fallback:${fallbackTransport.name}`, "true");
|
|
1496
|
+
}
|
|
1497
|
+
return this.log("transport", `established ${fallbackTransport.name} fallback`);
|
|
1498
|
+
}
|
|
1499
|
+
clearTimeout(this.fallbackTimer);
|
|
1500
|
+
this.fallbackTimer = setTimeout(fallback, fallbackThreshold);
|
|
1501
|
+
this.ping((rtt) => {
|
|
1502
|
+
this.log("transport", "connected to primary after", rtt);
|
|
1503
|
+
this.primaryPassedHealthCheck = true;
|
|
1504
|
+
clearTimeout(this.fallbackTimer);
|
|
1505
|
+
});
|
|
1506
|
+
});
|
|
1507
|
+
this.transportConnect();
|
|
1508
|
+
}
|
|
1509
|
+
clearHeartbeats() {
|
|
1510
|
+
clearTimeout(this.heartbeatTimer);
|
|
1511
|
+
clearTimeout(this.heartbeatTimeoutTimer);
|
|
1512
|
+
}
|
|
1513
|
+
onConnOpen() {
|
|
1514
|
+
if (this.hasLogger()) this.log("transport", `connected to ${this.endPointURL()}`);
|
|
1515
|
+
this.closeWasClean = false;
|
|
1516
|
+
this.disconnecting = false;
|
|
1517
|
+
this.establishedConnections++;
|
|
1518
|
+
this.flushSendBuffer();
|
|
1519
|
+
this.reconnectTimer.reset();
|
|
1520
|
+
if (this.autoSendHeartbeat) {
|
|
1521
|
+
this.resetHeartbeat();
|
|
1522
|
+
}
|
|
1523
|
+
this.triggerStateCallbacks("open");
|
|
1524
|
+
}
|
|
1525
|
+
/**
|
|
1526
|
+
* @private
|
|
1527
|
+
*/
|
|
1528
|
+
heartbeatTimeout() {
|
|
1529
|
+
if (this.pendingHeartbeatRef) {
|
|
1530
|
+
this.pendingHeartbeatRef = null;
|
|
1531
|
+
this.heartbeatSentAt = null;
|
|
1532
|
+
if (this.hasLogger()) {
|
|
1533
|
+
this.log("transport", "heartbeat timeout. Attempting to re-establish connection");
|
|
1534
|
+
}
|
|
1535
|
+
try {
|
|
1536
|
+
this.heartbeatCallback("timeout");
|
|
1537
|
+
} catch (e) {
|
|
1538
|
+
this.log("error", "error in heartbeat callback", e);
|
|
1539
|
+
}
|
|
1540
|
+
this.triggerChanError();
|
|
1541
|
+
this.closeWasClean = false;
|
|
1542
|
+
this.teardown(() => this.reconnectTimer.scheduleTimeout(), WS_CLOSE_NORMAL, "heartbeat timeout");
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
resetHeartbeat() {
|
|
1546
|
+
if (this.conn && this.conn.skipHeartbeat) {
|
|
1547
|
+
return;
|
|
1548
|
+
}
|
|
1549
|
+
this.pendingHeartbeatRef = null;
|
|
1550
|
+
this.clearHeartbeats();
|
|
1551
|
+
this.heartbeatTimer = setTimeout(() => this.sendHeartbeat(), this.heartbeatIntervalMs);
|
|
1552
|
+
}
|
|
1553
|
+
teardown(callback, code, reason) {
|
|
1554
|
+
if (!this.conn) {
|
|
1555
|
+
return callback && callback();
|
|
1556
|
+
}
|
|
1557
|
+
let connectClock = this.connectClock;
|
|
1558
|
+
this.waitForBufferDone(() => {
|
|
1559
|
+
if (connectClock !== this.connectClock) {
|
|
1560
|
+
return;
|
|
1561
|
+
}
|
|
1562
|
+
if (this.conn) {
|
|
1563
|
+
if (code) {
|
|
1564
|
+
this.conn.close(code, reason || "");
|
|
1565
|
+
} else {
|
|
1566
|
+
this.conn.close();
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
this.waitForSocketClosed(() => {
|
|
1570
|
+
if (connectClock !== this.connectClock) {
|
|
1571
|
+
return;
|
|
1572
|
+
}
|
|
1573
|
+
if (this.conn) {
|
|
1574
|
+
this.conn.onopen = function() {
|
|
1575
|
+
};
|
|
1576
|
+
this.conn.onerror = function() {
|
|
1577
|
+
};
|
|
1578
|
+
this.conn.onmessage = function() {
|
|
1579
|
+
};
|
|
1580
|
+
this.conn.onclose = function() {
|
|
1581
|
+
};
|
|
1582
|
+
this.conn = null;
|
|
1583
|
+
}
|
|
1584
|
+
callback && callback();
|
|
1585
|
+
});
|
|
1586
|
+
});
|
|
1587
|
+
}
|
|
1588
|
+
waitForBufferDone(callback, tries = 1) {
|
|
1589
|
+
if (tries === 5 || !this.conn || !this.conn.bufferedAmount) {
|
|
1590
|
+
callback();
|
|
1591
|
+
return;
|
|
1592
|
+
}
|
|
1593
|
+
setTimeout(() => {
|
|
1594
|
+
this.waitForBufferDone(callback, tries + 1);
|
|
1595
|
+
}, 150 * tries);
|
|
1596
|
+
}
|
|
1597
|
+
waitForSocketClosed(callback, tries = 1) {
|
|
1598
|
+
if (tries === 5 || !this.conn || this.conn.readyState === SOCKET_STATES.closed) {
|
|
1599
|
+
callback();
|
|
1600
|
+
return;
|
|
1601
|
+
}
|
|
1602
|
+
setTimeout(() => {
|
|
1603
|
+
this.waitForSocketClosed(callback, tries + 1);
|
|
1604
|
+
}, 150 * tries);
|
|
1605
|
+
}
|
|
1606
|
+
/**
|
|
1607
|
+
* @param {CloseEvent} event
|
|
1608
|
+
*/
|
|
1609
|
+
onConnClose(event) {
|
|
1610
|
+
if (this.conn) this.conn.onclose = () => {
|
|
1611
|
+
};
|
|
1612
|
+
if (this.hasLogger()) this.log("transport", "close", event);
|
|
1613
|
+
this.triggerChanError();
|
|
1614
|
+
this.clearHeartbeats();
|
|
1615
|
+
if (!this.closeWasClean) {
|
|
1616
|
+
this.reconnectTimer.scheduleTimeout();
|
|
1617
|
+
}
|
|
1618
|
+
this.triggerStateCallbacks("close", event);
|
|
1619
|
+
}
|
|
1620
|
+
/**
|
|
1621
|
+
* @private
|
|
1622
|
+
* @param {Event} error
|
|
1623
|
+
*/
|
|
1624
|
+
onConnError(error) {
|
|
1625
|
+
if (this.hasLogger()) this.log("transport", error);
|
|
1626
|
+
let transportBefore = this.transport;
|
|
1627
|
+
let establishedBefore = this.establishedConnections;
|
|
1628
|
+
this.triggerStateCallbacks("error", error, transportBefore, establishedBefore);
|
|
1629
|
+
if (transportBefore === this.transport || establishedBefore > 0) {
|
|
1630
|
+
this.triggerChanError();
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* @private
|
|
1635
|
+
*/
|
|
1636
|
+
triggerChanError() {
|
|
1637
|
+
this.channels.forEach((channel) => {
|
|
1638
|
+
if (!(channel.isErrored() || channel.isLeaving() || channel.isClosed())) {
|
|
1639
|
+
channel.trigger(CHANNEL_EVENTS.error);
|
|
1640
|
+
}
|
|
1641
|
+
});
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* @returns {string}
|
|
1645
|
+
*/
|
|
1646
|
+
connectionState() {
|
|
1647
|
+
switch (this.conn && this.conn.readyState) {
|
|
1648
|
+
case SOCKET_STATES.connecting:
|
|
1649
|
+
return "connecting";
|
|
1650
|
+
case SOCKET_STATES.open:
|
|
1651
|
+
return "open";
|
|
1652
|
+
case SOCKET_STATES.closing:
|
|
1653
|
+
return "closing";
|
|
1654
|
+
default:
|
|
1655
|
+
return "closed";
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
/**
|
|
1659
|
+
* @returns {boolean}
|
|
1660
|
+
*/
|
|
1661
|
+
isConnected() {
|
|
1662
|
+
return this.connectionState() === "open";
|
|
1663
|
+
}
|
|
1664
|
+
/**
|
|
1665
|
+
*
|
|
1666
|
+
* @param {Channel} channel
|
|
1667
|
+
*/
|
|
1668
|
+
remove(channel) {
|
|
1669
|
+
this.off(channel.stateChangeRefs);
|
|
1670
|
+
this.channels = this.channels.filter((c) => c !== channel);
|
|
1671
|
+
}
|
|
1672
|
+
/**
|
|
1673
|
+
* Removes `onOpen`, `onClose`, `onError,` and `onMessage` registrations.
|
|
1674
|
+
*
|
|
1675
|
+
* @param {string[]} refs - list of refs returned by calls to
|
|
1676
|
+
* `onOpen`, `onClose`, `onError,` and `onMessage`
|
|
1677
|
+
*/
|
|
1678
|
+
off(refs) {
|
|
1679
|
+
for (let key in this.stateChangeCallbacks) {
|
|
1680
|
+
this.stateChangeCallbacks[key] = this.stateChangeCallbacks[key].filter(([ref]) => {
|
|
1681
|
+
return refs.indexOf(ref) === -1;
|
|
1682
|
+
});
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
/**
|
|
1686
|
+
* Initiates a new channel for the given topic
|
|
1687
|
+
*
|
|
1688
|
+
* @param {string} topic
|
|
1689
|
+
* @param {Params | (() => Params)} [chanParams]- Parameters for the channel
|
|
1690
|
+
* @returns {Channel}
|
|
1691
|
+
*/
|
|
1692
|
+
channel(topic, chanParams = {}) {
|
|
1693
|
+
let chan = new Channel(topic, chanParams, this);
|
|
1694
|
+
this.channels.push(chan);
|
|
1695
|
+
return chan;
|
|
1696
|
+
}
|
|
1697
|
+
/**
|
|
1698
|
+
* @param {Message<Record<string, any>>} data
|
|
1699
|
+
*/
|
|
1700
|
+
push(data) {
|
|
1701
|
+
if (this.hasLogger()) {
|
|
1702
|
+
let { topic, event, payload, ref, join_ref } = data;
|
|
1703
|
+
this.log("push", `${topic} ${event} (${join_ref}, ${ref})`, payload);
|
|
1704
|
+
}
|
|
1705
|
+
if (this.isConnected()) {
|
|
1706
|
+
this.encode(data, (result) => this.conn.send(result));
|
|
1707
|
+
} else {
|
|
1708
|
+
this.sendBuffer.push(() => this.encode(data, (result) => this.conn.send(result)));
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
/**
|
|
1712
|
+
* Return the next message ref, accounting for overflows
|
|
1713
|
+
* @returns {string}
|
|
1714
|
+
*/
|
|
1715
|
+
makeRef() {
|
|
1716
|
+
let newRef = this.ref + 1;
|
|
1717
|
+
if (newRef === this.ref) {
|
|
1718
|
+
this.ref = 0;
|
|
1719
|
+
} else {
|
|
1720
|
+
this.ref = newRef;
|
|
1721
|
+
}
|
|
1722
|
+
return this.ref.toString();
|
|
1723
|
+
}
|
|
1724
|
+
sendHeartbeat() {
|
|
1725
|
+
if (!this.isConnected()) {
|
|
1726
|
+
try {
|
|
1727
|
+
this.heartbeatCallback("disconnected");
|
|
1728
|
+
} catch (e) {
|
|
1729
|
+
this.log("error", "error in heartbeat callback", e);
|
|
1730
|
+
}
|
|
1731
|
+
return;
|
|
1732
|
+
}
|
|
1733
|
+
if (this.pendingHeartbeatRef) {
|
|
1734
|
+
this.heartbeatTimeout();
|
|
1735
|
+
return;
|
|
1736
|
+
}
|
|
1737
|
+
this.pendingHeartbeatRef = this.makeRef();
|
|
1738
|
+
this.heartbeatSentAt = Date.now();
|
|
1739
|
+
this.push({ topic: "phoenix", event: "heartbeat", payload: {}, ref: this.pendingHeartbeatRef });
|
|
1740
|
+
try {
|
|
1741
|
+
this.heartbeatCallback("sent");
|
|
1742
|
+
} catch (e) {
|
|
1743
|
+
this.log("error", "error in heartbeat callback", e);
|
|
1744
|
+
}
|
|
1745
|
+
this.heartbeatTimeoutTimer = setTimeout(() => this.heartbeatTimeout(), this.heartbeatIntervalMs);
|
|
1746
|
+
}
|
|
1747
|
+
flushSendBuffer() {
|
|
1748
|
+
if (this.isConnected() && this.sendBuffer.length > 0) {
|
|
1749
|
+
this.sendBuffer.forEach((callback) => callback());
|
|
1750
|
+
this.sendBuffer = [];
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
/**
|
|
1754
|
+
* @param {MessageEvent<any>} rawMessage
|
|
1755
|
+
*/
|
|
1756
|
+
onConnMessage(rawMessage) {
|
|
1757
|
+
this.decode(rawMessage.data, (msg) => {
|
|
1758
|
+
let { topic, event, payload, ref, join_ref } = msg;
|
|
1759
|
+
if (ref && ref === this.pendingHeartbeatRef) {
|
|
1760
|
+
const latency = this.heartbeatSentAt ? Date.now() - this.heartbeatSentAt : void 0;
|
|
1761
|
+
this.clearHeartbeats();
|
|
1762
|
+
try {
|
|
1763
|
+
this.heartbeatCallback(payload.status === "ok" ? "ok" : "error", latency);
|
|
1764
|
+
} catch (e) {
|
|
1765
|
+
this.log("error", "error in heartbeat callback", e);
|
|
1766
|
+
}
|
|
1767
|
+
this.pendingHeartbeatRef = null;
|
|
1768
|
+
this.heartbeatSentAt = null;
|
|
1769
|
+
if (this.autoSendHeartbeat) {
|
|
1770
|
+
this.heartbeatTimer = setTimeout(() => this.sendHeartbeat(), this.heartbeatIntervalMs);
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
if (this.hasLogger()) this.log("receive", `${payload.status || ""} ${topic} ${event} ${ref && "(" + ref + ")" || ""}`.trim(), payload);
|
|
1774
|
+
for (let i = 0; i < this.channels.length; i++) {
|
|
1775
|
+
const channel = this.channels[i];
|
|
1776
|
+
if (!channel.isMember(topic, event, payload, join_ref)) {
|
|
1777
|
+
continue;
|
|
1778
|
+
}
|
|
1779
|
+
channel.trigger(event, payload, ref, join_ref);
|
|
1780
|
+
}
|
|
1781
|
+
this.triggerStateCallbacks("message", msg);
|
|
1782
|
+
});
|
|
1783
|
+
}
|
|
1784
|
+
/**
|
|
1785
|
+
* @private
|
|
1786
|
+
* @template {keyof SocketStateChangeCallbacks} K
|
|
1787
|
+
* @param {K} event
|
|
1788
|
+
* @param {...Parameters<SocketStateChangeCallbacks[K][number][1]>} args
|
|
1789
|
+
* @returns {void}
|
|
1790
|
+
*/
|
|
1791
|
+
triggerStateCallbacks(event, ...args) {
|
|
1792
|
+
try {
|
|
1793
|
+
this.stateChangeCallbacks[event].forEach(([_, callback]) => {
|
|
1794
|
+
try {
|
|
1795
|
+
callback(...args);
|
|
1796
|
+
} catch (e) {
|
|
1797
|
+
this.log("error", `error in ${event} callback`, e);
|
|
1798
|
+
}
|
|
1799
|
+
});
|
|
1800
|
+
} catch (e) {
|
|
1801
|
+
this.log("error", `error triggering ${event} callbacks`, e);
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
leaveOpenTopic(topic) {
|
|
1805
|
+
let dupChannel = this.channels.find((c) => c.topic === topic && (c.isJoined() || c.isJoining()));
|
|
1806
|
+
if (dupChannel) {
|
|
1807
|
+
if (this.hasLogger()) this.log("transport", `leaving duplicate topic "${topic}"`);
|
|
1808
|
+
dupChannel.leave();
|
|
1809
|
+
}
|
|
1810
|
+
}
|
|
1811
|
+
};
|
|
1812
|
+
//# sourceMappingURL=phoenix.cjs.js.map
|