@agg-build/sdk 4.17.4 → 4.18.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/dist/agg-websocket-BatL1Zrt.d.mts +4320 -0
- package/dist/agg-websocket-BatL1Zrt.d.ts +4320 -0
- package/dist/browser.d.mts +69 -0
- package/dist/browser.d.ts +69 -0
- package/dist/browser.js +2049 -0
- package/dist/browser.mjs +804 -0
- package/dist/chunk-QXBVA5TW.mjs +2073 -0
- package/dist/chunk-VD5UTAVD.mjs +158 -0
- package/dist/index.d.mts +431 -4249
- package/dist/index.d.ts +431 -4249
- package/dist/index.js +705 -114
- package/dist/index.mjs +244 -1752
- package/dist/market-data.d.mts +7 -0
- package/dist/market-data.d.ts +7 -0
- package/dist/market-data.js +1776 -0
- package/dist/market-data.mjs +352 -0
- package/package.json +27 -1
package/dist/browser.mjs
ADDED
|
@@ -0,0 +1,804 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AggWebSocket
|
|
3
|
+
} from "./chunk-QXBVA5TW.mjs";
|
|
4
|
+
import {
|
|
5
|
+
__async,
|
|
6
|
+
__objRest,
|
|
7
|
+
__spreadProps,
|
|
8
|
+
__spreadValues
|
|
9
|
+
} from "./chunk-WNQUEZJF.mjs";
|
|
10
|
+
|
|
11
|
+
// ../../node_modules/.pnpm/comlink@4.4.2/node_modules/comlink/dist/esm/comlink.mjs
|
|
12
|
+
var proxyMarker = /* @__PURE__ */ Symbol("Comlink.proxy");
|
|
13
|
+
var createEndpoint = /* @__PURE__ */ Symbol("Comlink.endpoint");
|
|
14
|
+
var releaseProxy = /* @__PURE__ */ Symbol("Comlink.releaseProxy");
|
|
15
|
+
var finalizer = /* @__PURE__ */ Symbol("Comlink.finalizer");
|
|
16
|
+
var throwMarker = /* @__PURE__ */ Symbol("Comlink.thrown");
|
|
17
|
+
var isObject = (val) => typeof val === "object" && val !== null || typeof val === "function";
|
|
18
|
+
var proxyTransferHandler = {
|
|
19
|
+
canHandle: (val) => isObject(val) && val[proxyMarker],
|
|
20
|
+
serialize(obj) {
|
|
21
|
+
const { port1, port2 } = new MessageChannel();
|
|
22
|
+
expose(obj, port1);
|
|
23
|
+
return [port2, [port2]];
|
|
24
|
+
},
|
|
25
|
+
deserialize(port) {
|
|
26
|
+
port.start();
|
|
27
|
+
return wrap(port);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var throwTransferHandler = {
|
|
31
|
+
canHandle: (value) => isObject(value) && throwMarker in value,
|
|
32
|
+
serialize({ value }) {
|
|
33
|
+
let serialized;
|
|
34
|
+
if (value instanceof Error) {
|
|
35
|
+
serialized = {
|
|
36
|
+
isError: true,
|
|
37
|
+
value: {
|
|
38
|
+
message: value.message,
|
|
39
|
+
name: value.name,
|
|
40
|
+
stack: value.stack
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
} else {
|
|
44
|
+
serialized = { isError: false, value };
|
|
45
|
+
}
|
|
46
|
+
return [serialized, []];
|
|
47
|
+
},
|
|
48
|
+
deserialize(serialized) {
|
|
49
|
+
if (serialized.isError) {
|
|
50
|
+
throw Object.assign(new Error(serialized.value.message), serialized.value);
|
|
51
|
+
}
|
|
52
|
+
throw serialized.value;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
var transferHandlers = /* @__PURE__ */ new Map([
|
|
56
|
+
["proxy", proxyTransferHandler],
|
|
57
|
+
["throw", throwTransferHandler]
|
|
58
|
+
]);
|
|
59
|
+
function isAllowedOrigin(allowedOrigins, origin) {
|
|
60
|
+
for (const allowedOrigin of allowedOrigins) {
|
|
61
|
+
if (origin === allowedOrigin || allowedOrigin === "*") {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
if (allowedOrigin instanceof RegExp && allowedOrigin.test(origin)) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
function expose(obj, ep = globalThis, allowedOrigins = ["*"]) {
|
|
71
|
+
ep.addEventListener("message", function callback(ev) {
|
|
72
|
+
if (!ev || !ev.data) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (!isAllowedOrigin(allowedOrigins, ev.origin)) {
|
|
76
|
+
console.warn(`Invalid origin '${ev.origin}' for comlink proxy`);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const { id, type, path } = Object.assign({ path: [] }, ev.data);
|
|
80
|
+
const argumentList = (ev.data.argumentList || []).map(fromWireValue);
|
|
81
|
+
let returnValue;
|
|
82
|
+
try {
|
|
83
|
+
const parent = path.slice(0, -1).reduce((obj2, prop) => obj2[prop], obj);
|
|
84
|
+
const rawValue = path.reduce((obj2, prop) => obj2[prop], obj);
|
|
85
|
+
switch (type) {
|
|
86
|
+
case "GET":
|
|
87
|
+
{
|
|
88
|
+
returnValue = rawValue;
|
|
89
|
+
}
|
|
90
|
+
break;
|
|
91
|
+
case "SET":
|
|
92
|
+
{
|
|
93
|
+
parent[path.slice(-1)[0]] = fromWireValue(ev.data.value);
|
|
94
|
+
returnValue = true;
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
case "APPLY":
|
|
98
|
+
{
|
|
99
|
+
returnValue = rawValue.apply(parent, argumentList);
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
case "CONSTRUCT":
|
|
103
|
+
{
|
|
104
|
+
const value = new rawValue(...argumentList);
|
|
105
|
+
returnValue = proxy(value);
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
case "ENDPOINT":
|
|
109
|
+
{
|
|
110
|
+
const { port1, port2 } = new MessageChannel();
|
|
111
|
+
expose(obj, port2);
|
|
112
|
+
returnValue = transfer(port1, [port1]);
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
case "RELEASE":
|
|
116
|
+
{
|
|
117
|
+
returnValue = void 0;
|
|
118
|
+
}
|
|
119
|
+
break;
|
|
120
|
+
default:
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
} catch (value) {
|
|
124
|
+
returnValue = { value, [throwMarker]: 0 };
|
|
125
|
+
}
|
|
126
|
+
Promise.resolve(returnValue).catch((value) => {
|
|
127
|
+
return { value, [throwMarker]: 0 };
|
|
128
|
+
}).then((returnValue2) => {
|
|
129
|
+
const [wireValue, transferables] = toWireValue(returnValue2);
|
|
130
|
+
ep.postMessage(Object.assign(Object.assign({}, wireValue), { id }), transferables);
|
|
131
|
+
if (type === "RELEASE") {
|
|
132
|
+
ep.removeEventListener("message", callback);
|
|
133
|
+
closeEndPoint(ep);
|
|
134
|
+
if (finalizer in obj && typeof obj[finalizer] === "function") {
|
|
135
|
+
obj[finalizer]();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}).catch((error) => {
|
|
139
|
+
const [wireValue, transferables] = toWireValue({
|
|
140
|
+
value: new TypeError("Unserializable return value"),
|
|
141
|
+
[throwMarker]: 0
|
|
142
|
+
});
|
|
143
|
+
ep.postMessage(Object.assign(Object.assign({}, wireValue), { id }), transferables);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
if (ep.start) {
|
|
147
|
+
ep.start();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function isMessagePort(endpoint) {
|
|
151
|
+
return endpoint.constructor.name === "MessagePort";
|
|
152
|
+
}
|
|
153
|
+
function closeEndPoint(endpoint) {
|
|
154
|
+
if (isMessagePort(endpoint))
|
|
155
|
+
endpoint.close();
|
|
156
|
+
}
|
|
157
|
+
function wrap(ep, target) {
|
|
158
|
+
const pendingListeners = /* @__PURE__ */ new Map();
|
|
159
|
+
ep.addEventListener("message", function handleMessage(ev) {
|
|
160
|
+
const { data } = ev;
|
|
161
|
+
if (!data || !data.id) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const resolver = pendingListeners.get(data.id);
|
|
165
|
+
if (!resolver) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
resolver(data);
|
|
170
|
+
} finally {
|
|
171
|
+
pendingListeners.delete(data.id);
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
return createProxy(ep, pendingListeners, [], target);
|
|
175
|
+
}
|
|
176
|
+
function throwIfProxyReleased(isReleased) {
|
|
177
|
+
if (isReleased) {
|
|
178
|
+
throw new Error("Proxy has been released and is not useable");
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function releaseEndpoint(ep) {
|
|
182
|
+
return requestResponseMessage(ep, /* @__PURE__ */ new Map(), {
|
|
183
|
+
type: "RELEASE"
|
|
184
|
+
}).then(() => {
|
|
185
|
+
closeEndPoint(ep);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
var proxyCounter = /* @__PURE__ */ new WeakMap();
|
|
189
|
+
var proxyFinalizers = "FinalizationRegistry" in globalThis && new FinalizationRegistry((ep) => {
|
|
190
|
+
const newCount = (proxyCounter.get(ep) || 0) - 1;
|
|
191
|
+
proxyCounter.set(ep, newCount);
|
|
192
|
+
if (newCount === 0) {
|
|
193
|
+
releaseEndpoint(ep);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
function registerProxy(proxy2, ep) {
|
|
197
|
+
const newCount = (proxyCounter.get(ep) || 0) + 1;
|
|
198
|
+
proxyCounter.set(ep, newCount);
|
|
199
|
+
if (proxyFinalizers) {
|
|
200
|
+
proxyFinalizers.register(proxy2, ep, proxy2);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function unregisterProxy(proxy2) {
|
|
204
|
+
if (proxyFinalizers) {
|
|
205
|
+
proxyFinalizers.unregister(proxy2);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function createProxy(ep, pendingListeners, path = [], target = function() {
|
|
209
|
+
}) {
|
|
210
|
+
let isProxyReleased = false;
|
|
211
|
+
const proxy2 = new Proxy(target, {
|
|
212
|
+
get(_target, prop) {
|
|
213
|
+
throwIfProxyReleased(isProxyReleased);
|
|
214
|
+
if (prop === releaseProxy) {
|
|
215
|
+
return () => {
|
|
216
|
+
unregisterProxy(proxy2);
|
|
217
|
+
releaseEndpoint(ep);
|
|
218
|
+
pendingListeners.clear();
|
|
219
|
+
isProxyReleased = true;
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (prop === "then") {
|
|
223
|
+
if (path.length === 0) {
|
|
224
|
+
return { then: () => proxy2 };
|
|
225
|
+
}
|
|
226
|
+
const r = requestResponseMessage(ep, pendingListeners, {
|
|
227
|
+
type: "GET",
|
|
228
|
+
path: path.map((p) => p.toString())
|
|
229
|
+
}).then(fromWireValue);
|
|
230
|
+
return r.then.bind(r);
|
|
231
|
+
}
|
|
232
|
+
return createProxy(ep, pendingListeners, [...path, prop]);
|
|
233
|
+
},
|
|
234
|
+
set(_target, prop, rawValue) {
|
|
235
|
+
throwIfProxyReleased(isProxyReleased);
|
|
236
|
+
const [value, transferables] = toWireValue(rawValue);
|
|
237
|
+
return requestResponseMessage(ep, pendingListeners, {
|
|
238
|
+
type: "SET",
|
|
239
|
+
path: [...path, prop].map((p) => p.toString()),
|
|
240
|
+
value
|
|
241
|
+
}, transferables).then(fromWireValue);
|
|
242
|
+
},
|
|
243
|
+
apply(_target, _thisArg, rawArgumentList) {
|
|
244
|
+
throwIfProxyReleased(isProxyReleased);
|
|
245
|
+
const last = path[path.length - 1];
|
|
246
|
+
if (last === createEndpoint) {
|
|
247
|
+
return requestResponseMessage(ep, pendingListeners, {
|
|
248
|
+
type: "ENDPOINT"
|
|
249
|
+
}).then(fromWireValue);
|
|
250
|
+
}
|
|
251
|
+
if (last === "bind") {
|
|
252
|
+
return createProxy(ep, pendingListeners, path.slice(0, -1));
|
|
253
|
+
}
|
|
254
|
+
const [argumentList, transferables] = processArguments(rawArgumentList);
|
|
255
|
+
return requestResponseMessage(ep, pendingListeners, {
|
|
256
|
+
type: "APPLY",
|
|
257
|
+
path: path.map((p) => p.toString()),
|
|
258
|
+
argumentList
|
|
259
|
+
}, transferables).then(fromWireValue);
|
|
260
|
+
},
|
|
261
|
+
construct(_target, rawArgumentList) {
|
|
262
|
+
throwIfProxyReleased(isProxyReleased);
|
|
263
|
+
const [argumentList, transferables] = processArguments(rawArgumentList);
|
|
264
|
+
return requestResponseMessage(ep, pendingListeners, {
|
|
265
|
+
type: "CONSTRUCT",
|
|
266
|
+
path: path.map((p) => p.toString()),
|
|
267
|
+
argumentList
|
|
268
|
+
}, transferables).then(fromWireValue);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
registerProxy(proxy2, ep);
|
|
272
|
+
return proxy2;
|
|
273
|
+
}
|
|
274
|
+
function myFlat(arr) {
|
|
275
|
+
return Array.prototype.concat.apply([], arr);
|
|
276
|
+
}
|
|
277
|
+
function processArguments(argumentList) {
|
|
278
|
+
const processed = argumentList.map(toWireValue);
|
|
279
|
+
return [processed.map((v) => v[0]), myFlat(processed.map((v) => v[1]))];
|
|
280
|
+
}
|
|
281
|
+
var transferCache = /* @__PURE__ */ new WeakMap();
|
|
282
|
+
function transfer(obj, transfers) {
|
|
283
|
+
transferCache.set(obj, transfers);
|
|
284
|
+
return obj;
|
|
285
|
+
}
|
|
286
|
+
function proxy(obj) {
|
|
287
|
+
return Object.assign(obj, { [proxyMarker]: true });
|
|
288
|
+
}
|
|
289
|
+
function toWireValue(value) {
|
|
290
|
+
for (const [name, handler] of transferHandlers) {
|
|
291
|
+
if (handler.canHandle(value)) {
|
|
292
|
+
const [serializedValue, transferables] = handler.serialize(value);
|
|
293
|
+
return [
|
|
294
|
+
{
|
|
295
|
+
type: "HANDLER",
|
|
296
|
+
name,
|
|
297
|
+
value: serializedValue
|
|
298
|
+
},
|
|
299
|
+
transferables
|
|
300
|
+
];
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return [
|
|
304
|
+
{
|
|
305
|
+
type: "RAW",
|
|
306
|
+
value
|
|
307
|
+
},
|
|
308
|
+
transferCache.get(value) || []
|
|
309
|
+
];
|
|
310
|
+
}
|
|
311
|
+
function fromWireValue(value) {
|
|
312
|
+
switch (value.type) {
|
|
313
|
+
case "HANDLER":
|
|
314
|
+
return transferHandlers.get(value.name).deserialize(value.value);
|
|
315
|
+
case "RAW":
|
|
316
|
+
return value.value;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function requestResponseMessage(ep, pendingListeners, msg, transfers) {
|
|
320
|
+
return new Promise((resolve) => {
|
|
321
|
+
const id = generateUUID();
|
|
322
|
+
pendingListeners.set(id, resolve);
|
|
323
|
+
if (ep.start) {
|
|
324
|
+
ep.start();
|
|
325
|
+
}
|
|
326
|
+
ep.postMessage(Object.assign({ id }, msg), transfers);
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
function generateUUID() {
|
|
330
|
+
return new Array(4).fill(0).map(() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16)).join("-");
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// src/market-data/worker-adapter.ts
|
|
334
|
+
var WorkerAggWebSocket = class extends AggWebSocket {
|
|
335
|
+
constructor(config, factory) {
|
|
336
|
+
var _a, _b;
|
|
337
|
+
super(config);
|
|
338
|
+
this.config = config;
|
|
339
|
+
this.endpointListeners = /* @__PURE__ */ new Map();
|
|
340
|
+
this.historyOperations = 0;
|
|
341
|
+
this.currentBooks = /* @__PURE__ */ new Map();
|
|
342
|
+
this.recovering = /* @__PURE__ */ new Set();
|
|
343
|
+
this.diagnosticRevisions = /* @__PURE__ */ new Map();
|
|
344
|
+
this.deliveredRevisions = /* @__PURE__ */ new Map();
|
|
345
|
+
this.resolvedRevisions = /* @__PURE__ */ new Map();
|
|
346
|
+
this.refs = /* @__PURE__ */ new Map();
|
|
347
|
+
this.arbListeners = /* @__PURE__ */ new Map();
|
|
348
|
+
this.feedListeners = /* @__PURE__ */ new Set();
|
|
349
|
+
this.latest = /* @__PURE__ */ new Map();
|
|
350
|
+
this.generation = 0;
|
|
351
|
+
this.online = false;
|
|
352
|
+
this.stopped = false;
|
|
353
|
+
this.queued = false;
|
|
354
|
+
this.historyFailures = /* @__PURE__ */ new Set();
|
|
355
|
+
this.fallback = null;
|
|
356
|
+
this.fallbackUnsubs = /* @__PURE__ */ new Map();
|
|
357
|
+
this.historyGeneration = 0;
|
|
358
|
+
this.visibility = () => {
|
|
359
|
+
if (document.visibilityState === "visible")
|
|
360
|
+
this.resnapshot(
|
|
361
|
+
[...this.refs.values()].filter((entry) => entry.sub.channel === "orderbook").map((entry) => entry.sub.id)
|
|
362
|
+
);
|
|
363
|
+
};
|
|
364
|
+
this.fail = () => {
|
|
365
|
+
var _a, _b, _c;
|
|
366
|
+
if (this.stopped || this.fallback) return;
|
|
367
|
+
this.releaseWorker();
|
|
368
|
+
this.currentBooks.clear();
|
|
369
|
+
this.latest.clear();
|
|
370
|
+
this.recovering.clear();
|
|
371
|
+
this.diagnosticRevisions.clear();
|
|
372
|
+
this.deliveredRevisions.clear();
|
|
373
|
+
this.resolvedRevisions.clear();
|
|
374
|
+
this.online = false;
|
|
375
|
+
(_b = (_a = this.handlers).onConnectionStateChange) == null ? void 0 : _b.call(_a, false);
|
|
376
|
+
this.fallback = new AggWebSocket(__spreadProps(__spreadValues({}, this.config), { callbacks: this.fallbackCallbacks() }));
|
|
377
|
+
for (const reject of this.historyFailures) reject(new Error("Market-data worker failed"));
|
|
378
|
+
this.historyFailures.clear();
|
|
379
|
+
(_c = this.resolveFailure) == null ? void 0 : _c.call(this);
|
|
380
|
+
this.syncFallback();
|
|
381
|
+
};
|
|
382
|
+
if (((_a = config.callbacks) == null ? void 0 : _a.onTrade) && !config.callbacks.onMarketView)
|
|
383
|
+
throw new Error(
|
|
384
|
+
"Raw trade callbacks require AggWebSocket; Worker views require onMarketView opt-in"
|
|
385
|
+
);
|
|
386
|
+
this.handlers = (_b = config.callbacks) != null ? _b : {};
|
|
387
|
+
this.worker = factory();
|
|
388
|
+
const worker = this.worker;
|
|
389
|
+
const endpoint = {
|
|
390
|
+
postMessage: worker.postMessage.bind(worker),
|
|
391
|
+
addEventListener: (type, listener) => {
|
|
392
|
+
this.endpointListeners.set(listener, type);
|
|
393
|
+
worker.addEventListener(type, listener);
|
|
394
|
+
},
|
|
395
|
+
removeEventListener: (type, listener) => {
|
|
396
|
+
this.endpointListeners.delete(listener);
|
|
397
|
+
worker.removeEventListener(type, listener);
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
this.remote = wrap(endpoint);
|
|
401
|
+
this.worker.addEventListener("error", this.fail);
|
|
402
|
+
this.worker.addEventListener("messageerror", this.fail);
|
|
403
|
+
const _c = config, { callbacks: _callbacks } = _c, options = __objRest(_c, ["callbacks"]);
|
|
404
|
+
const startup = this.remote.start(
|
|
405
|
+
options,
|
|
406
|
+
proxy((event) => {
|
|
407
|
+
if (!this.fallback) this.event(event.name, event.args, event.revision);
|
|
408
|
+
})
|
|
409
|
+
).then(() => {
|
|
410
|
+
if (this.stopped || this.fallback || !this.remote) return;
|
|
411
|
+
return this.remote.observeViews(
|
|
412
|
+
proxy((batch) => {
|
|
413
|
+
var _a2, _b2, _c2, _d, _e, _f, _g, _h, _i, _j;
|
|
414
|
+
if (batch.generation < this.generation || this.stopped || this.fallback) return;
|
|
415
|
+
for (const view of batch.views) {
|
|
416
|
+
if (!this.refs.has(`orderbook:${view.outcomeId}`) && !this.refs.has(`trades:${view.outcomeId}`))
|
|
417
|
+
continue;
|
|
418
|
+
if (((_a2 = this.resolvedRevisions.get(view.outcomeId)) != null ? _a2 : -1) >= view.revision) continue;
|
|
419
|
+
const integrity = ((_b2 = this.diagnosticRevisions.get(view.outcomeId)) != null ? _b2 : -1) > view.revision ? "resyncing" : view.integrity;
|
|
420
|
+
this.deliveredRevisions.set(view.outcomeId, view.revision);
|
|
421
|
+
(_d = (_c2 = this.handlers).onMarketView) == null ? void 0 : _d.call(_c2, __spreadProps(__spreadValues({}, view), { integrity }));
|
|
422
|
+
if (view.book) {
|
|
423
|
+
this.currentBooks.set(view.outcomeId, view.book);
|
|
424
|
+
if (integrity === "ok") {
|
|
425
|
+
this.recovering.delete(view.outcomeId);
|
|
426
|
+
this.diagnosticRevisions.delete(view.outcomeId);
|
|
427
|
+
} else this.recovering.add(view.outcomeId);
|
|
428
|
+
if (view.snapshot) (_f = (_e = this.handlers).onSnapshot) == null ? void 0 : _f.call(_e, view.outcomeId, view.book);
|
|
429
|
+
else (_h = (_g = this.handlers).onDelta) == null ? void 0 : _h.call(_g, view.outcomeId, view.book);
|
|
430
|
+
}
|
|
431
|
+
for (const trade of [...view.trades].reverse()) (_j = (_i = this.handlers).onTrade) == null ? void 0 : _j.call(_i, trade);
|
|
432
|
+
}
|
|
433
|
+
})
|
|
434
|
+
);
|
|
435
|
+
}).catch(this.fail);
|
|
436
|
+
const failed = new Promise((resolve) => {
|
|
437
|
+
this.resolveFailure = resolve;
|
|
438
|
+
});
|
|
439
|
+
this.ready = Promise.race([startup, failed]);
|
|
440
|
+
const startupTimeout = setTimeout(this.fail, 3e3);
|
|
441
|
+
void this.ready.then(() => clearTimeout(startupTimeout));
|
|
442
|
+
if (typeof document !== "undefined")
|
|
443
|
+
document.addEventListener("visibilitychange", this.visibility);
|
|
444
|
+
}
|
|
445
|
+
get processesCandles() {
|
|
446
|
+
return !this.fallback;
|
|
447
|
+
}
|
|
448
|
+
decodeHistory(payload) {
|
|
449
|
+
return __async(this, null, function* () {
|
|
450
|
+
this.historyOperations++;
|
|
451
|
+
try {
|
|
452
|
+
return yield this.decodeHistoryRequest(payload);
|
|
453
|
+
} finally {
|
|
454
|
+
this.historyOperations--;
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
decodeHistoryRequest(payload) {
|
|
459
|
+
return __async(this, null, function* () {
|
|
460
|
+
if (this.stopped) throw new Error("Market-data worker disposed");
|
|
461
|
+
yield this.ready;
|
|
462
|
+
if (this.stopped) throw new Error("Market-data worker disposed");
|
|
463
|
+
if (this.fallback) return this.fallback.decodeHistory(payload);
|
|
464
|
+
const remote = this.remote;
|
|
465
|
+
if (!remote) throw new Error("Market-data worker unavailable");
|
|
466
|
+
let rejectFailure;
|
|
467
|
+
const failed = new Promise((_resolve, reject) => {
|
|
468
|
+
rejectFailure = reject;
|
|
469
|
+
this.historyFailures.add(reject);
|
|
470
|
+
});
|
|
471
|
+
try {
|
|
472
|
+
return yield Promise.race([remote.decodeHistory(payload), failed]);
|
|
473
|
+
} finally {
|
|
474
|
+
this.historyFailures.delete(rejectFailure);
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
loadHistory(manifest, options) {
|
|
479
|
+
return __async(this, null, function* () {
|
|
480
|
+
this.historyOperations++;
|
|
481
|
+
try {
|
|
482
|
+
return yield this.loadHistoryRequest(manifest, options);
|
|
483
|
+
} finally {
|
|
484
|
+
this.historyOperations--;
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
loadHistoryRequest(manifest, options) {
|
|
489
|
+
return __async(this, null, function* () {
|
|
490
|
+
var _a, _b, _c, _d, _e;
|
|
491
|
+
if (this.stopped) throw new Error("Market-data worker disposed");
|
|
492
|
+
if (this.fallback) return this.fallback.loadHistory(manifest, options);
|
|
493
|
+
const generation = ++this.historyGeneration;
|
|
494
|
+
const key = `${manifest.venueMarketOutcomeId}:${manifest.resolution}:${manifest.from}:${manifest.to}`;
|
|
495
|
+
(_a = options == null ? void 0 : options.signal) == null ? void 0 : _a.throwIfAborted();
|
|
496
|
+
yield this.ready;
|
|
497
|
+
if (this.stopped) throw new Error("Market-data worker disposed");
|
|
498
|
+
(_b = options == null ? void 0 : options.signal) == null ? void 0 : _b.throwIfAborted();
|
|
499
|
+
const fallbackAfterStartup = this.fallback;
|
|
500
|
+
if (fallbackAfterStartup) return fallbackAfterStartup.loadHistory(manifest, options);
|
|
501
|
+
const remote = this.remote;
|
|
502
|
+
if (!remote) throw new Error("Market-data worker unavailable");
|
|
503
|
+
let rejectFailure;
|
|
504
|
+
const failed = new Promise((_resolve, reject) => {
|
|
505
|
+
rejectFailure = reject;
|
|
506
|
+
this.historyFailures.add(reject);
|
|
507
|
+
});
|
|
508
|
+
const abort = () => {
|
|
509
|
+
var _a2;
|
|
510
|
+
void ((_a2 = this.remote) == null ? void 0 : _a2.cancelHistoryRange(key, generation));
|
|
511
|
+
};
|
|
512
|
+
(_c = options == null ? void 0 : options.signal) == null ? void 0 : _c.addEventListener("abort", abort, { once: true });
|
|
513
|
+
try {
|
|
514
|
+
const result = yield Promise.race([
|
|
515
|
+
remote.loadImmutableRange(key, generation, manifest),
|
|
516
|
+
failed
|
|
517
|
+
]);
|
|
518
|
+
(_d = options == null ? void 0 : options.signal) == null ? void 0 : _d.throwIfAborted();
|
|
519
|
+
return result;
|
|
520
|
+
} finally {
|
|
521
|
+
(_e = options == null ? void 0 : options.signal) == null ? void 0 : _e.removeEventListener("abort", abort);
|
|
522
|
+
this.historyFailures.delete(rejectFailure);
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
setHostDiagnostics(provider) {
|
|
527
|
+
this.hostProvider = provider;
|
|
528
|
+
}
|
|
529
|
+
getDiagnostics() {
|
|
530
|
+
var _a, _b;
|
|
531
|
+
const subscriptions = [...this.refs.values()].map((entry) => entry.sub);
|
|
532
|
+
return this.fallback ? __spreadProps(__spreadValues({}, this.fallback.getDiagnostics()), {
|
|
533
|
+
host: (_a = this.hostProvider) == null ? void 0 : _a.call(this),
|
|
534
|
+
pendingHistoryRequests: this.historyOperations,
|
|
535
|
+
workerAttached: false,
|
|
536
|
+
bridgeListeners: this.endpointListeners.size
|
|
537
|
+
}) : {
|
|
538
|
+
connected: this.connected,
|
|
539
|
+
host: (_b = this.hostProvider) == null ? void 0 : _b.call(this),
|
|
540
|
+
transport: this.stopped ? "disposed" : "worker",
|
|
541
|
+
orderbookOutcomeIds: subscriptions.filter((sub) => sub.channel === "orderbook").map((sub) => sub.id),
|
|
542
|
+
tradeOutcomeIds: subscriptions.filter((sub) => sub.channel === "trades").map((sub) => sub.id),
|
|
543
|
+
arbMarketIds: [...this.arbListeners.keys()],
|
|
544
|
+
arbFeedSubscribers: this.feedListeners.size,
|
|
545
|
+
cachedBooks: this.currentBooks.size,
|
|
546
|
+
cachedArb: this.latest.size,
|
|
547
|
+
resyncingBooks: this.recovering.size,
|
|
548
|
+
pendingResnapshots: 0,
|
|
549
|
+
pendingHistoryRequests: this.historyOperations,
|
|
550
|
+
workerAttached: this.worker !== null,
|
|
551
|
+
bridgeListeners: this.endpointListeners.size
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
getWorkerDiagnostics() {
|
|
555
|
+
return __async(this, null, function* () {
|
|
556
|
+
yield this.ready;
|
|
557
|
+
const remote = this.remote;
|
|
558
|
+
if (this.stopped || !remote || this.fallback) return null;
|
|
559
|
+
let rejectFailure;
|
|
560
|
+
const failed = new Promise((_resolve, reject) => {
|
|
561
|
+
rejectFailure = reject;
|
|
562
|
+
this.historyFailures.add(reject);
|
|
563
|
+
});
|
|
564
|
+
try {
|
|
565
|
+
return yield Promise.race([remote.getMeasurements(), failed]);
|
|
566
|
+
} catch (e) {
|
|
567
|
+
return null;
|
|
568
|
+
} finally {
|
|
569
|
+
this.historyFailures.delete(rejectFailure);
|
|
570
|
+
}
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
get connected() {
|
|
574
|
+
var _a, _b;
|
|
575
|
+
return !this.stopped && ((_b = (_a = this.fallback) == null ? void 0 : _a.connected) != null ? _b : this.online);
|
|
576
|
+
}
|
|
577
|
+
getBook(id) {
|
|
578
|
+
var _a, _b, _c;
|
|
579
|
+
return (_c = (_b = (_a = this.fallback) == null ? void 0 : _a.getBook(id)) != null ? _b : this.currentBooks.get(id)) != null ? _c : null;
|
|
580
|
+
}
|
|
581
|
+
isResyncing(id) {
|
|
582
|
+
var _a, _b;
|
|
583
|
+
return (_b = (_a = this.fallback) == null ? void 0 : _a.isResyncing(id)) != null ? _b : this.recovering.has(id);
|
|
584
|
+
}
|
|
585
|
+
getArb(id) {
|
|
586
|
+
var _a, _b, _c;
|
|
587
|
+
return (_c = (_b = (_a = this.fallback) == null ? void 0 : _a.getArb(id)) != null ? _b : this.latest.get(id)) != null ? _c : null;
|
|
588
|
+
}
|
|
589
|
+
subscribe(id, channel = "orderbook") {
|
|
590
|
+
return this.ref({ id, channel });
|
|
591
|
+
}
|
|
592
|
+
subscribeTrades(id) {
|
|
593
|
+
return this.subscribe(id, "trades");
|
|
594
|
+
}
|
|
595
|
+
subscribeArb(id, cb) {
|
|
596
|
+
var _a;
|
|
597
|
+
const listeners = (_a = this.arbListeners.get(id)) != null ? _a : /* @__PURE__ */ new Set();
|
|
598
|
+
listeners.add(cb);
|
|
599
|
+
this.arbListeners.set(id, listeners);
|
|
600
|
+
const unsubscribe = this.ref({ id, channel: "arb" });
|
|
601
|
+
return () => {
|
|
602
|
+
unsubscribe();
|
|
603
|
+
listeners.delete(cb);
|
|
604
|
+
if (!listeners.size) this.arbListeners.delete(id);
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
subscribeArbFeed(cb) {
|
|
608
|
+
this.feedListeners.add(cb);
|
|
609
|
+
const unsubscribe = this.ref({ id: "*", channel: "arb-feed" });
|
|
610
|
+
return () => {
|
|
611
|
+
unsubscribe();
|
|
612
|
+
this.feedListeners.delete(cb);
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
setCallbacks(callbacks) {
|
|
616
|
+
var _a;
|
|
617
|
+
this.handlers = callbacks;
|
|
618
|
+
if (callbacks.onTrade && !callbacks.onMarketView && !this.fallback) this.fail();
|
|
619
|
+
(_a = this.fallback) == null ? void 0 : _a.setCallbacks(this.fallbackCallbacks());
|
|
620
|
+
}
|
|
621
|
+
authenticate(token) {
|
|
622
|
+
this.config.initialToken = token;
|
|
623
|
+
this.run((engine) => engine.authenticate(token));
|
|
624
|
+
}
|
|
625
|
+
connect() {
|
|
626
|
+
this.run((engine) => engine.connect());
|
|
627
|
+
}
|
|
628
|
+
disconnect() {
|
|
629
|
+
this.run((engine) => engine.disconnect());
|
|
630
|
+
}
|
|
631
|
+
resnapshot(id) {
|
|
632
|
+
const ids = Array.isArray(id) ? id : [id];
|
|
633
|
+
this.run((engine) => engine.resnapshot(ids));
|
|
634
|
+
}
|
|
635
|
+
destroy() {
|
|
636
|
+
var _a, _b;
|
|
637
|
+
if (this.stopped) return;
|
|
638
|
+
this.stopped = true;
|
|
639
|
+
this.online = false;
|
|
640
|
+
(_a = this.resolveFailure) == null ? void 0 : _a.call(this);
|
|
641
|
+
for (const reject of this.historyFailures) reject(new Error("Market-data worker disposed"));
|
|
642
|
+
this.historyFailures.clear();
|
|
643
|
+
this.refs.clear();
|
|
644
|
+
this.currentBooks.clear();
|
|
645
|
+
this.latest.clear();
|
|
646
|
+
this.arbListeners.clear();
|
|
647
|
+
this.feedListeners.clear();
|
|
648
|
+
this.recovering.clear();
|
|
649
|
+
this.diagnosticRevisions.clear();
|
|
650
|
+
this.deliveredRevisions.clear();
|
|
651
|
+
this.resolvedRevisions.clear();
|
|
652
|
+
(_b = this.fallback) == null ? void 0 : _b.destroy();
|
|
653
|
+
this.fallbackUnsubs.clear();
|
|
654
|
+
this.hostProvider = void 0;
|
|
655
|
+
this.handlers = {};
|
|
656
|
+
if (typeof document !== "undefined")
|
|
657
|
+
document.removeEventListener("visibilitychange", this.visibility);
|
|
658
|
+
this.releaseWorker();
|
|
659
|
+
}
|
|
660
|
+
releaseWorker() {
|
|
661
|
+
const worker = this.worker;
|
|
662
|
+
const remote = this.remote;
|
|
663
|
+
this.remote = null;
|
|
664
|
+
this.worker = null;
|
|
665
|
+
if (remote) remote[releaseProxy]();
|
|
666
|
+
if (!worker) return;
|
|
667
|
+
for (const [listener, type] of this.endpointListeners)
|
|
668
|
+
worker.removeEventListener(type, listener);
|
|
669
|
+
this.endpointListeners.clear();
|
|
670
|
+
worker.removeEventListener("error", this.fail);
|
|
671
|
+
worker.removeEventListener("messageerror", this.fail);
|
|
672
|
+
worker.terminate();
|
|
673
|
+
}
|
|
674
|
+
event(name, args, revision = 0) {
|
|
675
|
+
var _a, _b, _c, _d;
|
|
676
|
+
if (this.stopped) return;
|
|
677
|
+
if (name === "onConnectionStateChange") this.online = args[0];
|
|
678
|
+
if (name === "onDiagnostics") {
|
|
679
|
+
const diagnostic = args[0];
|
|
680
|
+
if (diagnostic.outcomeId && revision < ((_a = this.deliveredRevisions.get(diagnostic.outcomeId)) != null ? _a : -1))
|
|
681
|
+
return;
|
|
682
|
+
if (diagnostic.outcomeId && (this.refs.has(`orderbook:${diagnostic.outcomeId}`) || this.refs.has(`trades:${diagnostic.outcomeId}`)) && ["seq_gap", "checksum_mismatch"].includes(diagnostic.type)) {
|
|
683
|
+
this.recovering.add(diagnostic.outcomeId);
|
|
684
|
+
this.diagnosticRevisions.set(diagnostic.outcomeId, revision);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
if (name === "onMarketResolved") {
|
|
688
|
+
const id = args[0].outcomeId;
|
|
689
|
+
if (this.refs.has(`orderbook:${id}`) || this.refs.has(`trades:${id}`))
|
|
690
|
+
this.resolvedRevisions.set(id, revision);
|
|
691
|
+
this.currentBooks.delete(id);
|
|
692
|
+
}
|
|
693
|
+
if (name === "onArbMarket") {
|
|
694
|
+
const msg = args[0];
|
|
695
|
+
this.latest.set(msg.marketId, msg);
|
|
696
|
+
for (const cb of (_b = this.arbListeners.get(msg.marketId)) != null ? _b : []) cb(msg);
|
|
697
|
+
}
|
|
698
|
+
if (name === "onArbFeed") for (const cb of this.feedListeners) cb(args[0]);
|
|
699
|
+
(_d = (_c = this.handlers)[name]) == null ? void 0 : _d.call(_c, ...args);
|
|
700
|
+
}
|
|
701
|
+
ref(sub) {
|
|
702
|
+
var _a;
|
|
703
|
+
const key = `${sub.channel}:${sub.id}`;
|
|
704
|
+
const entry = (_a = this.refs.get(key)) != null ? _a : { sub, count: 0 };
|
|
705
|
+
entry.count++;
|
|
706
|
+
this.refs.set(key, entry);
|
|
707
|
+
this.sync();
|
|
708
|
+
let released = false;
|
|
709
|
+
return () => {
|
|
710
|
+
if (released) return;
|
|
711
|
+
released = true;
|
|
712
|
+
if (--entry.count === 0) {
|
|
713
|
+
this.refs.delete(key);
|
|
714
|
+
if (sub.channel === "orderbook") this.recovering.delete(sub.id);
|
|
715
|
+
if (!this.refs.has(`orderbook:${sub.id}`) && !this.refs.has(`trades:${sub.id}`)) {
|
|
716
|
+
this.currentBooks.delete(sub.id);
|
|
717
|
+
this.recovering.delete(sub.id);
|
|
718
|
+
this.diagnosticRevisions.delete(sub.id);
|
|
719
|
+
this.deliveredRevisions.delete(sub.id);
|
|
720
|
+
this.resolvedRevisions.delete(sub.id);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
this.sync();
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
sync() {
|
|
727
|
+
if (this.queued || this.stopped) return;
|
|
728
|
+
this.queued = true;
|
|
729
|
+
queueMicrotask(() => {
|
|
730
|
+
this.queued = false;
|
|
731
|
+
if (this.stopped) return;
|
|
732
|
+
if (this.fallback) {
|
|
733
|
+
this.syncFallback();
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
const generation = ++this.generation;
|
|
737
|
+
const subscriptions = [...this.refs.values()].map((entry) => entry.sub);
|
|
738
|
+
void this.ready.then(() => {
|
|
739
|
+
var _a;
|
|
740
|
+
if (!this.stopped && !this.fallback)
|
|
741
|
+
return (_a = this.remote) == null ? void 0 : _a.setSubscriptions(subscriptions, generation);
|
|
742
|
+
}).catch(this.fail);
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
run(action) {
|
|
746
|
+
if (this.stopped) return;
|
|
747
|
+
if (this.fallback) {
|
|
748
|
+
action(this.fallback);
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
void this.ready.then(() => {
|
|
752
|
+
var _a;
|
|
753
|
+
if (this.stopped) return;
|
|
754
|
+
const engine = (_a = this.fallback) != null ? _a : this.remote;
|
|
755
|
+
if (engine) return action(engine);
|
|
756
|
+
}).catch(this.fail);
|
|
757
|
+
}
|
|
758
|
+
fallbackCallbacks() {
|
|
759
|
+
return __spreadProps(__spreadValues({}, this.handlers), {
|
|
760
|
+
onArbMarket: (msg) => this.event("onArbMarket", [msg]),
|
|
761
|
+
onArbFeed: (msg) => this.event("onArbFeed", [msg])
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
syncFallback() {
|
|
765
|
+
for (const [key, unsubscribe] of this.fallbackUnsubs) {
|
|
766
|
+
if (!this.refs.has(key)) {
|
|
767
|
+
unsubscribe();
|
|
768
|
+
this.fallbackUnsubs.delete(key);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
for (const [key, { sub }] of this.refs) {
|
|
772
|
+
if (this.fallbackUnsubs.has(key)) continue;
|
|
773
|
+
this.fallbackUnsubs.set(
|
|
774
|
+
key,
|
|
775
|
+
sub.channel === "arb" ? this.fallback.subscribeArb(sub.id, () => {
|
|
776
|
+
}) : sub.channel === "arb-feed" ? this.fallback.subscribeArbFeed(() => {
|
|
777
|
+
}) : this.fallback.subscribe(sub.id, sub.channel)
|
|
778
|
+
);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
function createWorkerWebSocket(options, factory) {
|
|
783
|
+
var _a;
|
|
784
|
+
if (((_a = options.callbacks) == null ? void 0 : _a.onTrade) && !options.callbacks.onMarketView)
|
|
785
|
+
return new AggWebSocket(options);
|
|
786
|
+
try {
|
|
787
|
+
return new WorkerAggWebSocket(options, factory);
|
|
788
|
+
} catch (e) {
|
|
789
|
+
return new AggWebSocket(options);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
export {
|
|
793
|
+
WorkerAggWebSocket,
|
|
794
|
+
createWorkerWebSocket
|
|
795
|
+
};
|
|
796
|
+
/*! Bundled license information:
|
|
797
|
+
|
|
798
|
+
comlink/dist/esm/comlink.mjs:
|
|
799
|
+
(**
|
|
800
|
+
* @license
|
|
801
|
+
* Copyright 2019 Google LLC
|
|
802
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
803
|
+
*)
|
|
804
|
+
*/
|