Skip to content

Commit

Permalink
fix: enhance alias change handling to prevent loss of subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
maximovicd committed Mar 25, 2024
1 parent cee34a2 commit 3714a15
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/connection/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export class OpenFeedListeners {
if (symbols) {
symbols = symbols.filter((s) => s !== symbol);
}
if (!symbols) {
this.instrumentByMarketId.delete(marketId.toString());
}
this.instrumentBySymbol.delete(symbol);
}
this.instrumentByMarketId.set(marketId.toString(), [def, symbols]);
Expand All @@ -55,13 +58,30 @@ export class OpenFeedListeners {
} else if (message.instrumentAction) {
const { marketId } = message.instrumentAction?.instrument ?? {};
if (message.instrumentAction.action === ActionType.ALIAS_CHANGED && marketId) {
const { oldAlias } = message.instrumentAction;
const [root, num] = oldAlias.split("*");
const newAliasNum = oldAlias.endsWith("*0") ? 0 : Number.parseInt(num, 10) + 1;
const newAlias = `${root}*${newAliasNum}`;
// remove the old symbol in the source
[def, symbols] = getInstrumentDefinition(marketId);
const newSymbols = symbols?.filter((s) => !s.includes("*")) ?? [];
const newSymbols = symbols?.filter((s) => s !== oldAlias) ?? [];
if (!newSymbols.length) {
this.instrumentByMarketId.delete(marketId.toString());
} else {
this.instrumentByMarketId.set(marketId.toString(), [def, newSymbols]);
}

const { marketId: newMarketId } = message.instrumentAction.newInstrument ?? {};
// remove new alias from the destination, unless it's *0
if (newMarketId) {
const [newDef, newSym] = getInstrumentDefinition(newMarketId);
const newSymbolsFiltered = (newAlias === oldAlias ? newSym : newSym?.filter((s) => s !== newAlias)) ?? [];
if (!newSymbolsFiltered.length) {
this.instrumentByMarketId.delete(newMarketId.toString());
} else {
this.instrumentByMarketId.set(newMarketId.toString(), [newDef, newSymbolsFiltered]);
}
}
}
// In the event of exchange move, we just remove the instrument subscription and let the subscription response do the rest
if (message.instrumentAction.action === ActionType.EXCHANGE_MOVE && marketId) {
Expand Down

0 comments on commit 3714a15

Please sign in to comment.