@scalemule/sdk 0.0.62 → 0.0.63
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/dist/index.d.mts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +123 -4
- package/dist/index.mjs +123 -4
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -2011,7 +2011,11 @@ declare class RealtimeService extends ServiceModule {
|
|
|
2011
2011
|
private reconnectAttempt;
|
|
2012
2012
|
private reconnectTimer;
|
|
2013
2013
|
private heartbeatTimer;
|
|
2014
|
+
private connectWatchdogTimer;
|
|
2014
2015
|
private authenticated;
|
|
2016
|
+
private lastInboundAt;
|
|
2017
|
+
private wakeHandler;
|
|
2018
|
+
private visibilityHandler;
|
|
2015
2019
|
/** Current connection status */
|
|
2016
2020
|
get status(): ConnectionStatus;
|
|
2017
2021
|
/**
|
|
@@ -2051,10 +2055,23 @@ declare class RealtimeService extends ServiceModule {
|
|
|
2051
2055
|
private dispatchPresence;
|
|
2052
2056
|
private sendWs;
|
|
2053
2057
|
private scheduleReconnect;
|
|
2058
|
+
/** Drop the current socket without triggering its onclose reconnect path. */
|
|
2059
|
+
private teardownSocket;
|
|
2060
|
+
/** Tear down a dead-but-open socket and reconnect. */
|
|
2061
|
+
private forceReconnect;
|
|
2062
|
+
/**
|
|
2063
|
+
* Browsers suspend timers and silently kill sockets during sleep or network
|
|
2064
|
+
* changes. When the page wakes (tab visible, window focused, network back),
|
|
2065
|
+
* verify the connection instead of waiting for the next heartbeat tick.
|
|
2066
|
+
*/
|
|
2067
|
+
private installWakeListeners;
|
|
2068
|
+
private removeWakeListeners;
|
|
2069
|
+
private handleWake;
|
|
2054
2070
|
private getReconnectDelay;
|
|
2055
2071
|
private startHeartbeat;
|
|
2056
2072
|
private clearHeartbeat;
|
|
2057
2073
|
private clearTimers;
|
|
2074
|
+
private clearConnectWatchdog;
|
|
2058
2075
|
private setStatus;
|
|
2059
2076
|
}
|
|
2060
2077
|
|
package/dist/index.d.ts
CHANGED
|
@@ -2011,7 +2011,11 @@ declare class RealtimeService extends ServiceModule {
|
|
|
2011
2011
|
private reconnectAttempt;
|
|
2012
2012
|
private reconnectTimer;
|
|
2013
2013
|
private heartbeatTimer;
|
|
2014
|
+
private connectWatchdogTimer;
|
|
2014
2015
|
private authenticated;
|
|
2016
|
+
private lastInboundAt;
|
|
2017
|
+
private wakeHandler;
|
|
2018
|
+
private visibilityHandler;
|
|
2015
2019
|
/** Current connection status */
|
|
2016
2020
|
get status(): ConnectionStatus;
|
|
2017
2021
|
/**
|
|
@@ -2051,10 +2055,23 @@ declare class RealtimeService extends ServiceModule {
|
|
|
2051
2055
|
private dispatchPresence;
|
|
2052
2056
|
private sendWs;
|
|
2053
2057
|
private scheduleReconnect;
|
|
2058
|
+
/** Drop the current socket without triggering its onclose reconnect path. */
|
|
2059
|
+
private teardownSocket;
|
|
2060
|
+
/** Tear down a dead-but-open socket and reconnect. */
|
|
2061
|
+
private forceReconnect;
|
|
2062
|
+
/**
|
|
2063
|
+
* Browsers suspend timers and silently kill sockets during sleep or network
|
|
2064
|
+
* changes. When the page wakes (tab visible, window focused, network back),
|
|
2065
|
+
* verify the connection instead of waiting for the next heartbeat tick.
|
|
2066
|
+
*/
|
|
2067
|
+
private installWakeListeners;
|
|
2068
|
+
private removeWakeListeners;
|
|
2069
|
+
private handleWake;
|
|
2054
2070
|
private getReconnectDelay;
|
|
2055
2071
|
private startHeartbeat;
|
|
2056
2072
|
private clearHeartbeat;
|
|
2057
2073
|
private clearTimers;
|
|
2074
|
+
private clearConnectWatchdog;
|
|
2058
2075
|
private setStatus;
|
|
2059
2076
|
}
|
|
2060
2077
|
|
package/dist/index.js
CHANGED
|
@@ -4152,6 +4152,9 @@ function sleep3(ms) {
|
|
|
4152
4152
|
var DEFAULT_RECONNECT_BASE_MS = 1e3;
|
|
4153
4153
|
var MAX_RECONNECT_MS = 3e4;
|
|
4154
4154
|
var HEARTBEAT_INTERVAL_MS = 3e4;
|
|
4155
|
+
var TICKET_FETCH_TIMEOUT_MS = 1e4;
|
|
4156
|
+
var CONNECT_WATCHDOG_MS = 2e4;
|
|
4157
|
+
var LIVENESS_TIMEOUT_MS = 75e3;
|
|
4155
4158
|
var RealtimeService = class extends ServiceModule {
|
|
4156
4159
|
constructor() {
|
|
4157
4160
|
super(...arguments);
|
|
@@ -4165,7 +4168,11 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4165
4168
|
this.reconnectAttempt = 0;
|
|
4166
4169
|
this.reconnectTimer = null;
|
|
4167
4170
|
this.heartbeatTimer = null;
|
|
4171
|
+
this.connectWatchdogTimer = null;
|
|
4168
4172
|
this.authenticated = false;
|
|
4173
|
+
this.lastInboundAt = 0;
|
|
4174
|
+
this.wakeHandler = null;
|
|
4175
|
+
this.visibilityHandler = null;
|
|
4169
4176
|
}
|
|
4170
4177
|
/** Current connection status */
|
|
4171
4178
|
get status() {
|
|
@@ -4269,6 +4276,7 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4269
4276
|
/** Disconnect and clean up all subscriptions. */
|
|
4270
4277
|
disconnect() {
|
|
4271
4278
|
this.clearTimers();
|
|
4279
|
+
this.removeWakeListeners();
|
|
4272
4280
|
this.subscriptions.clear();
|
|
4273
4281
|
this.presenceCallbacks.clear();
|
|
4274
4282
|
this.statusCallbacks.clear();
|
|
@@ -4287,6 +4295,15 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4287
4295
|
connect() {
|
|
4288
4296
|
if (this._status === "connecting" || this._status === "connected") return;
|
|
4289
4297
|
this.setStatus(this.reconnectAttempt > 0 ? "reconnecting" : "connecting");
|
|
4298
|
+
this.installWakeListeners();
|
|
4299
|
+
this.clearConnectWatchdog();
|
|
4300
|
+
this.connectWatchdogTimer = setTimeout(() => {
|
|
4301
|
+
this.connectWatchdogTimer = null;
|
|
4302
|
+
if (this._status !== "connected") {
|
|
4303
|
+
this.teardownSocket();
|
|
4304
|
+
this.scheduleReconnect();
|
|
4305
|
+
}
|
|
4306
|
+
}, CONNECT_WATCHDOG_MS);
|
|
4290
4307
|
this.fetchTicketAndConnect();
|
|
4291
4308
|
}
|
|
4292
4309
|
async fetchTicketAndConnect() {
|
|
@@ -4297,10 +4314,18 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4297
4314
|
if (apiKey) headers["x-api-key"] = apiKey;
|
|
4298
4315
|
const token = this.client.getSessionToken();
|
|
4299
4316
|
if (token) headers["Authorization"] = `Bearer ${token}`;
|
|
4300
|
-
const
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4317
|
+
const abort = new AbortController();
|
|
4318
|
+
const abortTimer = setTimeout(() => abort.abort(), TICKET_FETCH_TIMEOUT_MS);
|
|
4319
|
+
let ticketRes;
|
|
4320
|
+
try {
|
|
4321
|
+
ticketRes = await fetch(`${baseUrl}/v1/realtime/ws/ticket`, {
|
|
4322
|
+
method: "POST",
|
|
4323
|
+
headers,
|
|
4324
|
+
signal: abort.signal
|
|
4325
|
+
});
|
|
4326
|
+
} finally {
|
|
4327
|
+
clearTimeout(abortTimer);
|
|
4328
|
+
}
|
|
4304
4329
|
let wsUrl;
|
|
4305
4330
|
if (ticketRes.ok) {
|
|
4306
4331
|
const ticketData = await ticketRes.json();
|
|
@@ -4318,6 +4343,7 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4318
4343
|
}
|
|
4319
4344
|
this.ws.onopen = () => {
|
|
4320
4345
|
this.reconnectAttempt = 0;
|
|
4346
|
+
this.lastInboundAt = Date.now();
|
|
4321
4347
|
if (!this.usedTicketAuth) {
|
|
4322
4348
|
this.authenticate();
|
|
4323
4349
|
}
|
|
@@ -4335,6 +4361,7 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4335
4361
|
}
|
|
4336
4362
|
};
|
|
4337
4363
|
this.ws.onmessage = (event) => {
|
|
4364
|
+
this.lastInboundAt = Date.now();
|
|
4338
4365
|
try {
|
|
4339
4366
|
const msg = JSON.parse(event.data);
|
|
4340
4367
|
this.handleMessage(msg);
|
|
@@ -4375,6 +4402,10 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4375
4402
|
break;
|
|
4376
4403
|
case "error":
|
|
4377
4404
|
break;
|
|
4405
|
+
case "reconnect":
|
|
4406
|
+
this.reconnectAttempt = 0;
|
|
4407
|
+
this.forceReconnect();
|
|
4408
|
+
break;
|
|
4378
4409
|
case "presence_state":
|
|
4379
4410
|
this.dispatchPresence({
|
|
4380
4411
|
type: "state",
|
|
@@ -4429,6 +4460,7 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4429
4460
|
// Private: Reconnection
|
|
4430
4461
|
// --------------------------------------------------------------------------
|
|
4431
4462
|
scheduleReconnect() {
|
|
4463
|
+
this.clearConnectWatchdog();
|
|
4432
4464
|
if (this.subscriptions.size === 0 && this.presenceCallbacks.size === 0) {
|
|
4433
4465
|
this.setStatus("disconnected");
|
|
4434
4466
|
return;
|
|
@@ -4436,10 +4468,83 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4436
4468
|
this.setStatus("reconnecting");
|
|
4437
4469
|
const delay = this.getReconnectDelay();
|
|
4438
4470
|
this.reconnectTimer = setTimeout(() => {
|
|
4471
|
+
this.reconnectTimer = null;
|
|
4439
4472
|
this.reconnectAttempt++;
|
|
4440
4473
|
this.connect();
|
|
4441
4474
|
}, delay);
|
|
4442
4475
|
}
|
|
4476
|
+
/** Drop the current socket without triggering its onclose reconnect path. */
|
|
4477
|
+
teardownSocket() {
|
|
4478
|
+
if (this.ws) {
|
|
4479
|
+
this.ws.onclose = null;
|
|
4480
|
+
this.ws.onerror = null;
|
|
4481
|
+
try {
|
|
4482
|
+
this.ws.close();
|
|
4483
|
+
} catch {
|
|
4484
|
+
}
|
|
4485
|
+
this.ws = null;
|
|
4486
|
+
}
|
|
4487
|
+
this.authenticated = false;
|
|
4488
|
+
this.clearHeartbeat();
|
|
4489
|
+
}
|
|
4490
|
+
/** Tear down a dead-but-open socket and reconnect. */
|
|
4491
|
+
forceReconnect() {
|
|
4492
|
+
this.teardownSocket();
|
|
4493
|
+
this.scheduleReconnect();
|
|
4494
|
+
}
|
|
4495
|
+
// --------------------------------------------------------------------------
|
|
4496
|
+
// Private: Wake / network-change recovery
|
|
4497
|
+
// --------------------------------------------------------------------------
|
|
4498
|
+
/**
|
|
4499
|
+
* Browsers suspend timers and silently kill sockets during sleep or network
|
|
4500
|
+
* changes. When the page wakes (tab visible, window focused, network back),
|
|
4501
|
+
* verify the connection instead of waiting for the next heartbeat tick.
|
|
4502
|
+
*/
|
|
4503
|
+
installWakeListeners() {
|
|
4504
|
+
if (this.wakeHandler || typeof window === "undefined") return;
|
|
4505
|
+
this.wakeHandler = () => this.handleWake();
|
|
4506
|
+
window.addEventListener("online", this.wakeHandler);
|
|
4507
|
+
window.addEventListener("focus", this.wakeHandler);
|
|
4508
|
+
if (typeof document !== "undefined") {
|
|
4509
|
+
this.visibilityHandler = () => {
|
|
4510
|
+
if (document.visibilityState === "visible") this.handleWake();
|
|
4511
|
+
};
|
|
4512
|
+
document.addEventListener("visibilitychange", this.visibilityHandler);
|
|
4513
|
+
}
|
|
4514
|
+
}
|
|
4515
|
+
removeWakeListeners() {
|
|
4516
|
+
if (typeof window !== "undefined" && this.wakeHandler) {
|
|
4517
|
+
window.removeEventListener("online", this.wakeHandler);
|
|
4518
|
+
window.removeEventListener("focus", this.wakeHandler);
|
|
4519
|
+
}
|
|
4520
|
+
if (typeof document !== "undefined" && this.visibilityHandler) {
|
|
4521
|
+
document.removeEventListener("visibilitychange", this.visibilityHandler);
|
|
4522
|
+
}
|
|
4523
|
+
this.wakeHandler = null;
|
|
4524
|
+
this.visibilityHandler = null;
|
|
4525
|
+
}
|
|
4526
|
+
handleWake() {
|
|
4527
|
+
if (this.subscriptions.size === 0 && this.presenceCallbacks.size === 0) return;
|
|
4528
|
+
if (this._status === "connected") {
|
|
4529
|
+
if (Date.now() - this.lastInboundAt > LIVENESS_TIMEOUT_MS) {
|
|
4530
|
+
this.reconnectAttempt = 0;
|
|
4531
|
+
this.forceReconnect();
|
|
4532
|
+
} else if (this.ws?.readyState === WebSocket.OPEN) {
|
|
4533
|
+
this.ws.send("ping");
|
|
4534
|
+
}
|
|
4535
|
+
return;
|
|
4536
|
+
}
|
|
4537
|
+
if (this._status === "reconnecting" && this.reconnectTimer) {
|
|
4538
|
+
clearTimeout(this.reconnectTimer);
|
|
4539
|
+
this.reconnectTimer = null;
|
|
4540
|
+
this.reconnectAttempt = 0;
|
|
4541
|
+
this.connect();
|
|
4542
|
+
return;
|
|
4543
|
+
}
|
|
4544
|
+
if (this._status === "disconnected") {
|
|
4545
|
+
this.connect();
|
|
4546
|
+
}
|
|
4547
|
+
}
|
|
4443
4548
|
getReconnectDelay() {
|
|
4444
4549
|
const exponential = DEFAULT_RECONNECT_BASE_MS * Math.pow(2, this.reconnectAttempt);
|
|
4445
4550
|
const jitter = Math.random() * 0.3 * exponential;
|
|
@@ -4451,6 +4556,10 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4451
4556
|
startHeartbeat() {
|
|
4452
4557
|
this.clearHeartbeat();
|
|
4453
4558
|
this.heartbeatTimer = setInterval(() => {
|
|
4559
|
+
if (Date.now() - this.lastInboundAt > LIVENESS_TIMEOUT_MS) {
|
|
4560
|
+
this.forceReconnect();
|
|
4561
|
+
return;
|
|
4562
|
+
}
|
|
4454
4563
|
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
4455
4564
|
this.ws.send("ping");
|
|
4456
4565
|
}
|
|
@@ -4464,17 +4573,27 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4464
4573
|
}
|
|
4465
4574
|
clearTimers() {
|
|
4466
4575
|
this.clearHeartbeat();
|
|
4576
|
+
this.clearConnectWatchdog();
|
|
4467
4577
|
if (this.reconnectTimer) {
|
|
4468
4578
|
clearTimeout(this.reconnectTimer);
|
|
4469
4579
|
this.reconnectTimer = null;
|
|
4470
4580
|
}
|
|
4471
4581
|
}
|
|
4582
|
+
clearConnectWatchdog() {
|
|
4583
|
+
if (this.connectWatchdogTimer) {
|
|
4584
|
+
clearTimeout(this.connectWatchdogTimer);
|
|
4585
|
+
this.connectWatchdogTimer = null;
|
|
4586
|
+
}
|
|
4587
|
+
}
|
|
4472
4588
|
// --------------------------------------------------------------------------
|
|
4473
4589
|
// Private: Status
|
|
4474
4590
|
// --------------------------------------------------------------------------
|
|
4475
4591
|
setStatus(status) {
|
|
4476
4592
|
if (this._status === status) return;
|
|
4477
4593
|
this._status = status;
|
|
4594
|
+
if (status === "connected") {
|
|
4595
|
+
this.clearConnectWatchdog();
|
|
4596
|
+
}
|
|
4478
4597
|
for (const cb of this.statusCallbacks) {
|
|
4479
4598
|
try {
|
|
4480
4599
|
cb(status);
|
package/dist/index.mjs
CHANGED
|
@@ -4048,6 +4048,9 @@ function sleep3(ms) {
|
|
|
4048
4048
|
var DEFAULT_RECONNECT_BASE_MS = 1e3;
|
|
4049
4049
|
var MAX_RECONNECT_MS = 3e4;
|
|
4050
4050
|
var HEARTBEAT_INTERVAL_MS = 3e4;
|
|
4051
|
+
var TICKET_FETCH_TIMEOUT_MS = 1e4;
|
|
4052
|
+
var CONNECT_WATCHDOG_MS = 2e4;
|
|
4053
|
+
var LIVENESS_TIMEOUT_MS = 75e3;
|
|
4051
4054
|
var RealtimeService = class extends ServiceModule {
|
|
4052
4055
|
constructor() {
|
|
4053
4056
|
super(...arguments);
|
|
@@ -4061,7 +4064,11 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4061
4064
|
this.reconnectAttempt = 0;
|
|
4062
4065
|
this.reconnectTimer = null;
|
|
4063
4066
|
this.heartbeatTimer = null;
|
|
4067
|
+
this.connectWatchdogTimer = null;
|
|
4064
4068
|
this.authenticated = false;
|
|
4069
|
+
this.lastInboundAt = 0;
|
|
4070
|
+
this.wakeHandler = null;
|
|
4071
|
+
this.visibilityHandler = null;
|
|
4065
4072
|
}
|
|
4066
4073
|
/** Current connection status */
|
|
4067
4074
|
get status() {
|
|
@@ -4165,6 +4172,7 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4165
4172
|
/** Disconnect and clean up all subscriptions. */
|
|
4166
4173
|
disconnect() {
|
|
4167
4174
|
this.clearTimers();
|
|
4175
|
+
this.removeWakeListeners();
|
|
4168
4176
|
this.subscriptions.clear();
|
|
4169
4177
|
this.presenceCallbacks.clear();
|
|
4170
4178
|
this.statusCallbacks.clear();
|
|
@@ -4183,6 +4191,15 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4183
4191
|
connect() {
|
|
4184
4192
|
if (this._status === "connecting" || this._status === "connected") return;
|
|
4185
4193
|
this.setStatus(this.reconnectAttempt > 0 ? "reconnecting" : "connecting");
|
|
4194
|
+
this.installWakeListeners();
|
|
4195
|
+
this.clearConnectWatchdog();
|
|
4196
|
+
this.connectWatchdogTimer = setTimeout(() => {
|
|
4197
|
+
this.connectWatchdogTimer = null;
|
|
4198
|
+
if (this._status !== "connected") {
|
|
4199
|
+
this.teardownSocket();
|
|
4200
|
+
this.scheduleReconnect();
|
|
4201
|
+
}
|
|
4202
|
+
}, CONNECT_WATCHDOG_MS);
|
|
4186
4203
|
this.fetchTicketAndConnect();
|
|
4187
4204
|
}
|
|
4188
4205
|
async fetchTicketAndConnect() {
|
|
@@ -4193,10 +4210,18 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4193
4210
|
if (apiKey) headers["x-api-key"] = apiKey;
|
|
4194
4211
|
const token = this.client.getSessionToken();
|
|
4195
4212
|
if (token) headers["Authorization"] = `Bearer ${token}`;
|
|
4196
|
-
const
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4213
|
+
const abort = new AbortController();
|
|
4214
|
+
const abortTimer = setTimeout(() => abort.abort(), TICKET_FETCH_TIMEOUT_MS);
|
|
4215
|
+
let ticketRes;
|
|
4216
|
+
try {
|
|
4217
|
+
ticketRes = await fetch(`${baseUrl}/v1/realtime/ws/ticket`, {
|
|
4218
|
+
method: "POST",
|
|
4219
|
+
headers,
|
|
4220
|
+
signal: abort.signal
|
|
4221
|
+
});
|
|
4222
|
+
} finally {
|
|
4223
|
+
clearTimeout(abortTimer);
|
|
4224
|
+
}
|
|
4200
4225
|
let wsUrl;
|
|
4201
4226
|
if (ticketRes.ok) {
|
|
4202
4227
|
const ticketData = await ticketRes.json();
|
|
@@ -4214,6 +4239,7 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4214
4239
|
}
|
|
4215
4240
|
this.ws.onopen = () => {
|
|
4216
4241
|
this.reconnectAttempt = 0;
|
|
4242
|
+
this.lastInboundAt = Date.now();
|
|
4217
4243
|
if (!this.usedTicketAuth) {
|
|
4218
4244
|
this.authenticate();
|
|
4219
4245
|
}
|
|
@@ -4231,6 +4257,7 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4231
4257
|
}
|
|
4232
4258
|
};
|
|
4233
4259
|
this.ws.onmessage = (event) => {
|
|
4260
|
+
this.lastInboundAt = Date.now();
|
|
4234
4261
|
try {
|
|
4235
4262
|
const msg = JSON.parse(event.data);
|
|
4236
4263
|
this.handleMessage(msg);
|
|
@@ -4271,6 +4298,10 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4271
4298
|
break;
|
|
4272
4299
|
case "error":
|
|
4273
4300
|
break;
|
|
4301
|
+
case "reconnect":
|
|
4302
|
+
this.reconnectAttempt = 0;
|
|
4303
|
+
this.forceReconnect();
|
|
4304
|
+
break;
|
|
4274
4305
|
case "presence_state":
|
|
4275
4306
|
this.dispatchPresence({
|
|
4276
4307
|
type: "state",
|
|
@@ -4325,6 +4356,7 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4325
4356
|
// Private: Reconnection
|
|
4326
4357
|
// --------------------------------------------------------------------------
|
|
4327
4358
|
scheduleReconnect() {
|
|
4359
|
+
this.clearConnectWatchdog();
|
|
4328
4360
|
if (this.subscriptions.size === 0 && this.presenceCallbacks.size === 0) {
|
|
4329
4361
|
this.setStatus("disconnected");
|
|
4330
4362
|
return;
|
|
@@ -4332,10 +4364,83 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4332
4364
|
this.setStatus("reconnecting");
|
|
4333
4365
|
const delay = this.getReconnectDelay();
|
|
4334
4366
|
this.reconnectTimer = setTimeout(() => {
|
|
4367
|
+
this.reconnectTimer = null;
|
|
4335
4368
|
this.reconnectAttempt++;
|
|
4336
4369
|
this.connect();
|
|
4337
4370
|
}, delay);
|
|
4338
4371
|
}
|
|
4372
|
+
/** Drop the current socket without triggering its onclose reconnect path. */
|
|
4373
|
+
teardownSocket() {
|
|
4374
|
+
if (this.ws) {
|
|
4375
|
+
this.ws.onclose = null;
|
|
4376
|
+
this.ws.onerror = null;
|
|
4377
|
+
try {
|
|
4378
|
+
this.ws.close();
|
|
4379
|
+
} catch {
|
|
4380
|
+
}
|
|
4381
|
+
this.ws = null;
|
|
4382
|
+
}
|
|
4383
|
+
this.authenticated = false;
|
|
4384
|
+
this.clearHeartbeat();
|
|
4385
|
+
}
|
|
4386
|
+
/** Tear down a dead-but-open socket and reconnect. */
|
|
4387
|
+
forceReconnect() {
|
|
4388
|
+
this.teardownSocket();
|
|
4389
|
+
this.scheduleReconnect();
|
|
4390
|
+
}
|
|
4391
|
+
// --------------------------------------------------------------------------
|
|
4392
|
+
// Private: Wake / network-change recovery
|
|
4393
|
+
// --------------------------------------------------------------------------
|
|
4394
|
+
/**
|
|
4395
|
+
* Browsers suspend timers and silently kill sockets during sleep or network
|
|
4396
|
+
* changes. When the page wakes (tab visible, window focused, network back),
|
|
4397
|
+
* verify the connection instead of waiting for the next heartbeat tick.
|
|
4398
|
+
*/
|
|
4399
|
+
installWakeListeners() {
|
|
4400
|
+
if (this.wakeHandler || typeof window === "undefined") return;
|
|
4401
|
+
this.wakeHandler = () => this.handleWake();
|
|
4402
|
+
window.addEventListener("online", this.wakeHandler);
|
|
4403
|
+
window.addEventListener("focus", this.wakeHandler);
|
|
4404
|
+
if (typeof document !== "undefined") {
|
|
4405
|
+
this.visibilityHandler = () => {
|
|
4406
|
+
if (document.visibilityState === "visible") this.handleWake();
|
|
4407
|
+
};
|
|
4408
|
+
document.addEventListener("visibilitychange", this.visibilityHandler);
|
|
4409
|
+
}
|
|
4410
|
+
}
|
|
4411
|
+
removeWakeListeners() {
|
|
4412
|
+
if (typeof window !== "undefined" && this.wakeHandler) {
|
|
4413
|
+
window.removeEventListener("online", this.wakeHandler);
|
|
4414
|
+
window.removeEventListener("focus", this.wakeHandler);
|
|
4415
|
+
}
|
|
4416
|
+
if (typeof document !== "undefined" && this.visibilityHandler) {
|
|
4417
|
+
document.removeEventListener("visibilitychange", this.visibilityHandler);
|
|
4418
|
+
}
|
|
4419
|
+
this.wakeHandler = null;
|
|
4420
|
+
this.visibilityHandler = null;
|
|
4421
|
+
}
|
|
4422
|
+
handleWake() {
|
|
4423
|
+
if (this.subscriptions.size === 0 && this.presenceCallbacks.size === 0) return;
|
|
4424
|
+
if (this._status === "connected") {
|
|
4425
|
+
if (Date.now() - this.lastInboundAt > LIVENESS_TIMEOUT_MS) {
|
|
4426
|
+
this.reconnectAttempt = 0;
|
|
4427
|
+
this.forceReconnect();
|
|
4428
|
+
} else if (this.ws?.readyState === WebSocket.OPEN) {
|
|
4429
|
+
this.ws.send("ping");
|
|
4430
|
+
}
|
|
4431
|
+
return;
|
|
4432
|
+
}
|
|
4433
|
+
if (this._status === "reconnecting" && this.reconnectTimer) {
|
|
4434
|
+
clearTimeout(this.reconnectTimer);
|
|
4435
|
+
this.reconnectTimer = null;
|
|
4436
|
+
this.reconnectAttempt = 0;
|
|
4437
|
+
this.connect();
|
|
4438
|
+
return;
|
|
4439
|
+
}
|
|
4440
|
+
if (this._status === "disconnected") {
|
|
4441
|
+
this.connect();
|
|
4442
|
+
}
|
|
4443
|
+
}
|
|
4339
4444
|
getReconnectDelay() {
|
|
4340
4445
|
const exponential = DEFAULT_RECONNECT_BASE_MS * Math.pow(2, this.reconnectAttempt);
|
|
4341
4446
|
const jitter = Math.random() * 0.3 * exponential;
|
|
@@ -4347,6 +4452,10 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4347
4452
|
startHeartbeat() {
|
|
4348
4453
|
this.clearHeartbeat();
|
|
4349
4454
|
this.heartbeatTimer = setInterval(() => {
|
|
4455
|
+
if (Date.now() - this.lastInboundAt > LIVENESS_TIMEOUT_MS) {
|
|
4456
|
+
this.forceReconnect();
|
|
4457
|
+
return;
|
|
4458
|
+
}
|
|
4350
4459
|
if (this.ws?.readyState === WebSocket.OPEN) {
|
|
4351
4460
|
this.ws.send("ping");
|
|
4352
4461
|
}
|
|
@@ -4360,17 +4469,27 @@ var RealtimeService = class extends ServiceModule {
|
|
|
4360
4469
|
}
|
|
4361
4470
|
clearTimers() {
|
|
4362
4471
|
this.clearHeartbeat();
|
|
4472
|
+
this.clearConnectWatchdog();
|
|
4363
4473
|
if (this.reconnectTimer) {
|
|
4364
4474
|
clearTimeout(this.reconnectTimer);
|
|
4365
4475
|
this.reconnectTimer = null;
|
|
4366
4476
|
}
|
|
4367
4477
|
}
|
|
4478
|
+
clearConnectWatchdog() {
|
|
4479
|
+
if (this.connectWatchdogTimer) {
|
|
4480
|
+
clearTimeout(this.connectWatchdogTimer);
|
|
4481
|
+
this.connectWatchdogTimer = null;
|
|
4482
|
+
}
|
|
4483
|
+
}
|
|
4368
4484
|
// --------------------------------------------------------------------------
|
|
4369
4485
|
// Private: Status
|
|
4370
4486
|
// --------------------------------------------------------------------------
|
|
4371
4487
|
setStatus(status) {
|
|
4372
4488
|
if (this._status === status) return;
|
|
4373
4489
|
this._status = status;
|
|
4490
|
+
if (status === "connected") {
|
|
4491
|
+
this.clearConnectWatchdog();
|
|
4492
|
+
}
|
|
4374
4493
|
for (const cb of this.statusCallbacks) {
|
|
4375
4494
|
try {
|
|
4376
4495
|
cb(status);
|