From 4f32f755d20c3669f59f6b8879bd9d9dd11a9f48 Mon Sep 17 00:00:00 2001 From: kajgrant Date: Wed, 12 Jun 2024 16:20:35 -0700 Subject: [PATCH] improve socket typing and store access for tickergrid --- client/src/socket/socket.ts | 19 ++++++++----------- client/src/types/types.ts | 2 -- client/src/views/Chart.vue | 4 +--- client/src/views/TickerGrid.vue | 7 +++++-- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/client/src/socket/socket.ts b/client/src/socket/socket.ts index 2746d93..7afc08e 100644 --- a/client/src/socket/socket.ts +++ b/client/src/socket/socket.ts @@ -1,6 +1,6 @@ import { useTickerStore } from '@/store/ticker.js'; import { priceDirection } from '@/helpers/helpers.js'; -import { WebSocketMessage, type StatusType, type TickerData, type WebsocketData } from '@/types/types.js'; +import { type StatusType, type TickerData, type WebsocketData } from '@/types/types.js'; const CRYPTO_ENDPOINT = 'wss://ws-feed.exchange.coinbase.com'; @@ -28,24 +28,21 @@ export function websocketConnect() { }; socket.onmessage = (e: MessageEvent) => { - const msg = JSON.parse(e.data as string); - - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + const msg = JSON.parse(e.data as string) as WebsocketData; if (msg['type'] == 'ticker') { - const socketTicker = msg as WebsocketData; - const prevRes = tickerStore.tickerValue(socketTicker.product_id); - const curPrice = parseFloat(socketTicker.price); - const dayPrice = parseFloat(socketTicker.open_24h); + const prevRes = tickerStore.tickerValue(msg.product_id); + const curPrice = parseFloat(msg.price); + const dayPrice = parseFloat(msg.open_24h); const tickerValue = { - id: socketTicker.product_id, + id: msg.product_id, curPrice: curPrice, - volume: parseFloat(socketTicker.volume_24h), + volume: parseFloat(msg.volume_24h), dayPercentage: ((curPrice - dayPrice) / dayPrice) * 100, prevPrice: prevRes.curPrice, dirFilter: priceDirection(prevRes.dirFilter, curPrice, prevRes.prevPrice), status: 'CONNECTED' } as TickerData; - tickerStore.updateTickerData(socketTicker.product_id, tickerValue); + tickerStore.updateTickerData(msg.product_id, tickerValue); } }; diff --git a/client/src/types/types.ts b/client/src/types/types.ts index 0d059ea..e92e029 100644 --- a/client/src/types/types.ts +++ b/client/src/types/types.ts @@ -35,8 +35,6 @@ export interface ApiRequestData { timestamp: number; } -export type WebSocketMessage = { data: object }; - export interface WebsocketData { best_ask: string; best_ask_size: string; diff --git a/client/src/views/Chart.vue b/client/src/views/Chart.vue index b064d60..d8833b4 100644 --- a/client/src/views/Chart.vue +++ b/client/src/views/Chart.vue @@ -9,12 +9,10 @@ const { tickerId } = defineProps<{ }>(); const tickerStore = useTickerStore(); - -const boxSize = 'LARGE' as SizeType; - const ticker = computed(() => { return tickerStore.tickerValue(tickerId); }); +const boxSize = 'LARGE' as SizeType;