@stryke-xyz/premarket-sdk 1.2.1 → 1.2.2

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