@arca-network/sdk 1.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 +91 -0
- package/README.md +65 -0
- package/dist/admin.d.ts +159 -0
- package/dist/admin.d.ts.map +1 -0
- package/dist/admin.js +399 -0
- package/dist/admin.js.map +1 -0
- package/dist/arca.d.ts +1343 -0
- package/dist/arca.d.ts.map +1 -0
- package/dist/arca.js +3454 -0
- package/dist/arca.js.map +1 -0
- package/dist/cache.d.ts +29 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +74 -0
- package/dist/cache.js.map +1 -0
- package/dist/candle-cdn.d.ts +17 -0
- package/dist/candle-cdn.d.ts.map +1 -0
- package/dist/candle-cdn.js +138 -0
- package/dist/candle-cdn.js.map +1 -0
- package/dist/client.d.ts +98 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +225 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +153 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +243 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +2 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +6 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +71 -0
- package/dist/index.js.map +1 -0
- package/dist/ladder.d.ts +40 -0
- package/dist/ladder.d.ts.map +1 -0
- package/dist/ladder.js +145 -0
- package/dist/ladder.js.map +1 -0
- package/dist/ladder.test.d.ts +2 -0
- package/dist/ladder.test.d.ts.map +1 -0
- package/dist/ladder.test.js +112 -0
- package/dist/ladder.test.js.map +1 -0
- package/dist/operation-handle.d.ts +88 -0
- package/dist/operation-handle.d.ts.map +1 -0
- package/dist/operation-handle.js +262 -0
- package/dist/operation-handle.js.map +1 -0
- package/dist/recovery.d.ts +20 -0
- package/dist/recovery.d.ts.map +1 -0
- package/dist/recovery.js +91 -0
- package/dist/recovery.js.map +1 -0
- package/dist/revalue.d.ts +22 -0
- package/dist/revalue.d.ts.map +1 -0
- package/dist/revalue.js +153 -0
- package/dist/revalue.js.map +1 -0
- package/dist/streams.d.ts +661 -0
- package/dist/streams.d.ts.map +1 -0
- package/dist/streams.js +2272 -0
- package/dist/streams.js.map +1 -0
- package/dist/types.d.ts +2814 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +118 -0
- package/dist/types.js.map +1 -0
- package/dist/websocket.d.ts +240 -0
- package/dist/websocket.d.ts.map +1 -0
- package/dist/websocket.js +1288 -0
- package/dist/websocket.js.map +1 -0
- package/package.json +48 -0
|
@@ -0,0 +1,1288 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebSocketManager = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
function sameCoinSet(a, b) {
|
|
6
|
+
if (a.length !== b.length)
|
|
7
|
+
return false;
|
|
8
|
+
const sa = [...a].sort();
|
|
9
|
+
const sb = [...b].sort();
|
|
10
|
+
for (let i = 0; i < sa.length; i++) {
|
|
11
|
+
if (sa[i] !== sb[i])
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
function getWS() {
|
|
17
|
+
if (typeof globalThis.WebSocket !== 'undefined') {
|
|
18
|
+
return globalThis.WebSocket;
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
22
|
+
const wsModule = 'ws';
|
|
23
|
+
return require(wsModule);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
throw new Error('WebSocket is not available. If running Node.js < 22, install the "ws" package: npm install ws');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
class WebSocketManager {
|
|
30
|
+
config;
|
|
31
|
+
ws = null;
|
|
32
|
+
reconnectTimer = null;
|
|
33
|
+
reconnectAttempt = 0;
|
|
34
|
+
maxReconnectDelay;
|
|
35
|
+
maxReconnectAttempts;
|
|
36
|
+
callbacks = new Map();
|
|
37
|
+
statusCallbacks = new Set();
|
|
38
|
+
errorCallbacks = new Set();
|
|
39
|
+
lastStatus = 'disconnected';
|
|
40
|
+
subscribedMids = null;
|
|
41
|
+
subscribedCandles = null;
|
|
42
|
+
subscribedTrades = null;
|
|
43
|
+
shouldReconnect = false;
|
|
44
|
+
// Ref-counted path watch engine
|
|
45
|
+
pathRefs = new Map();
|
|
46
|
+
snapshotCache = new Map();
|
|
47
|
+
pendingSnapshots = new Map(); // requestId → pending snapshot
|
|
48
|
+
// Mids subscription is reference-counted with optional per-market filtering.
|
|
49
|
+
// `midsAllRefs` counts subscribers that want ALL coins (no filter).
|
|
50
|
+
// `midsCoinRefs` counts subscribers per specific market.
|
|
51
|
+
// The wire subscription sends `coins: []` (all) when midsAllRefs > 0,
|
|
52
|
+
// otherwise the union of keys in midsCoinRefs.
|
|
53
|
+
midsAllRefs = 0;
|
|
54
|
+
midsCoinRefs = new Map();
|
|
55
|
+
midsExchange = 'sim';
|
|
56
|
+
candleRefs = new Map();
|
|
57
|
+
tradeRefs = new Map();
|
|
58
|
+
chartHistoryWatches = new Map();
|
|
59
|
+
unsubTimers = new Map();
|
|
60
|
+
idleTimer = null;
|
|
61
|
+
static UNSUB_DEBOUNCE_MS = 100;
|
|
62
|
+
static IDLE_DISCONNECT_MS = 60_000;
|
|
63
|
+
static PING_INTERVAL_MS = 30_000;
|
|
64
|
+
static STALE_THRESHOLD_MS = 45_000;
|
|
65
|
+
// Hidden duration shorter than this is treated as a quick alt-tab and
|
|
66
|
+
// does NOT trigger a resume signal. Tuned to skip noisy
|
|
67
|
+
// visibility flickers from focus changes, browser shortcuts, etc.
|
|
68
|
+
static RESUME_HIDDEN_THRESHOLD_MS = 5_000;
|
|
69
|
+
// After resuming visibility we ping and wait this long for any inbound
|
|
70
|
+
// message; absence is treated as a half-open TCP and forces a reconnect.
|
|
71
|
+
static RESUME_PING_TIMEOUT_MS = 2_000;
|
|
72
|
+
pingTimer = null;
|
|
73
|
+
lastMessageAt = Date.now();
|
|
74
|
+
// Monotonic counter incremented on every inbound message; used by the
|
|
75
|
+
// resume-probe to detect "got any reply" without relying on wall-clock
|
|
76
|
+
// resolution (which is ambiguous under fake timers in tests, and can be
|
|
77
|
+
// sub-millisecond in production).
|
|
78
|
+
messageCounter = 0;
|
|
79
|
+
lastDeliverySeq = 0;
|
|
80
|
+
gapCallbacks = new Set();
|
|
81
|
+
resumeCallbacks = new Set();
|
|
82
|
+
authenticatedCallbacks = new Set();
|
|
83
|
+
hiddenAt = null;
|
|
84
|
+
removeVisibilityListener = null;
|
|
85
|
+
resumePingTimer = null;
|
|
86
|
+
nextRequestId = 0;
|
|
87
|
+
pendingRequests = new Map();
|
|
88
|
+
constructor(config) {
|
|
89
|
+
this.config = config;
|
|
90
|
+
this.maxReconnectDelay = config.maxReconnectDelay ?? 30000;
|
|
91
|
+
this.maxReconnectAttempts = config.maxReconnectAttempts ?? Infinity;
|
|
92
|
+
this.installVisibilityListener();
|
|
93
|
+
}
|
|
94
|
+
// ---- Ref-counted path watch management ----
|
|
95
|
+
/**
|
|
96
|
+
* Watch a path. Increments the ref count for the path; sends a `watch`
|
|
97
|
+
* message on first interest. Returns a promise that resolves with the
|
|
98
|
+
* initial snapshot from the server.
|
|
99
|
+
*/
|
|
100
|
+
watchPath(path) {
|
|
101
|
+
this.cancelIdleTimer();
|
|
102
|
+
const prev = this.pathRefs.get(path) ?? 0;
|
|
103
|
+
this.pathRefs.set(path, prev + 1);
|
|
104
|
+
if (prev > 0) {
|
|
105
|
+
const cached = this.snapshotCache.get(path);
|
|
106
|
+
if (cached)
|
|
107
|
+
return cached;
|
|
108
|
+
}
|
|
109
|
+
const timerKey = `path:${path}`;
|
|
110
|
+
const pendingTimer = this.unsubTimers.get(timerKey);
|
|
111
|
+
if (pendingTimer) {
|
|
112
|
+
clearTimeout(pendingTimer);
|
|
113
|
+
this.unsubTimers.delete(timerKey);
|
|
114
|
+
const cached = this.snapshotCache.get(path);
|
|
115
|
+
if (cached)
|
|
116
|
+
return cached;
|
|
117
|
+
}
|
|
118
|
+
this.ensureConnected();
|
|
119
|
+
const promise = this.sendWatch(path);
|
|
120
|
+
this.snapshotCache.set(path, promise);
|
|
121
|
+
return promise;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Unwatch a path. Decrements the ref count; debounced unwatch when the
|
|
125
|
+
* last watcher leaves.
|
|
126
|
+
*/
|
|
127
|
+
unwatchPath(path) {
|
|
128
|
+
const current = this.pathRefs.get(path) ?? 0;
|
|
129
|
+
if (current <= 1) {
|
|
130
|
+
this.pathRefs.delete(path);
|
|
131
|
+
const timerKey = `path:${path}`;
|
|
132
|
+
this.unsubTimers.set(timerKey, setTimeout(() => {
|
|
133
|
+
this.unsubTimers.delete(timerKey);
|
|
134
|
+
if (!this.pathRefs.has(path)) {
|
|
135
|
+
this.snapshotCache.delete(path);
|
|
136
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
137
|
+
this.ws.send(JSON.stringify({ action: 'unwatch', path }));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
this.maybeStartIdleTimer();
|
|
141
|
+
}, WebSocketManager.UNSUB_DEBOUNCE_MS));
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
this.pathRefs.set(path, current - 1);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
sendWatch(path) {
|
|
148
|
+
const requestId = `watch-${++this.nextRequestId}`;
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
const timeout = setTimeout(() => {
|
|
151
|
+
this.pendingSnapshots.delete(requestId);
|
|
152
|
+
reject(new Error(`watchPath: timeout waiting for snapshot for ${path}`));
|
|
153
|
+
}, 15_000);
|
|
154
|
+
this.pendingSnapshots.set(requestId, { resolve, reject, timeout });
|
|
155
|
+
const doSend = () => {
|
|
156
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
157
|
+
this.ws.send(JSON.stringify({ action: 'watch', path, requestId }));
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
if (this.lastStatus === 'connected') {
|
|
161
|
+
doSend();
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
const unsub = this.onStatus((status) => {
|
|
165
|
+
if (status === 'connected') {
|
|
166
|
+
unsub();
|
|
167
|
+
doSend();
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Acquire interest in mid price updates.
|
|
175
|
+
*
|
|
176
|
+
* @param exchange - Exchange identifier (e.g. `"sim"`)
|
|
177
|
+
* @param coins - Optional canonical market IDs to narrow the subscription.
|
|
178
|
+
* If omitted or empty, subscribes to all coins on the exchange.
|
|
179
|
+
* Multiple callers with different filters are merged into the union.
|
|
180
|
+
*/
|
|
181
|
+
acquireMids(exchange, coins) {
|
|
182
|
+
this.cancelIdleTimer();
|
|
183
|
+
this.midsExchange = exchange;
|
|
184
|
+
if (!coins || coins.length === 0) {
|
|
185
|
+
this.midsAllRefs++;
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
for (const c of coins) {
|
|
189
|
+
this.midsCoinRefs.set(c, (this.midsCoinRefs.get(c) ?? 0) + 1);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const timerKey = 'mids';
|
|
193
|
+
const pendingTimer = this.unsubTimers.get(timerKey);
|
|
194
|
+
if (pendingTimer) {
|
|
195
|
+
clearTimeout(pendingTimer);
|
|
196
|
+
this.unsubTimers.delete(timerKey);
|
|
197
|
+
}
|
|
198
|
+
this.ensureConnected();
|
|
199
|
+
this.syncMidsSubscription();
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Release interest in mid price updates. `coins` must match what was
|
|
203
|
+
* passed to `acquireMids` (or undefined/empty for the "all" case).
|
|
204
|
+
*/
|
|
205
|
+
releaseMids(coins) {
|
|
206
|
+
if (!coins || coins.length === 0) {
|
|
207
|
+
this.midsAllRefs = Math.max(0, this.midsAllRefs - 1);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
for (const c of coins) {
|
|
211
|
+
const count = this.midsCoinRefs.get(c) ?? 0;
|
|
212
|
+
if (count <= 1) {
|
|
213
|
+
this.midsCoinRefs.delete(c);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
this.midsCoinRefs.set(c, count - 1);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const timerKey = 'mids';
|
|
221
|
+
this.unsubTimers.set(timerKey, setTimeout(() => {
|
|
222
|
+
this.unsubTimers.delete(timerKey);
|
|
223
|
+
this.syncMidsSubscription();
|
|
224
|
+
this.maybeStartIdleTimer();
|
|
225
|
+
}, WebSocketManager.UNSUB_DEBOUNCE_MS));
|
|
226
|
+
}
|
|
227
|
+
midsSubscriptionCoins() {
|
|
228
|
+
if (this.midsAllRefs > 0)
|
|
229
|
+
return [];
|
|
230
|
+
if (this.midsCoinRefs.size > 0)
|
|
231
|
+
return Array.from(this.midsCoinRefs.keys()).sort();
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
syncMidsSubscription() {
|
|
235
|
+
const desired = this.midsSubscriptionCoins();
|
|
236
|
+
if (desired === null) {
|
|
237
|
+
if (this.subscribedMids)
|
|
238
|
+
this.unsubscribeMids();
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const prev = this.subscribedMids;
|
|
242
|
+
if (prev && prev.exchange === this.midsExchange && sameCoinSet(prev.coins, desired)) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
this.subscribeMids(this.midsExchange, desired);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Acquire interest in candle updates for specific coins and intervals.
|
|
249
|
+
*/
|
|
250
|
+
acquireCandles(coins, intervals) {
|
|
251
|
+
this.cancelIdleTimer();
|
|
252
|
+
for (const market of coins) {
|
|
253
|
+
if (!this.candleRefs.has(market)) {
|
|
254
|
+
this.candleRefs.set(market, new Set());
|
|
255
|
+
}
|
|
256
|
+
for (const iv of intervals) {
|
|
257
|
+
this.candleRefs.get(market).add(iv);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
this.ensureConnected();
|
|
261
|
+
this.syncCandleSubscription();
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Release interest in candle updates for specific coins and intervals.
|
|
265
|
+
*/
|
|
266
|
+
releaseCandles(coins, intervals) {
|
|
267
|
+
for (const market of coins) {
|
|
268
|
+
const ivs = this.candleRefs.get(market);
|
|
269
|
+
if (ivs) {
|
|
270
|
+
for (const iv of intervals)
|
|
271
|
+
ivs.delete(iv);
|
|
272
|
+
if (ivs.size === 0)
|
|
273
|
+
this.candleRefs.delete(market);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const timerKey = 'candles';
|
|
277
|
+
this.unsubTimers.set(timerKey, setTimeout(() => {
|
|
278
|
+
this.unsubTimers.delete(timerKey);
|
|
279
|
+
this.syncCandleSubscription();
|
|
280
|
+
this.maybeStartIdleTimer();
|
|
281
|
+
}, WebSocketManager.UNSUB_DEBOUNCE_MS));
|
|
282
|
+
}
|
|
283
|
+
syncCandleSubscription() {
|
|
284
|
+
if (this.candleRefs.size === 0) {
|
|
285
|
+
this.unsubscribeCandles();
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
const allCoins = Array.from(this.candleRefs.keys());
|
|
289
|
+
const allIntervals = new Set();
|
|
290
|
+
for (const ivs of this.candleRefs.values()) {
|
|
291
|
+
for (const iv of ivs)
|
|
292
|
+
allIntervals.add(iv);
|
|
293
|
+
}
|
|
294
|
+
this.subscribeCandles(allCoins, Array.from(allIntervals));
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Acquire interest in real-time market trade updates for specific coins.
|
|
298
|
+
* @param coins - Coins in canonical format (e.g. ["hl:0:BTC", "hl:0:ETH"])
|
|
299
|
+
*/
|
|
300
|
+
acquireTrades(coins) {
|
|
301
|
+
this.cancelIdleTimer();
|
|
302
|
+
for (const market of coins) {
|
|
303
|
+
this.tradeRefs.set(market, (this.tradeRefs.get(market) ?? 0) + 1);
|
|
304
|
+
}
|
|
305
|
+
this.ensureConnected();
|
|
306
|
+
this.syncTradeSubscription();
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Release interest in real-time market trade updates for specific coins.
|
|
310
|
+
*/
|
|
311
|
+
releaseTrades(coins) {
|
|
312
|
+
for (const market of coins) {
|
|
313
|
+
const count = this.tradeRefs.get(market) ?? 0;
|
|
314
|
+
if (count <= 1) {
|
|
315
|
+
this.tradeRefs.delete(market);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
this.tradeRefs.set(market, count - 1);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const timerKey = 'trades';
|
|
322
|
+
this.unsubTimers.set(timerKey, setTimeout(() => {
|
|
323
|
+
this.unsubTimers.delete(timerKey);
|
|
324
|
+
this.syncTradeSubscription();
|
|
325
|
+
this.maybeStartIdleTimer();
|
|
326
|
+
}, WebSocketManager.UNSUB_DEBOUNCE_MS));
|
|
327
|
+
}
|
|
328
|
+
syncTradeSubscription() {
|
|
329
|
+
if (this.tradeRefs.size === 0) {
|
|
330
|
+
this.unsubscribeTrades();
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
this.subscribeTrades(Array.from(this.tradeRefs.keys()));
|
|
334
|
+
}
|
|
335
|
+
hasAnyMidsInterest() {
|
|
336
|
+
return this.midsAllRefs > 0 || this.midsCoinRefs.size > 0;
|
|
337
|
+
}
|
|
338
|
+
hasAnyInterest() {
|
|
339
|
+
return this.pathRefs.size > 0 || this.hasAnyMidsInterest() || this.candleRefs.size > 0 || this.tradeRefs.size > 0;
|
|
340
|
+
}
|
|
341
|
+
maybeStartIdleTimer() {
|
|
342
|
+
if (this.hasAnyInterest())
|
|
343
|
+
return;
|
|
344
|
+
if (this.idleTimer)
|
|
345
|
+
return;
|
|
346
|
+
this.idleTimer = setTimeout(() => {
|
|
347
|
+
this.idleTimer = null;
|
|
348
|
+
if (!this.hasAnyInterest()) {
|
|
349
|
+
this.disconnect();
|
|
350
|
+
}
|
|
351
|
+
}, WebSocketManager.IDLE_DISCONNECT_MS);
|
|
352
|
+
}
|
|
353
|
+
cancelIdleTimer() {
|
|
354
|
+
if (this.idleTimer) {
|
|
355
|
+
clearTimeout(this.idleTimer);
|
|
356
|
+
this.idleTimer = null;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
/** Update the bearer token. Takes effect immediately: if the socket is
|
|
360
|
+
* disconnected and should reconnect, triggers an immediate reconnect. */
|
|
361
|
+
updateToken(token) {
|
|
362
|
+
this.config = { ...this.config, credential: token, credentialType: 'token' };
|
|
363
|
+
if (this.shouldReconnect && (!this.ws || this.ws.readyState === getWS().CLOSED)) {
|
|
364
|
+
if (this.reconnectTimer) {
|
|
365
|
+
clearTimeout(this.reconnectTimer);
|
|
366
|
+
this.reconnectTimer = null;
|
|
367
|
+
}
|
|
368
|
+
this.reconnectAttempt = 0;
|
|
369
|
+
this.doConnect();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
connect() {
|
|
373
|
+
this.shouldReconnect = true;
|
|
374
|
+
// Reinstall the visibilitychange listener after a prior `disconnect()`
|
|
375
|
+
// cleared it. `installVisibilityListener` is idempotent so the
|
|
376
|
+
// constructor + connect-after-construct path doesn't double-install.
|
|
377
|
+
// Without this, a `disconnect()` → `connect()` cycle would permanently
|
|
378
|
+
// lose the handler and `onResume` callbacks would stop firing on tab
|
|
379
|
+
// resume — Swift's `connect()` already does the equivalent via
|
|
380
|
+
// `installLifecycleObservers()`.
|
|
381
|
+
this.installVisibilityListener();
|
|
382
|
+
this.doConnect();
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Connect only if not already connected or connecting.
|
|
386
|
+
* Safe to call from multiple components without disrupting existing connections.
|
|
387
|
+
*/
|
|
388
|
+
ensureConnected() {
|
|
389
|
+
if (this.ws && (this.ws.readyState === getWS().OPEN || this.ws.readyState === getWS().CONNECTING)) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
this.connect();
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Returns a promise that resolves when the WebSocket reaches 'connected'
|
|
396
|
+
* (authenticated) status. Initiates the connection if not already started.
|
|
397
|
+
* Rejects after `timeoutMs` (default 15 s).
|
|
398
|
+
*/
|
|
399
|
+
waitForConnected(timeoutMs = 15_000) {
|
|
400
|
+
return new Promise((resolve, reject) => {
|
|
401
|
+
if (this.lastStatus === 'connected') {
|
|
402
|
+
resolve();
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const timer = setTimeout(() => {
|
|
406
|
+
unsub();
|
|
407
|
+
reject(new Error(`waitForConnected: timed out after ${timeoutMs}ms`));
|
|
408
|
+
}, timeoutMs);
|
|
409
|
+
const unsub = this.onStatus((status) => {
|
|
410
|
+
if (status === 'connected') {
|
|
411
|
+
clearTimeout(timer);
|
|
412
|
+
unsub();
|
|
413
|
+
resolve();
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
this.ensureConnected();
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
doConnect() {
|
|
420
|
+
if (this.ws) {
|
|
421
|
+
this.ws.close();
|
|
422
|
+
this.ws = null;
|
|
423
|
+
}
|
|
424
|
+
const httpUrl = this.config.baseUrl.replace(/\/api\/v1$/, '');
|
|
425
|
+
const wsUrl = httpUrl.replace(/^http/, 'ws') + '/api/v1/ws';
|
|
426
|
+
this.emitStatus('connecting');
|
|
427
|
+
const ws = new (getWS())(wsUrl);
|
|
428
|
+
this.ws = ws;
|
|
429
|
+
ws.onopen = () => {
|
|
430
|
+
this.reconnectAttempt = 0;
|
|
431
|
+
const sendAuth = (credential) => {
|
|
432
|
+
const authMsg = {
|
|
433
|
+
action: 'auth',
|
|
434
|
+
realmId: this.config.getRealmId(),
|
|
435
|
+
};
|
|
436
|
+
if (this.config.credentialType === 'apiKey') {
|
|
437
|
+
authMsg.apiKey = credential;
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
authMsg.token = credential;
|
|
441
|
+
}
|
|
442
|
+
ws.send(JSON.stringify(authMsg));
|
|
443
|
+
};
|
|
444
|
+
if (this.config.getToken) {
|
|
445
|
+
this.config.getToken().then(token => {
|
|
446
|
+
this.config = { ...this.config, credential: token };
|
|
447
|
+
sendAuth(token);
|
|
448
|
+
}).catch(() => {
|
|
449
|
+
sendAuth(this.config.credential);
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
sendAuth(this.config.credential);
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
ws.onmessage = (e) => {
|
|
457
|
+
try {
|
|
458
|
+
const msg = JSON.parse(e.data);
|
|
459
|
+
this.lastMessageAt = Date.now();
|
|
460
|
+
this.messageCounter++;
|
|
461
|
+
if (msg.type === 'pong')
|
|
462
|
+
return;
|
|
463
|
+
if (msg.type === 'authenticated') {
|
|
464
|
+
this.lastDeliverySeq = 0;
|
|
465
|
+
this.emitStatus('connected');
|
|
466
|
+
this.startHeartbeat();
|
|
467
|
+
// Re-subscribe mids based on current ref state (not the stale
|
|
468
|
+
// `subscribedMids` cache, which may no longer reflect consumer intent
|
|
469
|
+
// after unmounts/remounts across a disconnect).
|
|
470
|
+
const desiredMids = this.midsSubscriptionCoins();
|
|
471
|
+
if (desiredMids !== null) {
|
|
472
|
+
this.subscribedMids = { exchange: this.midsExchange, coins: desiredMids };
|
|
473
|
+
ws.send(JSON.stringify({
|
|
474
|
+
action: 'subscribe_mids',
|
|
475
|
+
exchange: this.midsExchange,
|
|
476
|
+
coins: desiredMids,
|
|
477
|
+
}));
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
this.subscribedMids = null;
|
|
481
|
+
}
|
|
482
|
+
if (this.subscribedCandles) {
|
|
483
|
+
ws.send(JSON.stringify({
|
|
484
|
+
action: 'subscribe_candles',
|
|
485
|
+
coins: this.subscribedCandles.coins,
|
|
486
|
+
intervals: this.subscribedCandles.intervals,
|
|
487
|
+
batch: true,
|
|
488
|
+
}));
|
|
489
|
+
}
|
|
490
|
+
if (this.candleRefs.size > 0 && !this.subscribedCandles) {
|
|
491
|
+
const allCoins = Array.from(this.candleRefs.keys());
|
|
492
|
+
const allIntervals = new Set();
|
|
493
|
+
for (const ivs of this.candleRefs.values()) {
|
|
494
|
+
for (const iv of ivs)
|
|
495
|
+
allIntervals.add(iv);
|
|
496
|
+
}
|
|
497
|
+
ws.send(JSON.stringify({ action: 'subscribe_candles', coins: allCoins, intervals: Array.from(allIntervals), batch: true }));
|
|
498
|
+
}
|
|
499
|
+
if (this.subscribedTrades) {
|
|
500
|
+
ws.send(JSON.stringify({
|
|
501
|
+
action: 'subscribe_trades',
|
|
502
|
+
coins: this.subscribedTrades.coins,
|
|
503
|
+
}));
|
|
504
|
+
}
|
|
505
|
+
if (this.tradeRefs.size > 0 && !this.subscribedTrades) {
|
|
506
|
+
ws.send(JSON.stringify({ action: 'subscribe_trades', coins: Array.from(this.tradeRefs.keys()) }));
|
|
507
|
+
}
|
|
508
|
+
// Re-watch all paths from ref-counted state
|
|
509
|
+
for (const path of this.pathRefs.keys()) {
|
|
510
|
+
const requestId = `watch-${++this.nextRequestId}`;
|
|
511
|
+
ws.send(JSON.stringify({ action: 'watch', path, requestId }));
|
|
512
|
+
}
|
|
513
|
+
for (const [watchId, req] of this.chartHistoryWatches) {
|
|
514
|
+
ws.send(JSON.stringify({
|
|
515
|
+
action: 'watch_chart_history',
|
|
516
|
+
watchId,
|
|
517
|
+
target: req.target,
|
|
518
|
+
kind: req.kind,
|
|
519
|
+
objectId: req.objectId,
|
|
520
|
+
}));
|
|
521
|
+
}
|
|
522
|
+
// Notify subscribers AFTER all subscriptions are re-issued so any
|
|
523
|
+
// chart-history watch IDs they depend on are already registered.
|
|
524
|
+
this.authenticatedCallbacks.forEach(cb => cb());
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
if (msg.type === 'error') {
|
|
528
|
+
if (msg.requestId && this.pendingRequests.has(msg.requestId)) {
|
|
529
|
+
const pending = this.pendingRequests.get(msg.requestId);
|
|
530
|
+
this.pendingRequests.delete(msg.requestId);
|
|
531
|
+
clearTimeout(pending.timeout);
|
|
532
|
+
pending.onCleanup?.();
|
|
533
|
+
pending.reject(new Error(msg.message ?? 'server error'));
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
if (msg.requestId && this.pendingSnapshots.has(msg.requestId)) {
|
|
537
|
+
const pending = this.pendingSnapshots.get(msg.requestId);
|
|
538
|
+
this.pendingSnapshots.delete(msg.requestId);
|
|
539
|
+
clearTimeout(pending.timeout);
|
|
540
|
+
pending.reject(new Error(msg.message ?? 'server error'));
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
this.emitError({ message: msg.message ?? 'Unknown WebSocket error' });
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
if (msg.type === 'aggregation_watch_created') {
|
|
547
|
+
if (msg.requestId && this.pendingRequests.has(msg.requestId)) {
|
|
548
|
+
const pending = this.pendingRequests.get(msg.requestId);
|
|
549
|
+
this.pendingRequests.delete(msg.requestId);
|
|
550
|
+
clearTimeout(pending.timeout);
|
|
551
|
+
pending.onCleanup?.();
|
|
552
|
+
pending.resolve({ watchId: msg.watchId, aggregation: msg.aggregation });
|
|
553
|
+
}
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
if (msg.type === 'chart_snapshot_watch_created') {
|
|
557
|
+
if (msg.requestId && this.pendingRequests.has(msg.requestId)) {
|
|
558
|
+
const pending = this.pendingRequests.get(msg.requestId);
|
|
559
|
+
this.pendingRequests.delete(msg.requestId);
|
|
560
|
+
clearTimeout(pending.timeout);
|
|
561
|
+
pending.resolve({ watchId: msg.watchId });
|
|
562
|
+
}
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
// Path-scoped watch snapshot
|
|
566
|
+
if (msg.type === 'watch_snapshot') {
|
|
567
|
+
const snapshot = {
|
|
568
|
+
path: msg.path ?? '',
|
|
569
|
+
balances: msg.balances ?? [],
|
|
570
|
+
operations: msg.operations ?? [],
|
|
571
|
+
bufferedOperations: msg.bufferedOperations ?? [],
|
|
572
|
+
exchange: msg.exchange ?? null,
|
|
573
|
+
valuation: msg.valuation ?? null,
|
|
574
|
+
valuations: msg.valuations ?? undefined,
|
|
575
|
+
watchId: msg.watchId ?? null,
|
|
576
|
+
};
|
|
577
|
+
if (msg.requestId && this.pendingSnapshots.has(msg.requestId)) {
|
|
578
|
+
const pending = this.pendingSnapshots.get(msg.requestId);
|
|
579
|
+
this.pendingSnapshots.delete(msg.requestId);
|
|
580
|
+
clearTimeout(pending.timeout);
|
|
581
|
+
pending.resolve(snapshot);
|
|
582
|
+
}
|
|
583
|
+
this.dispatchEvent({ type: 'watch_snapshot', ...msg });
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
if (msg.type === 'mids.snapshot' && msg.mids) {
|
|
587
|
+
// Server-side `mids.snapshot` is a full current-state read sent
|
|
588
|
+
// immediately after subscribe (and after reconnect). It has
|
|
589
|
+
// different semantics from `mids.updated` (no deliverySeq, no
|
|
590
|
+
// marketDataAsOf, always complete for the subscribed market set) but
|
|
591
|
+
// the SDK collapses it into the same MidsUpdated dispatch so
|
|
592
|
+
// consumers get a single callback to write against. Stream
|
|
593
|
+
// implementations can distinguish via the `_snapshot` marker.
|
|
594
|
+
this.dispatchEvent({ type: types_1.EventType.MidsUpdated, mids: msg.mids, _snapshot: true });
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
if (msg.type === 'candles.updated' && Array.isArray(msg.candles)) {
|
|
598
|
+
if (typeof msg.deliverySeq === 'number') {
|
|
599
|
+
this.checkDeliveryGap(msg.deliverySeq);
|
|
600
|
+
}
|
|
601
|
+
for (const item of msg.candles) {
|
|
602
|
+
this.dispatchEvent({
|
|
603
|
+
type: types_1.EventType.CandleUpdated,
|
|
604
|
+
market: item.market,
|
|
605
|
+
interval: item.interval,
|
|
606
|
+
candle: item.candle,
|
|
607
|
+
publishedAt: msg.publishedAt,
|
|
608
|
+
platformReceivedAt: msg.platformReceivedAt,
|
|
609
|
+
wsSentAt: msg.wsSentAt,
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
if (msg.type === 'trades.batch' && Array.isArray(msg.trades)) {
|
|
615
|
+
if (typeof msg.deliverySeq === 'number') {
|
|
616
|
+
this.checkDeliveryGap(msg.deliverySeq);
|
|
617
|
+
}
|
|
618
|
+
for (const item of msg.trades) {
|
|
619
|
+
this.dispatchEvent({
|
|
620
|
+
type: types_1.EventType.TradeExecuted,
|
|
621
|
+
market: item.market,
|
|
622
|
+
trade: item,
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
if (typeof msg.deliverySeq === 'number') {
|
|
628
|
+
this.checkDeliveryGap(msg.deliverySeq);
|
|
629
|
+
}
|
|
630
|
+
const event = msg;
|
|
631
|
+
this.dispatchEvent(event);
|
|
632
|
+
}
|
|
633
|
+
catch {
|
|
634
|
+
// Ignore parse errors
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
ws.onclose = () => {
|
|
638
|
+
this.stopHeartbeat();
|
|
639
|
+
this.rejectPendingRequests('WebSocket disconnected');
|
|
640
|
+
this.rejectPendingSnapshots('WebSocket disconnected');
|
|
641
|
+
this.emitStatus('disconnected');
|
|
642
|
+
if (this.shouldReconnect) {
|
|
643
|
+
this.scheduleReconnect();
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
ws.onerror = () => {
|
|
647
|
+
// onclose will fire after onerror
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
subscribeMids(exchange, coins = []) {
|
|
651
|
+
this.subscribedMids = { exchange, coins };
|
|
652
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
653
|
+
this.ws.send(JSON.stringify({ action: 'subscribe_mids', exchange, coins }));
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
unsubscribeMids() {
|
|
657
|
+
this.subscribedMids = null;
|
|
658
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
659
|
+
this.ws.send(JSON.stringify({ action: 'unsubscribe_mids' }));
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
subscribeCandles(coins, intervals) {
|
|
663
|
+
this.subscribedCandles = { coins, intervals };
|
|
664
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
665
|
+
this.ws.send(JSON.stringify({ action: 'subscribe_candles', coins, intervals, batch: true }));
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
unsubscribeCandles() {
|
|
669
|
+
this.subscribedCandles = null;
|
|
670
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
671
|
+
this.ws.send(JSON.stringify({ action: 'unsubscribe_candles' }));
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
subscribeTrades(coins) {
|
|
675
|
+
this.subscribedTrades = { coins };
|
|
676
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
677
|
+
this.ws.send(JSON.stringify({ action: 'subscribe_trades', coins }));
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
unsubscribeTrades() {
|
|
681
|
+
this.subscribedTrades = null;
|
|
682
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
683
|
+
this.ws.send(JSON.stringify({ action: 'unsubscribe_trades' }));
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
// ---- Generic listeners ----
|
|
687
|
+
on(eventType, callback) {
|
|
688
|
+
if (!this.callbacks.has(eventType)) {
|
|
689
|
+
this.callbacks.set(eventType, new Set());
|
|
690
|
+
}
|
|
691
|
+
this.callbacks.get(eventType).add(callback);
|
|
692
|
+
}
|
|
693
|
+
off(eventType, callback) {
|
|
694
|
+
const set = this.callbacks.get(eventType);
|
|
695
|
+
if (!set)
|
|
696
|
+
return;
|
|
697
|
+
set.delete(callback);
|
|
698
|
+
if (set.size === 0)
|
|
699
|
+
this.callbacks.delete(eventType);
|
|
700
|
+
}
|
|
701
|
+
onStatus(callback) {
|
|
702
|
+
this.statusCallbacks.add(callback);
|
|
703
|
+
return () => { this.statusCallbacks.delete(callback); };
|
|
704
|
+
}
|
|
705
|
+
offStatus(callback) {
|
|
706
|
+
this.statusCallbacks.delete(callback);
|
|
707
|
+
}
|
|
708
|
+
onError(callback) {
|
|
709
|
+
this.errorCallbacks.add(callback);
|
|
710
|
+
}
|
|
711
|
+
offError(callback) {
|
|
712
|
+
this.errorCallbacks.delete(callback);
|
|
713
|
+
}
|
|
714
|
+
// ---- Typed convenience listeners (each returns an unsubscribe function) ----
|
|
715
|
+
onOperationCreated(cb) {
|
|
716
|
+
const handler = (event) => {
|
|
717
|
+
if (event.operation)
|
|
718
|
+
cb(event.operation, event);
|
|
719
|
+
};
|
|
720
|
+
this.on(types_1.EventType.OperationCreated, handler);
|
|
721
|
+
return () => this.off(types_1.EventType.OperationCreated, handler);
|
|
722
|
+
}
|
|
723
|
+
onOperationUpdated(cb) {
|
|
724
|
+
const handler = (event) => {
|
|
725
|
+
if (event.operation)
|
|
726
|
+
cb(event.operation, event);
|
|
727
|
+
};
|
|
728
|
+
this.on(types_1.EventType.OperationUpdated, handler);
|
|
729
|
+
return () => this.off(types_1.EventType.OperationUpdated, handler);
|
|
730
|
+
}
|
|
731
|
+
onEventCreated(cb) {
|
|
732
|
+
const handler = (event) => {
|
|
733
|
+
if (event.event)
|
|
734
|
+
cb(event.event, event);
|
|
735
|
+
};
|
|
736
|
+
this.on(types_1.EventType.EventCreated, handler);
|
|
737
|
+
return () => this.off(types_1.EventType.EventCreated, handler);
|
|
738
|
+
}
|
|
739
|
+
onObjectCreated(cb) {
|
|
740
|
+
const handler = (event) => {
|
|
741
|
+
cb(event.object, event);
|
|
742
|
+
};
|
|
743
|
+
this.on(types_1.EventType.ObjectCreated, handler);
|
|
744
|
+
return () => this.off(types_1.EventType.ObjectCreated, handler);
|
|
745
|
+
}
|
|
746
|
+
onObjectUpdated(cb) {
|
|
747
|
+
const handler = (event) => {
|
|
748
|
+
cb(event.object, event);
|
|
749
|
+
};
|
|
750
|
+
this.on(types_1.EventType.ObjectUpdated, handler);
|
|
751
|
+
return () => this.off(types_1.EventType.ObjectUpdated, handler);
|
|
752
|
+
}
|
|
753
|
+
onObjectDeleted(cb) {
|
|
754
|
+
this.on(types_1.EventType.ObjectDeleted, cb);
|
|
755
|
+
return () => this.off(types_1.EventType.ObjectDeleted, cb);
|
|
756
|
+
}
|
|
757
|
+
onBalanceUpdated(cb) {
|
|
758
|
+
const handler = (event) => {
|
|
759
|
+
if (!event.entityId)
|
|
760
|
+
return;
|
|
761
|
+
cb(event.entityId, event);
|
|
762
|
+
};
|
|
763
|
+
this.on(types_1.EventType.BalanceUpdated, handler);
|
|
764
|
+
return () => this.off(types_1.EventType.BalanceUpdated, handler);
|
|
765
|
+
}
|
|
766
|
+
onObjectValuation(cb) {
|
|
767
|
+
const handler = (event) => {
|
|
768
|
+
if (!event.path || !event.watchId)
|
|
769
|
+
return;
|
|
770
|
+
cb(event.path, event.valuation, event.watchId, event);
|
|
771
|
+
};
|
|
772
|
+
this.on(types_1.EventType.ObjectValuation, handler);
|
|
773
|
+
return () => this.off(types_1.EventType.ObjectValuation, handler);
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* Subscribe to all TWAP events for the current realm. Each callback
|
|
777
|
+
* receives the TwapEvent type discriminator + the (server-enriched)
|
|
778
|
+
* full Twap object. To listen to a single TWAP, filter on
|
|
779
|
+
* `event.twapId === target` or use {@link Arca.watchTwap}.
|
|
780
|
+
*/
|
|
781
|
+
onTwapEvent(cb) {
|
|
782
|
+
const handlers = [];
|
|
783
|
+
const types = [
|
|
784
|
+
types_1.EventType.TwapStarted,
|
|
785
|
+
types_1.EventType.TwapProgress,
|
|
786
|
+
types_1.EventType.TwapCompleted,
|
|
787
|
+
types_1.EventType.TwapCancelled,
|
|
788
|
+
types_1.EventType.TwapFailed,
|
|
789
|
+
];
|
|
790
|
+
for (const t of types) {
|
|
791
|
+
const handler = (event) => {
|
|
792
|
+
cb(event);
|
|
793
|
+
};
|
|
794
|
+
this.on(t, handler);
|
|
795
|
+
handlers.push([t, handler]);
|
|
796
|
+
}
|
|
797
|
+
return () => {
|
|
798
|
+
for (const [t, h] of handlers)
|
|
799
|
+
this.off(t, h);
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
createAggregationWatch(sources, flowsSince) {
|
|
803
|
+
return new Promise((resolve, reject) => {
|
|
804
|
+
this.ensureConnected();
|
|
805
|
+
const requestId = String(++this.nextRequestId);
|
|
806
|
+
const timeout = setTimeout(() => {
|
|
807
|
+
const pending = this.pendingRequests.get(requestId);
|
|
808
|
+
pending?.onCleanup?.();
|
|
809
|
+
this.pendingRequests.delete(requestId);
|
|
810
|
+
reject(new Error('createAggregationWatch: timeout waiting for server response'));
|
|
811
|
+
}, 10_000);
|
|
812
|
+
this.pendingRequests.set(requestId, { resolve, reject, timeout });
|
|
813
|
+
const send = () => {
|
|
814
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
815
|
+
const msg = { action: 'create_aggregation_watch', sources, requestId };
|
|
816
|
+
if (flowsSince)
|
|
817
|
+
msg.flowsSince = flowsSince;
|
|
818
|
+
this.ws.send(JSON.stringify(msg));
|
|
819
|
+
}
|
|
820
|
+
};
|
|
821
|
+
if (this.lastStatus === 'connected') {
|
|
822
|
+
send();
|
|
823
|
+
}
|
|
824
|
+
else {
|
|
825
|
+
const unsub = this.onStatus((status) => {
|
|
826
|
+
if (status === 'connected') {
|
|
827
|
+
unsub();
|
|
828
|
+
const pending = this.pendingRequests.get(requestId);
|
|
829
|
+
if (pending) {
|
|
830
|
+
pending.onCleanup = undefined;
|
|
831
|
+
send();
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
});
|
|
835
|
+
const pending = this.pendingRequests.get(requestId);
|
|
836
|
+
if (pending) {
|
|
837
|
+
pending.onCleanup = unsub;
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
destroyAggregationWatch(watchId) {
|
|
843
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
844
|
+
this.ws.send(JSON.stringify({ action: 'destroy_aggregation_watch', watchId }));
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
createChartHistoryWatch(req) {
|
|
848
|
+
return new Promise((resolve, reject) => {
|
|
849
|
+
this.ensureConnected();
|
|
850
|
+
const requestId = `chart-${++this.nextRequestId}`;
|
|
851
|
+
const watchId = `chart-${++this.nextRequestId}`;
|
|
852
|
+
const kind = req.kind ?? 'path';
|
|
853
|
+
this.chartHistoryWatches.set(watchId, { target: req.target, kind, objectId: req.objectId });
|
|
854
|
+
const timeout = setTimeout(() => {
|
|
855
|
+
const pending = this.pendingRequests.get(requestId);
|
|
856
|
+
pending?.onCleanup?.();
|
|
857
|
+
this.pendingRequests.delete(requestId);
|
|
858
|
+
this.chartHistoryWatches.delete(watchId);
|
|
859
|
+
reject(new Error('createChartHistoryWatch: timeout waiting for server response'));
|
|
860
|
+
}, 10_000);
|
|
861
|
+
this.pendingRequests.set(requestId, {
|
|
862
|
+
resolve,
|
|
863
|
+
reject,
|
|
864
|
+
timeout,
|
|
865
|
+
onCleanup: () => { this.chartHistoryWatches.delete(watchId); },
|
|
866
|
+
});
|
|
867
|
+
const send = () => {
|
|
868
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
869
|
+
this.ws.send(JSON.stringify({
|
|
870
|
+
action: 'watch_chart_history',
|
|
871
|
+
requestId,
|
|
872
|
+
watchId,
|
|
873
|
+
target: req.target,
|
|
874
|
+
kind,
|
|
875
|
+
objectId: req.objectId,
|
|
876
|
+
}));
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
if (this.lastStatus === 'connected') {
|
|
880
|
+
send();
|
|
881
|
+
}
|
|
882
|
+
else {
|
|
883
|
+
const unsub = this.onStatus((status) => {
|
|
884
|
+
if (status === 'connected') {
|
|
885
|
+
unsub();
|
|
886
|
+
const pending = this.pendingRequests.get(requestId);
|
|
887
|
+
if (pending) {
|
|
888
|
+
pending.onCleanup = () => { this.chartHistoryWatches.delete(watchId); };
|
|
889
|
+
send();
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
const pending = this.pendingRequests.get(requestId);
|
|
894
|
+
if (pending) {
|
|
895
|
+
pending.onCleanup = () => {
|
|
896
|
+
unsub();
|
|
897
|
+
this.chartHistoryWatches.delete(watchId);
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
destroyChartHistoryWatch(watchId) {
|
|
904
|
+
this.chartHistoryWatches.delete(watchId);
|
|
905
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
906
|
+
this.ws.send(JSON.stringify({ action: 'unwatch_chart_history', watchId }));
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
onExchangeUpdated(cb) {
|
|
910
|
+
const handler = (event) => {
|
|
911
|
+
if (event.exchangeState)
|
|
912
|
+
cb(event.exchangeState, event);
|
|
913
|
+
};
|
|
914
|
+
this.on(types_1.EventType.ExchangeUpdated, handler);
|
|
915
|
+
return () => this.off(types_1.EventType.ExchangeUpdated, handler);
|
|
916
|
+
}
|
|
917
|
+
onExchangeNotification(cb) {
|
|
918
|
+
const handler = (event) => {
|
|
919
|
+
cb(event);
|
|
920
|
+
};
|
|
921
|
+
this.on(types_1.EventType.ExchangeUpdated, handler);
|
|
922
|
+
return () => this.off(types_1.EventType.ExchangeUpdated, handler);
|
|
923
|
+
}
|
|
924
|
+
onFillPreviewed(cb) {
|
|
925
|
+
const handler = (event) => {
|
|
926
|
+
if (event.fill)
|
|
927
|
+
cb(event.fill, event);
|
|
928
|
+
};
|
|
929
|
+
this.on(types_1.EventType.FillPreviewed, handler);
|
|
930
|
+
return () => this.off(types_1.EventType.FillPreviewed, handler);
|
|
931
|
+
}
|
|
932
|
+
/** @deprecated Use {@link onFillPreviewed}. Event `exchange.fill` was renamed to `fill.previewed`. */
|
|
933
|
+
onExchangeFill(cb) {
|
|
934
|
+
return this.onFillPreviewed(cb);
|
|
935
|
+
}
|
|
936
|
+
onFillRecorded(cb) {
|
|
937
|
+
const handler = (event) => {
|
|
938
|
+
if (event.fill)
|
|
939
|
+
cb(event.fill, event);
|
|
940
|
+
};
|
|
941
|
+
this.on(types_1.EventType.FillRecorded, handler);
|
|
942
|
+
return () => this.off(types_1.EventType.FillRecorded, handler);
|
|
943
|
+
}
|
|
944
|
+
onExchangeFunding(cb) {
|
|
945
|
+
const handler = (event) => {
|
|
946
|
+
if (event.funding)
|
|
947
|
+
cb(event.funding, event);
|
|
948
|
+
};
|
|
949
|
+
this.on(types_1.EventType.ExchangeFunding, handler);
|
|
950
|
+
return () => this.off(types_1.EventType.ExchangeFunding, handler);
|
|
951
|
+
}
|
|
952
|
+
onMidsUpdated(cb) {
|
|
953
|
+
const handler = (event) => {
|
|
954
|
+
if (event.mids) {
|
|
955
|
+
const snapshot = event._snapshot === true;
|
|
956
|
+
cb(event.mids, { snapshot });
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
this.on(types_1.EventType.MidsUpdated, handler);
|
|
960
|
+
return () => this.off(types_1.EventType.MidsUpdated, handler);
|
|
961
|
+
}
|
|
962
|
+
onCandleClosed(cb) {
|
|
963
|
+
const handler = (raw) => {
|
|
964
|
+
const timed = raw;
|
|
965
|
+
cb({
|
|
966
|
+
market: timed.market,
|
|
967
|
+
interval: timed.interval,
|
|
968
|
+
candle: timed.candle,
|
|
969
|
+
publishedAt: timed.publishedAt,
|
|
970
|
+
platformReceivedAt: timed.platformReceivedAt,
|
|
971
|
+
wsSentAt: timed.wsSentAt,
|
|
972
|
+
});
|
|
973
|
+
};
|
|
974
|
+
this.on(types_1.EventType.CandleClosed, handler);
|
|
975
|
+
return () => this.off(types_1.EventType.CandleClosed, handler);
|
|
976
|
+
}
|
|
977
|
+
onCandleUpdated(cb) {
|
|
978
|
+
const handler = (raw) => {
|
|
979
|
+
const timed = raw;
|
|
980
|
+
cb({
|
|
981
|
+
market: timed.market,
|
|
982
|
+
interval: timed.interval,
|
|
983
|
+
candle: timed.candle,
|
|
984
|
+
publishedAt: timed.publishedAt,
|
|
985
|
+
platformReceivedAt: timed.platformReceivedAt,
|
|
986
|
+
wsSentAt: timed.wsSentAt,
|
|
987
|
+
});
|
|
988
|
+
};
|
|
989
|
+
this.on(types_1.EventType.CandleUpdated, handler);
|
|
990
|
+
return () => this.off(types_1.EventType.CandleUpdated, handler);
|
|
991
|
+
}
|
|
992
|
+
onTradeExecuted(cb) {
|
|
993
|
+
const handler = (raw) => {
|
|
994
|
+
cb({
|
|
995
|
+
market: raw.market,
|
|
996
|
+
trade: raw.trade,
|
|
997
|
+
});
|
|
998
|
+
};
|
|
999
|
+
this.on(types_1.EventType.TradeExecuted, handler);
|
|
1000
|
+
return () => this.off(types_1.EventType.TradeExecuted, handler);
|
|
1001
|
+
}
|
|
1002
|
+
onAggregationUpdated(cb) {
|
|
1003
|
+
const handler = (event) => {
|
|
1004
|
+
if (!event.entityId)
|
|
1005
|
+
return;
|
|
1006
|
+
cb(event.entityId, event.aggregation, event);
|
|
1007
|
+
};
|
|
1008
|
+
this.on(types_1.EventType.AggregationUpdated, handler);
|
|
1009
|
+
return () => this.off(types_1.EventType.AggregationUpdated, handler);
|
|
1010
|
+
}
|
|
1011
|
+
onChartSnapshotUpdated(cb) {
|
|
1012
|
+
const handler = (event) => {
|
|
1013
|
+
const watchId = event.watchId;
|
|
1014
|
+
if (!watchId)
|
|
1015
|
+
return;
|
|
1016
|
+
cb(watchId, event);
|
|
1017
|
+
};
|
|
1018
|
+
this.on('chart.snapshot.updated', handler);
|
|
1019
|
+
return () => this.off('chart.snapshot.updated', handler);
|
|
1020
|
+
}
|
|
1021
|
+
onRealmCreated(cb) {
|
|
1022
|
+
const handler = (event) => {
|
|
1023
|
+
if (event.realm)
|
|
1024
|
+
cb(event.realm, event);
|
|
1025
|
+
};
|
|
1026
|
+
this.on(types_1.EventType.RealmCreated, handler);
|
|
1027
|
+
return () => this.off(types_1.EventType.RealmCreated, handler);
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* Listen for all agent.* events. The callback receives typed AgentEvent
|
|
1031
|
+
* discriminated by `type`. Filter by `chatId` on the caller side.
|
|
1032
|
+
* Returns an unsubscribe function.
|
|
1033
|
+
*/
|
|
1034
|
+
onAgentEvent(cb) {
|
|
1035
|
+
const agentTypes = [
|
|
1036
|
+
types_1.EventType.AgentText,
|
|
1037
|
+
types_1.EventType.AgentToolUse,
|
|
1038
|
+
types_1.EventType.AgentPlan,
|
|
1039
|
+
types_1.EventType.AgentConversationLog,
|
|
1040
|
+
types_1.EventType.AgentDone,
|
|
1041
|
+
types_1.EventType.AgentStepUpdated,
|
|
1042
|
+
types_1.EventType.AgentExecutionDone,
|
|
1043
|
+
];
|
|
1044
|
+
const handlers = [];
|
|
1045
|
+
for (const t of agentTypes) {
|
|
1046
|
+
const handler = (event) => {
|
|
1047
|
+
cb(event);
|
|
1048
|
+
};
|
|
1049
|
+
this.on(t, handler);
|
|
1050
|
+
handlers.push({ type: t, handler });
|
|
1051
|
+
}
|
|
1052
|
+
return () => {
|
|
1053
|
+
for (const { type, handler } of handlers) {
|
|
1054
|
+
this.off(type, handler);
|
|
1055
|
+
}
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
onSummaryUpdated(cb) {
|
|
1059
|
+
const handler = (event) => {
|
|
1060
|
+
if (event.summary)
|
|
1061
|
+
cb(event.summary);
|
|
1062
|
+
};
|
|
1063
|
+
if (!this.callbacks.has('*')) {
|
|
1064
|
+
this.callbacks.set('*', new Set());
|
|
1065
|
+
}
|
|
1066
|
+
this.callbacks.get('*').add(handler);
|
|
1067
|
+
return () => { this.callbacks.get('*')?.delete(handler); };
|
|
1068
|
+
}
|
|
1069
|
+
disconnect() {
|
|
1070
|
+
this.shouldReconnect = false;
|
|
1071
|
+
this.stopHeartbeat();
|
|
1072
|
+
if (this.reconnectTimer) {
|
|
1073
|
+
clearTimeout(this.reconnectTimer);
|
|
1074
|
+
this.reconnectTimer = null;
|
|
1075
|
+
}
|
|
1076
|
+
if (this.resumePingTimer) {
|
|
1077
|
+
clearTimeout(this.resumePingTimer);
|
|
1078
|
+
this.resumePingTimer = null;
|
|
1079
|
+
}
|
|
1080
|
+
this.cancelIdleTimer();
|
|
1081
|
+
for (const timer of this.unsubTimers.values())
|
|
1082
|
+
clearTimeout(timer);
|
|
1083
|
+
this.unsubTimers.clear();
|
|
1084
|
+
this.rejectPendingSnapshots('WebSocket disconnected');
|
|
1085
|
+
if (this.ws) {
|
|
1086
|
+
this.ws.close();
|
|
1087
|
+
this.ws = null;
|
|
1088
|
+
}
|
|
1089
|
+
this.emitStatus('disconnected');
|
|
1090
|
+
if (this.removeVisibilityListener) {
|
|
1091
|
+
this.removeVisibilityListener();
|
|
1092
|
+
this.removeVisibilityListener = null;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
emitLocal(event) {
|
|
1096
|
+
this.dispatchEvent(event);
|
|
1097
|
+
}
|
|
1098
|
+
dispatchEvent(event) {
|
|
1099
|
+
const type = event.type;
|
|
1100
|
+
this.callbacks.get(type)?.forEach(cb => cb(event));
|
|
1101
|
+
this.callbacks.get('*')?.forEach(cb => cb(event));
|
|
1102
|
+
}
|
|
1103
|
+
emitStatus(status) {
|
|
1104
|
+
if (status !== this.lastStatus) {
|
|
1105
|
+
this.lastStatus = status;
|
|
1106
|
+
this.statusCallbacks.forEach(cb => cb(status));
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
checkDeliveryGap(seq) {
|
|
1110
|
+
if (this.lastDeliverySeq > 0 && seq > this.lastDeliverySeq + 1) {
|
|
1111
|
+
const missed = seq - this.lastDeliverySeq - 1;
|
|
1112
|
+
this.gapCallbacks.forEach(cb => cb(missed));
|
|
1113
|
+
}
|
|
1114
|
+
this.lastDeliverySeq = seq;
|
|
1115
|
+
}
|
|
1116
|
+
/**
|
|
1117
|
+
* Register a callback for delivery sequence gaps. The callback receives the
|
|
1118
|
+
* number of missed events. Watch streams use this to trigger targeted refetches.
|
|
1119
|
+
*/
|
|
1120
|
+
onGap(cb) {
|
|
1121
|
+
this.gapCallbacks.add(cb);
|
|
1122
|
+
return () => { this.gapCallbacks.delete(cb); };
|
|
1123
|
+
}
|
|
1124
|
+
/**
|
|
1125
|
+
* Register a callback fired when the page returns from a backgrounded /
|
|
1126
|
+
* tab-frozen state (visibility transition `hidden -> visible` after at
|
|
1127
|
+
* least {@link WebSocketManager.RESUME_HIDDEN_THRESHOLD_MS}ms hidden).
|
|
1128
|
+
* Watch streams use this to refresh historical data even when the WS
|
|
1129
|
+
* stayed nominally connected through the freeze. Receives the hidden
|
|
1130
|
+
* duration in milliseconds.
|
|
1131
|
+
*/
|
|
1132
|
+
onResume(cb) {
|
|
1133
|
+
this.resumeCallbacks.add(cb);
|
|
1134
|
+
return () => { this.resumeCallbacks.delete(cb); };
|
|
1135
|
+
}
|
|
1136
|
+
/**
|
|
1137
|
+
* Register a callback fired on every successful WebSocket
|
|
1138
|
+
* `authenticated` message. Includes the very first auth and every
|
|
1139
|
+
* subsequent reconnect-and-auth. Watch streams use this to refresh on
|
|
1140
|
+
* fresh auth instead of relying on observing a `disconnected -> connected`
|
|
1141
|
+
* status transition (which can be missed if the WS reconnects faster
|
|
1142
|
+
* than the disconnect callback fires).
|
|
1143
|
+
*/
|
|
1144
|
+
onAuthenticated(cb) {
|
|
1145
|
+
this.authenticatedCallbacks.add(cb);
|
|
1146
|
+
return () => { this.authenticatedCallbacks.delete(cb); };
|
|
1147
|
+
}
|
|
1148
|
+
installVisibilityListener() {
|
|
1149
|
+
// Idempotent: if a listener is already installed, return. The constructor
|
|
1150
|
+
// calls this once and `connect()` re-calls it after a `disconnect()`
|
|
1151
|
+
// cleared it; back-to-back connects with no intervening disconnect must
|
|
1152
|
+
// not double-install. Mirrors Swift's `installLifecycleObservers` which
|
|
1153
|
+
// guards on `lifecycleObservers.isEmpty`.
|
|
1154
|
+
if (this.removeVisibilityListener)
|
|
1155
|
+
return;
|
|
1156
|
+
// Access `document` through `globalThis` so the SDK still type-checks
|
|
1157
|
+
// under the Node/ES2022 lib (no DOM types) while continuing to attach
|
|
1158
|
+
// the listener in browsers.
|
|
1159
|
+
const doc = globalThis.document;
|
|
1160
|
+
if (!doc)
|
|
1161
|
+
return;
|
|
1162
|
+
const handler = () => {
|
|
1163
|
+
if (doc.hidden) {
|
|
1164
|
+
this.hiddenAt = Date.now();
|
|
1165
|
+
}
|
|
1166
|
+
else {
|
|
1167
|
+
const hiddenAt = this.hiddenAt;
|
|
1168
|
+
this.hiddenAt = null;
|
|
1169
|
+
if (hiddenAt === null)
|
|
1170
|
+
return;
|
|
1171
|
+
const hiddenDuration = Date.now() - hiddenAt;
|
|
1172
|
+
if (hiddenDuration < WebSocketManager.RESUME_HIDDEN_THRESHOLD_MS)
|
|
1173
|
+
return;
|
|
1174
|
+
this.handleAppResumed(hiddenDuration);
|
|
1175
|
+
}
|
|
1176
|
+
};
|
|
1177
|
+
doc.addEventListener('visibilitychange', handler);
|
|
1178
|
+
this.removeVisibilityListener = () => {
|
|
1179
|
+
doc.removeEventListener('visibilitychange', handler);
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
/**
|
|
1183
|
+
* Internal: invoked on a `hidden -> visible` transition after the
|
|
1184
|
+
* threshold. Fires `onResume` callbacks and probes the connection for
|
|
1185
|
+
* staleness. Exposed for tests to drive without requiring `document`.
|
|
1186
|
+
* @internal
|
|
1187
|
+
*/
|
|
1188
|
+
triggerResume(hiddenDurationMs) {
|
|
1189
|
+
this.handleAppResumed(hiddenDurationMs);
|
|
1190
|
+
}
|
|
1191
|
+
handleAppResumed(hiddenDurationMs) {
|
|
1192
|
+
this.resumeCallbacks.forEach(cb => cb(hiddenDurationMs));
|
|
1193
|
+
this.probeStaleConnection();
|
|
1194
|
+
}
|
|
1195
|
+
probeStaleConnection() {
|
|
1196
|
+
if (this.resumePingTimer) {
|
|
1197
|
+
clearTimeout(this.resumePingTimer);
|
|
1198
|
+
this.resumePingTimer = null;
|
|
1199
|
+
}
|
|
1200
|
+
if (this.ws?.readyState !== getWS().OPEN) {
|
|
1201
|
+
// Not OPEN: either CONNECTING (let it finish), or CLOSED/null
|
|
1202
|
+
// (reconnect machinery already running if shouldReconnect).
|
|
1203
|
+
return;
|
|
1204
|
+
}
|
|
1205
|
+
const baselineCounter = this.messageCounter;
|
|
1206
|
+
this.ws.send(JSON.stringify({ action: 'ping' }));
|
|
1207
|
+
this.resumePingTimer = setTimeout(() => {
|
|
1208
|
+
this.resumePingTimer = null;
|
|
1209
|
+
// Any inbound message bumps `messageCounter`; if it changed, the
|
|
1210
|
+
// connection is alive and we don't need to reconnect.
|
|
1211
|
+
if (this.messageCounter !== baselineCounter)
|
|
1212
|
+
return;
|
|
1213
|
+
this.stopHeartbeat();
|
|
1214
|
+
if (this.ws) {
|
|
1215
|
+
this.ws.close();
|
|
1216
|
+
this.ws = null;
|
|
1217
|
+
}
|
|
1218
|
+
this.emitStatus('disconnected');
|
|
1219
|
+
if (this.shouldReconnect) {
|
|
1220
|
+
this.scheduleReconnect();
|
|
1221
|
+
}
|
|
1222
|
+
}, WebSocketManager.RESUME_PING_TIMEOUT_MS);
|
|
1223
|
+
}
|
|
1224
|
+
rejectPendingRequests(reason) {
|
|
1225
|
+
for (const [, pending] of this.pendingRequests) {
|
|
1226
|
+
clearTimeout(pending.timeout);
|
|
1227
|
+
pending.onCleanup?.();
|
|
1228
|
+
pending.reject(new Error(reason));
|
|
1229
|
+
}
|
|
1230
|
+
this.pendingRequests.clear();
|
|
1231
|
+
}
|
|
1232
|
+
rejectPendingSnapshots(reason) {
|
|
1233
|
+
for (const [, pending] of this.pendingSnapshots) {
|
|
1234
|
+
clearTimeout(pending.timeout);
|
|
1235
|
+
pending.reject(new Error(reason));
|
|
1236
|
+
}
|
|
1237
|
+
this.pendingSnapshots.clear();
|
|
1238
|
+
this.snapshotCache.clear();
|
|
1239
|
+
}
|
|
1240
|
+
emitError(error) {
|
|
1241
|
+
if (this.errorCallbacks.size > 0) {
|
|
1242
|
+
this.errorCallbacks.forEach(cb => cb(error));
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
startHeartbeat() {
|
|
1246
|
+
this.stopHeartbeat();
|
|
1247
|
+
this.lastMessageAt = Date.now();
|
|
1248
|
+
this.pingTimer = setInterval(() => {
|
|
1249
|
+
const elapsed = Date.now() - this.lastMessageAt;
|
|
1250
|
+
if (elapsed >= WebSocketManager.STALE_THRESHOLD_MS) {
|
|
1251
|
+
this.stopHeartbeat();
|
|
1252
|
+
if (this.ws) {
|
|
1253
|
+
this.ws.close();
|
|
1254
|
+
this.ws = null;
|
|
1255
|
+
}
|
|
1256
|
+
this.emitStatus('disconnected');
|
|
1257
|
+
if (this.shouldReconnect) {
|
|
1258
|
+
this.scheduleReconnect();
|
|
1259
|
+
}
|
|
1260
|
+
return;
|
|
1261
|
+
}
|
|
1262
|
+
if (this.ws?.readyState === getWS().OPEN) {
|
|
1263
|
+
this.ws.send(JSON.stringify({ action: 'ping' }));
|
|
1264
|
+
}
|
|
1265
|
+
}, WebSocketManager.PING_INTERVAL_MS);
|
|
1266
|
+
}
|
|
1267
|
+
stopHeartbeat() {
|
|
1268
|
+
if (this.pingTimer) {
|
|
1269
|
+
clearInterval(this.pingTimer);
|
|
1270
|
+
this.pingTimer = null;
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
scheduleReconnect() {
|
|
1274
|
+
if (this.reconnectTimer)
|
|
1275
|
+
return;
|
|
1276
|
+
if (this.reconnectAttempt >= this.maxReconnectAttempts) {
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempt), this.maxReconnectDelay);
|
|
1280
|
+
this.reconnectAttempt++;
|
|
1281
|
+
this.reconnectTimer = setTimeout(() => {
|
|
1282
|
+
this.reconnectTimer = null;
|
|
1283
|
+
this.doConnect();
|
|
1284
|
+
}, delay);
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
exports.WebSocketManager = WebSocketManager;
|
|
1288
|
+
//# sourceMappingURL=websocket.js.map
|