Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions core/src/subscriber/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,16 @@ export class AddressWatcher {
if (assetId) {
const assetKey = `${key} ${assetId}`;
return new Promise<Trade[]>((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<SubscribedAddressSnapshot>((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);
});
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/utils/market-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions core/src/utils/throttler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
return new Promise<void>((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;
Expand Down
Loading