export interface BigOrder { id: number; symbol: string; price: number; quantity: number;…
- User
export interface BigOrder { id: number; symbol: string; price: number; quantity: number; volumeAsDollars: number; }
let isSubscribed = false; let wsConnection: WebSocket|null = null; let count = 0; let symbols:string[] = []; let bigOrdersQuantity = 300; let bigOrders: BigOrder[] = []; let precision: {[key: string]: Precision} = {}; let quantity: number; let quantityAssets: {[key: string]: number|null}; const connectToWs = (callback: () => void) => { if (wsConnection) { callback();
return;}
wsConnection = new WebSocket(
${process.env.NEXT_PUBLIC_RUST_WS_SERVER});wsConnection.onopen = () => { if (null !== wsConnection) { callback(); } }; wsConnection.onmessage = async(message: MessageEvent) => { if (!message.data) return;
const data = JSON.parse(message.data); if (!data?.commands || data.commands.length === 0) return; const bigOrder = data.commands.find((item: any) => "undefined" !== typeof item.BigOrder); const bigOrderElimination = data.commands.find((item: any) => "undefined" !== typeof item.BigOrderElimination); if (bigOrder) { const symbol = bigOrder.BigOrder.exchange.substring(3); if (symbols.includes(symbol)) { const price = bigOrder.BigOrder.price / Math.pow(10, precision[symbol].price); const quantity = bigOrder.BigOrder.quantity / Math.pow(10, precision[symbol].quantity); const order = { id: count, symbol: symbol, price: price, quantity: quantity, timestamp: bigOrder.BigOrder.timestamp, volumeAsDollars: price * quantity, }; bigOrders.push(order); count = count + 1; publish(); } } if(bigOrderElimination) { const price = bigOrderElimination.bigOrderElimination.price }}; };
нужно изменить let bigOrders: BigOrder[] = [];, сделать объектом, где ключами будут
${symbol}_${price}if(bigOrderElimination) { то удалять по ключу из объекта
- Assistant
export interface BigOrder { id: number; symbol: string; price: number; quantity: number; volumeAsDollars: number; }
let isSubscribed = false; let wsConnection: WebSocket|null = null; let count = 0; let symbols:string[] = []; let bigOrders: {[key: string]: BigOrder} = {}; let precision: {[key: string]: Precision} = {}; let quantity: number; let quantityAssets: {[key: string]: number|null};
const connectToWs = (callback: () => void) => { if (wsConnection) { callback();
return;}
wsConnection = new WebSocket(
${process.env.NEXT_PUBLIC_RUST_WS_SERVER});wsConnection.onopen = () => { if (null !== wsConnection) { callback(); } }; wsConnection.onmessage = async(message: MessageEvent) => { if (!message.data) return;
const data = JSON.parse(message.data); if (!data?.commands || data.commands.length === 0) return; const bigOrder = data.commands.find((item: any) => "undefined" !== typeof item.BigOrder); const bigOrderElimination = data.commands.find((item: any) => "undefined" !== typeof item.BigOrderElimination); if (bigOrder) { const symbol = bigOrder.BigOrder.exchange.substring(3); if (symbols.includes(symbol)) { const price = bigOrder.BigOrder.price / Math.pow(10, precision[symbol].price); const quantity = bigOrder.BigOrder.quantity / Math.pow(10, precision[symbol].quantity); const orderKey = `${symbol}_${price}`; const order: BigOrder = { id: count, symbol: symbol, price: price, quantity: quantity, timestamp: bigOrder.BigOrder.timestamp, volumeAsDollars: price * quantity, }; bigOrders[orderKey] = order; count = count + 1; publish(); } } if (bigOrderElimination) { const symbol = bigOrderElimination.bigOrderElimination.exchange.substring(3); const price = bigOrderElimination.bigOrderElimination.price / Math.pow(10, precision[symbol].price); const orderKey = `${symbol}_${price}`; delete bigOrders[orderKey]; }}; };
By making `bigOrders` an object with keys as `${symbol}_${price}`, we can easily add and remove orders using the appropriate key.