@stryke-xyz/premarket-sdk 1.2.1 → 1.2.3

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.
Files changed (35) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/sync/clients/activity-client.js +325 -454
  3. package/dist/cjs/sync/clients/order-client.js +609 -429
  4. package/dist/esm/package.json +1 -1
  5. package/dist/esm/sync/clients/activity-client.js +325 -454
  6. package/dist/esm/sync/clients/order-client.js +609 -429
  7. package/dist/types/sync/clients/activity-client.d.ts +39 -32
  8. package/dist/types/sync/clients/order-client.d.ts +79 -58
  9. package/dist/types/sync/index.d.ts +2 -2
  10. package/package.json +58 -58
  11. package/dist/cjs/api/README.md +0 -296
  12. package/dist/cjs/config/README.md +0 -112
  13. package/dist/cjs/exchange/README.md +0 -280
  14. package/dist/cjs/registry/README.md +0 -150
  15. package/dist/cjs/shared/README.md +0 -235
  16. package/dist/cjs/shared/utils.js +0 -1
  17. package/dist/cjs/sync/README.md +0 -215
  18. package/dist/cjs/sync/clients/base-client.js +0 -518
  19. package/dist/cjs/sync/redis-ws-client.js +0 -235
  20. package/dist/cjs/utils/README.md +0 -89
  21. package/dist/cjs/vault/README.md +0 -268
  22. package/dist/esm/api/README.md +0 -296
  23. package/dist/esm/config/README.md +0 -112
  24. package/dist/esm/exchange/README.md +0 -280
  25. package/dist/esm/registry/README.md +0 -150
  26. package/dist/esm/shared/README.md +0 -235
  27. package/dist/esm/shared/utils.js +0 -0
  28. package/dist/esm/sync/README.md +0 -215
  29. package/dist/esm/sync/clients/base-client.js +0 -508
  30. package/dist/esm/sync/redis-ws-client.js +0 -225
  31. package/dist/esm/utils/README.md +0 -89
  32. package/dist/esm/vault/README.md +0 -268
  33. package/dist/types/shared/utils.d.ts +0 -0
  34. package/dist/types/sync/clients/base-client.d.ts +0 -56
  35. package/dist/types/sync/redis-ws-client.d.ts +0 -21
@@ -8,35 +8,6 @@ Object.defineProperty(exports, "ActivitySyncClient", {
8
8
  return ActivitySyncClient;
9
9
  }
10
10
  });
11
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
12
- try {
13
- var info = gen[key](arg);
14
- var value = info.value;
15
- } catch (error) {
16
- reject(error);
17
- return;
18
- }
19
- if (info.done) {
20
- resolve(value);
21
- } else {
22
- Promise.resolve(value).then(_next, _throw);
23
- }
24
- }
25
- function _async_to_generator(fn) {
26
- return function() {
27
- var self = this, args = arguments;
28
- return new Promise(function(resolve, reject) {
29
- var gen = fn.apply(self, args);
30
- function _next(value) {
31
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
32
- }
33
- function _throw(err) {
34
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
35
- }
36
- _next(undefined);
37
- });
38
- };
39
- }
40
11
  function _class_call_check(instance, Constructor) {
41
12
  if (!(instance instanceof Constructor)) {
42
13
  throw new TypeError("Cannot call a class as a function");
@@ -84,116 +55,20 @@ function _object_spread(target) {
84
55
  }
85
56
  return target;
86
57
  }
87
- function _ts_generator(thisArg, body) {
88
- var f, y, t, _ = {
89
- label: 0,
90
- sent: function() {
91
- if (t[0] & 1) throw t[1];
92
- return t[1];
93
- },
94
- trys: [],
95
- ops: []
96
- }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
97
- return d(g, "next", {
98
- value: verb(0)
99
- }), d(g, "throw", {
100
- value: verb(1)
101
- }), d(g, "return", {
102
- value: verb(2)
103
- }), typeof Symbol === "function" && d(g, Symbol.iterator, {
104
- value: function() {
105
- return this;
106
- }
107
- }), g;
108
- function verb(n) {
109
- return function(v) {
110
- return step([
111
- n,
112
- v
113
- ]);
114
- };
115
- }
116
- function step(op) {
117
- if (f) throw new TypeError("Generator is already executing.");
118
- while(g && (g = 0, op[0] && (_ = 0)), _)try {
119
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
120
- if (y = 0, t) op = [
121
- op[0] & 2,
122
- t.value
123
- ];
124
- switch(op[0]){
125
- case 0:
126
- case 1:
127
- t = op;
128
- break;
129
- case 4:
130
- _.label++;
131
- return {
132
- value: op[1],
133
- done: false
134
- };
135
- case 5:
136
- _.label++;
137
- y = op[1];
138
- op = [
139
- 0
140
- ];
141
- continue;
142
- case 7:
143
- op = _.ops.pop();
144
- _.trys.pop();
145
- continue;
146
- default:
147
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
148
- _ = 0;
149
- continue;
150
- }
151
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
152
- _.label = op[1];
153
- break;
154
- }
155
- if (op[0] === 6 && _.label < t[1]) {
156
- _.label = t[1];
157
- t = op;
158
- break;
159
- }
160
- if (t && _.label < t[2]) {
161
- _.label = t[2];
162
- _.ops.push(op);
163
- break;
164
- }
165
- if (t[2]) _.ops.pop();
166
- _.trys.pop();
167
- continue;
168
- }
169
- op = body.call(thisArg, _);
170
- } catch (e) {
171
- op = [
172
- 6,
173
- e
174
- ];
175
- y = 0;
176
- } finally{
177
- f = t = 0;
178
- }
179
- if (op[0] & 5) throw op[1];
180
- return {
181
- value: op[0] ? op[1] : void 0,
182
- done: true
183
- };
184
- }
185
- }
186
58
  /**
187
59
  * Order fill event from activity stream (published via PG logical replication from Ponder).
188
60
  *
189
61
  * Two sources:
190
62
  * - orders_matched:{marketId} channel: includes maker, taker, transactionHash, blockNumber
191
63
  * - user:{address} channel: does not include maker/taker (user already knows their address)
192
- */ /** Maker address (null for user channel messages) */ /** Taker address (null for user channel messages) */ /** Blockchain timestamp (seconds since epoch, null for user channel) */ /** Subscribe to fills for this market (orders_matched:marketId) */ /** Subscribe to fills for this user address (user_info:address) */ /**
193
- * Client for activity streams: order fills per market (orders_matched) and per user (user_info).
194
- * Connects to the same worker as MarketDepthSyncClient; use WORKER_MODES that include "activity".
64
+ */ /** Maker address (null for user channel messages) */ /** Taker address (null for user channel messages) */ /** Blockchain timestamp (seconds since epoch, null for user channel) */ /**
65
+ * Multi-market, multi-user activity sync client.
66
+ *
67
+ * One WebSocket handles all subscriptions. Use:
68
+ * - subscribeMarket(marketId) / unsubscribeMarket(marketId) for fill streams
69
+ * - subscribeUser(address) / unsubscribeUser(address) for user order updates
195
70
  *
196
- * No ordering or gap handling we accept every order_fill message as it arrives.
71
+ * All subscriptions are re-sent automatically on reconnect.
197
72
  */ var ActivitySyncClient = /*#__PURE__*/ function() {
198
73
  "use strict";
199
74
  function ActivitySyncClient(config) {
@@ -201,183 +76,329 @@ function _ts_generator(thisArg, body) {
201
76
  _define_property(this, "ws", null);
202
77
  _define_property(this, "config", void 0);
203
78
  _define_property(this, "status", "disconnected");
204
- _define_property(this, "fillListeners", new Set());
205
- _define_property(this, "userFillListeners", new Set());
206
- _define_property(this, "marketFillListeners", new Set());
207
- _define_property(this, "statusListeners", new Set());
208
79
  _define_property(this, "shouldBeConnected", false);
209
80
  _define_property(this, "reconnectAttempts", 0);
210
81
  _define_property(this, "reconnectTimeoutId", null);
211
82
  _define_property(this, "heartbeatIntervalId", null);
212
83
  _define_property(this, "heartbeatTimeoutId", null);
213
- _define_property(this, "lastPongTime", 0);
214
- _define_property(this, "visibilityChangeHandler", null);
215
- if (!config.marketId && !config.userAddress) {
216
- throw new Error("ActivitySyncClient requires at least one of marketId or userAddress");
217
- }
84
+ /** Active market subscriptions: marketId → Set of callbacks */ _define_property(this, "marketListeners", new Map());
85
+ /** Active user subscriptions: address → Set of callbacks */ _define_property(this, "userListeners", new Map());
86
+ /** Generic fill listeners (all markets) */ _define_property(this, "fillListeners", new Set());
87
+ /** Status listeners */ _define_property(this, "statusListeners", new Set());
218
88
  this.config = _object_spread({
219
- heartbeatIntervalMs: 30000,
89
+ heartbeatIntervalMs: 25000,
220
90
  heartbeatTimeoutMs: 10000,
221
- maxReconnectAttempts: Infinity,
222
91
  initialReconnectDelayMs: 1000,
223
92
  maxReconnectDelayMs: 30000
224
93
  }, config);
225
94
  }
226
95
  _create_class(ActivitySyncClient, [
227
96
  {
97
+ // ── Connection lifecycle ──────────────────────────────────────────────────
228
98
  key: "connect",
229
- value: /** Connects to the activity websocket and subscribes to market and/or user channels. */ function connect() {
230
- return _async_to_generator(function() {
231
- var _this, wsUrl;
232
- return _ts_generator(this, function(_state) {
233
- _this = this;
234
- this.clearReconnectTimeout();
235
- this.removeVisibilityChangeHandler();
236
- if (this.ws) {
237
- this.ws.onclose = null;
238
- this.ws.close();
239
- this.ws = null;
99
+ value: function connect() {
100
+ var _this = this;
101
+ var _this_ws, _this_ws1;
102
+ if (((_this_ws = this.ws) === null || _this_ws === void 0 ? void 0 : _this_ws.readyState) === WebSocket.OPEN || ((_this_ws1 = this.ws) === null || _this_ws1 === void 0 ? void 0 : _this_ws1.readyState) === WebSocket.CONNECTING) {
103
+ return Promise.resolve();
104
+ }
105
+ this.clearReconnectTimeout();
106
+ this.shouldBeConnected = true;
107
+ this.setStatus("connecting");
108
+ return new Promise(function(resolve, reject) {
109
+ var settled = false;
110
+ var done = function(err) {
111
+ if (settled) return;
112
+ settled = true;
113
+ err ? reject(err) : resolve();
114
+ };
115
+ try {
116
+ _this.ws = new WebSocket(_this.config.wsUrl);
117
+ } catch (e) {
118
+ done(e);
119
+ _this.scheduleReconnect();
120
+ return;
121
+ }
122
+ _this.ws.onopen = function() {
123
+ _this.reconnectAttempts = 0;
124
+ _this.resubscribeAll();
125
+ _this.startHeartbeat();
126
+ _this.setStatus("synced");
127
+ done();
128
+ };
129
+ _this.ws.onmessage = function(ev) {
130
+ return _this.handleMessage(ev);
131
+ };
132
+ _this.ws.onerror = function() {
133
+ done(new Error("ActivitySyncClient WebSocket error"));
134
+ };
135
+ _this.ws.onclose = function() {
136
+ _this.stopHeartbeat();
137
+ _this.setStatus("disconnected");
138
+ done(new Error("WebSocket closed"));
139
+ if (_this.shouldBeConnected) _this.scheduleReconnect();
140
+ };
141
+ });
142
+ }
143
+ },
144
+ {
145
+ key: "disconnect",
146
+ value: function disconnect() {
147
+ this.shouldBeConnected = false;
148
+ this.clearReconnectTimeout();
149
+ this.stopHeartbeat();
150
+ if (this.ws) {
151
+ this.ws.onclose = null;
152
+ try {
153
+ this.ws.close();
154
+ } catch (unused) {}
155
+ /* noop */ this.ws = null;
156
+ }
157
+ this.setStatus("disconnected");
158
+ }
159
+ },
160
+ {
161
+ key: "getStatus",
162
+ value: function getStatus() {
163
+ return this.status;
164
+ }
165
+ },
166
+ {
167
+ key: "isSynced",
168
+ value: function isSynced() {
169
+ return this.status === "synced";
170
+ }
171
+ },
172
+ {
173
+ // ── Market subscriptions ──────────────────────────────────────────────────
174
+ key: "subscribeMarket",
175
+ value: function subscribeMarket(marketId, callback) {
176
+ var _this = this;
177
+ if (!this.marketListeners.has(marketId)) {
178
+ this.marketListeners.set(marketId, new Set());
179
+ // First subscriber — send subscribe to server
180
+ this.send({
181
+ type: "subscribe_fills",
182
+ marketId: marketId
183
+ });
184
+ }
185
+ this.marketListeners.get(marketId).add(callback);
186
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) this.connect().catch(function() {});
187
+ return function() {
188
+ var set = _this.marketListeners.get(marketId);
189
+ if (!set) return;
190
+ set.delete(callback);
191
+ if (set.size === 0) {
192
+ _this.marketListeners.delete(marketId);
193
+ _this.send({
194
+ type: "unsubscribe_fills",
195
+ marketId: marketId
196
+ });
197
+ if (_this.marketListeners.size === 0 && _this.userListeners.size === 0) _this.disconnect();
198
+ }
199
+ };
200
+ }
201
+ },
202
+ {
203
+ // ── User subscriptions ────────────────────────────────────────────────────
204
+ key: "subscribeUser",
205
+ value: function subscribeUser(address, callback) {
206
+ var _this = this;
207
+ if (!this.userListeners.has(address)) {
208
+ this.userListeners.set(address, new Set());
209
+ this.send({
210
+ type: "subscribe_user",
211
+ address: address
212
+ });
213
+ }
214
+ this.userListeners.get(address).add(callback);
215
+ if (!this.ws || this.ws.readyState !== WebSocket.OPEN) this.connect().catch(function() {});
216
+ return function() {
217
+ var set = _this.userListeners.get(address);
218
+ if (!set) return;
219
+ set.delete(callback);
220
+ if (set.size === 0) {
221
+ _this.userListeners.delete(address);
222
+ _this.send({
223
+ type: "unsubscribe_user",
224
+ address: address
225
+ });
226
+ if (_this.marketListeners.size === 0 && _this.userListeners.size === 0) _this.disconnect();
227
+ }
228
+ };
229
+ }
230
+ },
231
+ {
232
+ // ── Generic listeners ─────────────────────────────────────────────────────
233
+ /** Fires for every fill event regardless of market or user channel. */ key: "onFill",
234
+ value: function onFill(callback) {
235
+ var _this = this;
236
+ this.fillListeners.add(callback);
237
+ return function() {
238
+ return _this.fillListeners.delete(callback);
239
+ };
240
+ }
241
+ },
242
+ {
243
+ key: "onStatus",
244
+ value: function onStatus(callback) {
245
+ var _this = this;
246
+ this.statusListeners.add(callback);
247
+ return function() {
248
+ return _this.statusListeners.delete(callback);
249
+ };
250
+ }
251
+ },
252
+ {
253
+ key: "send",
254
+ value: // ── Internal ──────────────────────────────────────────────────────────────
255
+ function send(msg) {
256
+ var _this_ws;
257
+ if (((_this_ws = this.ws) === null || _this_ws === void 0 ? void 0 : _this_ws.readyState) === WebSocket.OPEN) {
258
+ try {
259
+ this.ws.send(JSON.stringify(msg));
260
+ } catch (unused) {}
261
+ }
262
+ }
263
+ },
264
+ {
265
+ key: "resubscribeAll",
266
+ value: /* noop */ function resubscribeAll() {
267
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
268
+ try {
269
+ for(var _iterator = this.marketListeners.keys()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
270
+ var marketId = _step.value;
271
+ this.send({
272
+ type: "subscribe_fills",
273
+ marketId: marketId
274
+ });
275
+ }
276
+ } catch (err) {
277
+ _didIteratorError = true;
278
+ _iteratorError = err;
279
+ } finally{
280
+ try {
281
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
282
+ _iterator.return();
240
283
  }
241
- this.shouldBeConnected = true;
242
- this.setStatus("connecting");
243
- this.setupVisibilityChangeHandler();
244
- wsUrl = this.config.wsUrl;
245
- if (!wsUrl.startsWith("ws://") && !wsUrl.startsWith("wss://")) {
246
- throw new Error("Invalid WebSocket URL: ".concat(wsUrl));
284
+ } finally{
285
+ if (_didIteratorError) {
286
+ throw _iteratorError;
247
287
  }
248
- return [
249
- 2,
250
- new Promise(function(resolve, reject) {
251
- var settled = false;
252
- var expectedSubscriptions = new Set();
253
- if (_this.config.marketId) expectedSubscriptions.add("subscribed_fills");
254
- if (_this.config.userAddress) expectedSubscriptions.add("subscribed_user");
255
- var resolveOnce = function() {
256
- if (settled) return;
257
- settled = true;
258
- resolve();
259
- };
260
- var rejectOnce = function(error) {
261
- if (settled) return;
262
- settled = true;
263
- reject(error);
264
- };
265
- _this.ws = new WebSocket(wsUrl);
266
- _this.ws.onopen = function() {
267
- _this.reconnectAttempts = 0;
268
- _this.lastPongTime = Date.now();
269
- if (_this.config.marketId) {
270
- _this.ws.send(JSON.stringify({
271
- type: "subscribe_fills",
272
- marketId: _this.config.marketId
273
- }));
274
- }
275
- if (_this.config.userAddress) {
276
- _this.ws.send(JSON.stringify({
277
- type: "subscribe_user",
278
- address: _this.config.userAddress
279
- }));
280
- }
281
- _this.startHeartbeat();
282
- };
283
- _this.ws.onmessage = function(event) {
284
- try {
285
- var msg = JSON.parse(event.data);
286
- if (msg.type === "pong") {
287
- _this.lastPongTime = Date.now();
288
- _this.clearHeartbeatTimeout();
289
- return;
290
- }
291
- if (msg.type === "subscribed_fills" || msg.type === "subscribed_user") {
292
- expectedSubscriptions.delete(msg.type);
293
- if (expectedSubscriptions.size === 0) {
294
- _this.setStatus("synced");
295
- resolveOnce();
296
- }
297
- return;
298
- }
299
- if (msg.type === "unsubscribed_fills" || msg.type === "unsubscribed_user") {
300
- return;
301
- }
302
- if (msg.type === "error") {
303
- var error = new Error(typeof msg.message === "string" ? msg.message : "Activity websocket subscription failed");
304
- console.error("[ActivitySyncClient] error:", msg.message);
305
- if (!settled) {
306
- rejectOnce(error);
307
- _this.handleConnectionLost();
308
- }
309
- return;
310
- }
311
- // Accept fill events (no seq/gap checks)
312
- // "order_fill" comes from orders_matched:{marketId} channel
313
- // "fill" comes from user_info:{address} channel
314
- if (msg.type === "order_fill" || msg.type === "fill") {
315
- var _msg_marketId, _msg_maker, _msg_taker, _msg_makerAsset, _msg_takerAsset, _msg_optionTokenId, _msg_transactionHash, _msg_blockNumber, _msg_timestamp;
316
- var isFromUserChannel = msg.type === "fill";
317
- var isFromMarketChannel = msg.type === "order_fill";
318
- var fill = {
319
- type: "order_fill",
320
- marketId: (_msg_marketId = msg.marketId) !== null && _msg_marketId !== void 0 ? _msg_marketId : null,
321
- orderHash: msg.orderHash,
322
- maker: (_msg_maker = msg.maker) !== null && _msg_maker !== void 0 ? _msg_maker : null,
323
- taker: (_msg_taker = msg.taker) !== null && _msg_taker !== void 0 ? _msg_taker : null,
324
- makerAsset: (_msg_makerAsset = msg.makerAsset) !== null && _msg_makerAsset !== void 0 ? _msg_makerAsset : "",
325
- takerAsset: (_msg_takerAsset = msg.takerAsset) !== null && _msg_takerAsset !== void 0 ? _msg_takerAsset : "",
326
- makingAmount: msg.makingAmount,
327
- takingAmount: msg.takingAmount,
328
- optionTokenId: (_msg_optionTokenId = msg.optionTokenId) !== null && _msg_optionTokenId !== void 0 ? _msg_optionTokenId : null,
329
- transactionHash: (_msg_transactionHash = msg.transactionHash) !== null && _msg_transactionHash !== void 0 ? _msg_transactionHash : null,
330
- blockNumber: (_msg_blockNumber = msg.blockNumber) !== null && _msg_blockNumber !== void 0 ? _msg_blockNumber : null,
331
- timestamp: (_msg_timestamp = msg.timestamp) !== null && _msg_timestamp !== void 0 ? _msg_timestamp : null
332
- };
333
- // Notify generic fill listeners (all events)
334
- _this.fillListeners.forEach(function(listener) {
335
- try {
336
- listener(fill);
337
- } catch (e) {
338
- console.error("Error in fill listener:", e);
339
- }
340
- });
341
- // Notify user-specific listeners (only events from user_info channel)
342
- if (isFromUserChannel) {
343
- _this.userFillListeners.forEach(function(listener) {
344
- try {
345
- listener(fill);
346
- } catch (e) {
347
- console.error("Error in user fill listener:", e);
348
- }
349
- });
350
- }
351
- // Notify market-specific listeners (only events from orders_matched channel)
352
- if (isFromMarketChannel) {
353
- _this.marketFillListeners.forEach(function(listener) {
354
- try {
355
- listener(fill);
356
- } catch (e) {
357
- console.error("Error in market fill listener:", e);
358
- }
359
- });
360
- }
361
- }
362
- } catch (e) {
363
- console.error("Error parsing activity message:", e);
364
- }
365
- };
366
- _this.ws.onerror = function(err) {
367
- if (_this.status === "connecting") rejectOnce(err);
368
- };
369
- _this.ws.onclose = function() {
370
- _this.stopHeartbeat();
371
- _this.setStatus("disconnected");
372
- if (!settled) {
373
- rejectOnce(new Error("WebSocket closed before subscriptions were acknowledged"));
374
- }
375
- if (_this.shouldBeConnected) _this.scheduleReconnect();
376
- };
377
- })
378
- ];
288
+ }
289
+ }
290
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
291
+ try {
292
+ for(var _iterator1 = this.userListeners.keys()[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
293
+ var address = _step1.value;
294
+ this.send({
295
+ type: "subscribe_user",
296
+ address: address
297
+ });
298
+ }
299
+ } catch (err) {
300
+ _didIteratorError1 = true;
301
+ _iteratorError1 = err;
302
+ } finally{
303
+ try {
304
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
305
+ _iterator1.return();
306
+ }
307
+ } finally{
308
+ if (_didIteratorError1) {
309
+ throw _iteratorError1;
310
+ }
311
+ }
312
+ }
313
+ }
314
+ },
315
+ {
316
+ key: "handleMessage",
317
+ value: function handleMessage(ev) {
318
+ var msg;
319
+ try {
320
+ msg = JSON.parse(ev.data);
321
+ } catch (unused) {
322
+ return;
323
+ }
324
+ var type = msg.type;
325
+ if (type === "pong") {
326
+ this.clearHeartbeatTimeout();
327
+ return;
328
+ }
329
+ if (type === "order_fill") {
330
+ var fill = this.normalizeFill(msg);
331
+ this.fillListeners.forEach(function(cb) {
332
+ try {
333
+ cb(fill);
334
+ } catch (unused) {}
379
335
  });
380
- }).call(this);
336
+ /* noop */ var mid = fill.marketId;
337
+ if (mid) {
338
+ var _this_marketListeners_get;
339
+ (_this_marketListeners_get = this.marketListeners.get(mid)) === null || _this_marketListeners_get === void 0 ? void 0 : _this_marketListeners_get.forEach(function(cb) {
340
+ try {
341
+ cb(fill);
342
+ } catch (unused) {}
343
+ });
344
+ }
345
+ /* noop */ return;
346
+ }
347
+ if (type === "fill" || type === "order_update") {
348
+ var _$ev = _object_spread({
349
+ type: type,
350
+ orderHash: msg.orderHash,
351
+ marketId: msg.marketId,
352
+ tokenId: msg.tokenId,
353
+ status: msg.status,
354
+ remainingAmount: msg.remainingAmount
355
+ }, msg);
356
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
357
+ try {
358
+ for(var _iterator = this.userListeners.values()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
359
+ var cbs = _step.value;
360
+ cbs.forEach(function(cb) {
361
+ try {
362
+ cb(_$ev);
363
+ } catch (unused) {}
364
+ });
365
+ }
366
+ } catch (err) {
367
+ _didIteratorError = true;
368
+ _iteratorError = err;
369
+ } finally{
370
+ try {
371
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
372
+ _iterator.return();
373
+ }
374
+ } finally{
375
+ if (_didIteratorError) {
376
+ throw _iteratorError;
377
+ }
378
+ }
379
+ }
380
+ }
381
+ }
382
+ },
383
+ {
384
+ key: "normalizeFill",
385
+ value: /* noop */ function normalizeFill(msg) {
386
+ var _msg_marketId, _msg_maker, _msg_taker, _msg_makerAsset, _msg_takerAsset, _msg_optionTokenId, _msg_transactionHash, _msg_blockNumber, _msg_timestamp;
387
+ return {
388
+ type: "order_fill",
389
+ marketId: (_msg_marketId = msg.marketId) !== null && _msg_marketId !== void 0 ? _msg_marketId : null,
390
+ orderHash: msg.orderHash,
391
+ maker: (_msg_maker = msg.maker) !== null && _msg_maker !== void 0 ? _msg_maker : null,
392
+ taker: (_msg_taker = msg.taker) !== null && _msg_taker !== void 0 ? _msg_taker : null,
393
+ makerAsset: (_msg_makerAsset = msg.makerAsset) !== null && _msg_makerAsset !== void 0 ? _msg_makerAsset : "",
394
+ takerAsset: (_msg_takerAsset = msg.takerAsset) !== null && _msg_takerAsset !== void 0 ? _msg_takerAsset : "",
395
+ makingAmount: msg.makingAmount,
396
+ takingAmount: msg.takingAmount,
397
+ optionTokenId: (_msg_optionTokenId = msg.optionTokenId) !== null && _msg_optionTokenId !== void 0 ? _msg_optionTokenId : null,
398
+ transactionHash: (_msg_transactionHash = msg.transactionHash) !== null && _msg_transactionHash !== void 0 ? _msg_transactionHash : null,
399
+ blockNumber: (_msg_blockNumber = msg.blockNumber) !== null && _msg_blockNumber !== void 0 ? _msg_blockNumber : null,
400
+ timestamp: (_msg_timestamp = msg.timestamp) !== null && _msg_timestamp !== void 0 ? _msg_timestamp : null
401
+ };
381
402
  }
382
403
  },
383
404
  {
@@ -386,18 +407,12 @@ function _ts_generator(thisArg, body) {
386
407
  var _this = this;
387
408
  this.stopHeartbeat();
388
409
  this.heartbeatIntervalId = setInterval(function() {
389
- if (!_this.ws || _this.ws.readyState !== WebSocket.OPEN) return;
390
- try {
391
- _this.ws.send(JSON.stringify({
392
- type: "ping"
393
- }));
394
- _this.clearHeartbeatTimeout();
395
- _this.heartbeatTimeoutId = setTimeout(function() {
396
- _this.handleConnectionLost();
397
- }, _this.config.heartbeatTimeoutMs);
398
- } catch (unused) {
410
+ _this.send({
411
+ type: "ping"
412
+ });
413
+ _this.heartbeatTimeoutId = setTimeout(function() {
399
414
  _this.handleConnectionLost();
400
- }
415
+ }, _this.config.heartbeatTimeoutMs);
401
416
  }, this.config.heartbeatIntervalMs);
402
417
  }
403
418
  },
@@ -429,59 +444,16 @@ function _ts_generator(thisArg, body) {
429
444
  }
430
445
  }
431
446
  },
432
- {
433
- key: "setupVisibilityChangeHandler",
434
- value: function setupVisibilityChangeHandler() {
435
- var _this = this;
436
- if (typeof document === "undefined") return;
437
- this.removeVisibilityChangeHandler();
438
- this.visibilityChangeHandler = function() {
439
- if (document.visibilityState === "visible" && _this.shouldBeConnected) {
440
- _this.checkConnectionHealth();
441
- }
442
- };
443
- document.addEventListener("visibilitychange", this.visibilityChangeHandler);
444
- }
445
- },
446
- {
447
- key: "removeVisibilityChangeHandler",
448
- value: function removeVisibilityChangeHandler() {
449
- if (typeof document === "undefined") return;
450
- if (this.visibilityChangeHandler) {
451
- document.removeEventListener("visibilitychange", this.visibilityChangeHandler);
452
- this.visibilityChangeHandler = null;
453
- }
454
- }
455
- },
456
- {
457
- key: "checkConnectionHealth",
458
- value: function checkConnectionHealth() {
459
- var _this = this;
460
- if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
461
- if (this.shouldBeConnected) this.handleConnectionLost();
462
- return;
463
- }
464
- try {
465
- this.ws.send(JSON.stringify({
466
- type: "ping"
467
- }));
468
- this.clearHeartbeatTimeout();
469
- this.heartbeatTimeoutId = setTimeout(function() {
470
- _this.handleConnectionLost();
471
- }, this.config.heartbeatTimeoutMs);
472
- } catch (unused) {
473
- this.handleConnectionLost();
474
- }
475
- }
476
- },
477
447
  {
478
448
  key: "handleConnectionLost",
479
449
  value: function handleConnectionLost() {
480
450
  this.stopHeartbeat();
481
451
  if (this.ws) {
482
452
  this.ws.onclose = null;
483
- this.ws.close();
484
- this.ws = null;
453
+ try {
454
+ this.ws.close();
455
+ } catch (unused) {}
456
+ /* noop */ this.ws = null;
485
457
  }
486
458
  this.setStatus("disconnected");
487
459
  if (this.shouldBeConnected) this.scheduleReconnect();
@@ -491,12 +463,6 @@ function _ts_generator(thisArg, body) {
491
463
  key: "scheduleReconnect",
492
464
  value: function scheduleReconnect() {
493
465
  var _this = this;
494
- if (!this.shouldBeConnected) return;
495
- var max = this.config.maxReconnectAttempts;
496
- if (this.reconnectAttempts >= max) {
497
- console.error("[ActivitySyncClient] Max reconnect attempts (".concat(max, ") reached"));
498
- return;
499
- }
500
466
  var base = this.config.initialReconnectDelayMs;
501
467
  var cap = this.config.maxReconnectDelayMs;
502
468
  var delay = Math.min(base * Math.pow(2, this.reconnectAttempts) + Math.random() * 1000, cap);
@@ -504,9 +470,7 @@ function _ts_generator(thisArg, body) {
504
470
  this.setStatus("recovering");
505
471
  this.clearReconnectTimeout();
506
472
  this.reconnectTimeoutId = setTimeout(function() {
507
- _this.connect().catch(function(e) {
508
- return console.error("[ActivitySyncClient] Reconnect failed:", e);
509
- });
473
+ _this.connect().catch(function() {});
510
474
  }, delay);
511
475
  }
512
476
  },
@@ -518,103 +482,10 @@ function _ts_generator(thisArg, body) {
518
482
  this.statusListeners.forEach(function(cb) {
519
483
  try {
520
484
  cb(status);
521
- } catch (e) {
522
- console.error("Error in status listener:", e);
523
- }
485
+ } catch (unused) {}
524
486
  });
525
487
  }
526
- },
527
- {
528
- key: "getStatus",
529
- value: function getStatus() {
530
- return this.status;
531
- }
532
- },
533
- {
534
- /** Returns true once the websocket has an active subscription. */ key: "isSynced",
535
- value: function isSynced() {
536
- return this.status === "synced";
537
- }
538
- },
539
- {
540
- /** Registers a listener for connection lifecycle updates. */ key: "onStatus",
541
- value: function onStatus(callback) {
542
- var _this = this;
543
- this.statusListeners.add(callback);
544
- return function() {
545
- return _this.statusListeners.delete(callback);
546
- };
547
- }
548
- },
549
- {
550
- /** Subscribes to all normalized fill events regardless of source channel. */ key: "onOrderFill",
551
- value: function onOrderFill(callback) {
552
- var _this = this;
553
- this.fillListeners.add(callback);
554
- return function() {
555
- return _this.fillListeners.delete(callback);
556
- };
557
- }
558
- },
559
- {
560
- /** Subscribe to fills from user_info channel only (user's orders being filled) */ key: "onUserFill",
561
- value: function onUserFill(callback) {
562
- var _this = this;
563
- this.userFillListeners.add(callback);
564
- return function() {
565
- return _this.userFillListeners.delete(callback);
566
- };
567
- }
568
- },
569
- {
570
- /** Subscribe to fills from orders_matched channel only (market activity) */ key: "onMarketFill",
571
- value: function onMarketFill(callback) {
572
- var _this = this;
573
- this.marketFillListeners.add(callback);
574
- return function() {
575
- return _this.marketFillListeners.delete(callback);
576
- };
577
- }
578
- },
579
- {
580
- key: "disconnect",
581
- value: /** Closes the websocket, unsubscribes active channels, and stops reconnects. */ function disconnect() {
582
- return _async_to_generator(function() {
583
- return _ts_generator(this, function(_state) {
584
- this.shouldBeConnected = false;
585
- this.clearReconnectTimeout();
586
- this.removeVisibilityChangeHandler();
587
- this.stopHeartbeat();
588
- this.reconnectAttempts = 0;
589
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
590
- if (this.config.marketId) {
591
- try {
592
- this.ws.send(JSON.stringify({
593
- type: "unsubscribe_fills",
594
- marketId: this.config.marketId
595
- }));
596
- } catch (unused) {}
597
- }
598
- /* ignore */ if (this.config.userAddress) {
599
- try {
600
- this.ws.send(JSON.stringify({
601
- type: "unsubscribe_user",
602
- address: this.config.userAddress
603
- }));
604
- } catch (unused) {}
605
- }
606
- /* ignore */ this.ws.onclose = null;
607
- this.ws.close();
608
- }
609
- this.ws = null;
610
- this.setStatus("disconnected");
611
- return [
612
- 2
613
- ];
614
- });
615
- }).call(this);
616
- }
617
488
  }
618
489
  ]);
619
490
  return ActivitySyncClient;
620
- }();
491
+ } /* noop */ ();