diff --git a/core/src/subscriber/watcher.ts b/core/src/subscriber/watcher.ts index 9864804a..a83c74dc 100644 --- a/core/src/subscriber/watcher.ts +++ b/core/src/subscriber/watcher.ts @@ -101,18 +101,16 @@ export class AddressWatcher { if (assetId) { const assetKey = `${key} ${assetId}`; return new Promise((resolve, reject) => { - if (!this.assetIdResolvers.has(assetKey)) { - this.assetIdResolvers.set(assetKey, []); - } - this.assetIdResolvers.get(assetKey)!.push({ resolve, reject }); + const list = this.assetIdResolvers.get(assetKey) ?? []; + list.push({ resolve, reject }); + this.assetIdResolvers.set(assetKey, list); }); } return new Promise((resolve, reject) => { - if (!this.resolvers.has(key)) { - this.resolvers.set(key, []); - } - this.resolvers.get(key)!.push({ resolve, reject }); + const list = this.resolvers.get(key) ?? []; + list.push({ resolve, reject }); + this.resolvers.set(key, list); }); } diff --git a/core/src/utils/market-utils.ts b/core/src/utils/market-utils.ts index c54e431e..c3131a7b 100644 --- a/core/src/utils/market-utils.ts +++ b/core/src/utils/market-utils.ts @@ -46,11 +46,11 @@ export function addBinaryOutcomes(market: UnifiedMarket): void { // those are meaningful labels for financial markets. const yesLabel = market.yes?.label.toLowerCase(); const noLabel = market.no?.label.toLowerCase(); - if (market.title && yesLabel === 'yes') { - market.yes!.label = market.title; + if (market.title && market.yes && yesLabel === 'yes') { + market.yes = { ...market.yes, label: market.title }; } - if (market.title && noLabel === 'no') { - market.no!.label = `Not ${market.title}`; + if (market.title && market.no && noLabel === 'no') { + market.no = { ...market.no, label: `Not ${market.title}` }; } market.up = market.yes; diff --git a/core/src/utils/throttler.ts b/core/src/utils/throttler.ts index 4b299dd3..7673f5e1 100644 --- a/core/src/utils/throttler.ts +++ b/core/src/utils/throttler.ts @@ -7,19 +7,28 @@ export class Throttler { private refillRate: number; private capacity: number; private delay: number; + private maxQueueDepth: number; constructor(config: { refillRate: number; // tokens per ms (1 / rateLimit) capacity: number; // max tokens delay: number; // polling interval in ms + maxQueueDepth?: number; // max queued requests (default 1000) }) { this.refillRate = config.refillRate; this.capacity = config.capacity; this.delay = config.delay; + this.maxQueueDepth = config.maxQueueDepth ?? 1000; } async throttle(cost: number = 1): Promise { return new Promise((resolve) => { + if (this.queue.length >= this.maxQueueDepth) { + const dropped = this.queue.shift(); + if (dropped) { + dropped.resolve(); + } + } this.queue.push({ resolve, cost }); if (!this.running) { this.running = true;