Skip to content

Commit

Permalink
improve socket typing and store access for tickergrid
Browse files Browse the repository at this point in the history
  • Loading branch information
kajgm committed Jun 12, 2024
1 parent ef629f0 commit 4f32f75
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
19 changes: 8 additions & 11 deletions client/src/socket/socket.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -28,24 +28,21 @@ export function websocketConnect() {
};

socket.onmessage = (e: MessageEvent<unknown>) => {
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);
}
};

Expand Down
2 changes: 0 additions & 2 deletions client/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ export interface ApiRequestData {
timestamp: number;
}

export type WebSocketMessage = { data: object };

export interface WebsocketData {
best_ask: string;
best_ask_size: string;
Expand Down
4 changes: 1 addition & 3 deletions client/src/views/Chart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ const { tickerId } = defineProps<{
}>();
const tickerStore = useTickerStore();
const boxSize = 'LARGE' as SizeType;
const ticker = computed<TickerData>(() => {
return tickerStore.tickerValue(tickerId);
});
const boxSize = 'LARGE' as SizeType;
</script>

<template>
Expand Down
7 changes: 5 additions & 2 deletions client/src/views/TickerGrid.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<script setup lang="ts">
import { computed } from 'vue';
import { useTickerStore } from '@/store/ticker';
import { type SizeType } from '@/types/types';
import TickerBox from '@/components/ticker/TickerBox.vue';
const tickerStore = useTickerStore();
const tickerIds = computed<string[]>(() => {
return tickerStore.tickerKeys;
});
const boxSize = 'SMALL' as SizeType;
</script>

Expand All @@ -13,7 +16,7 @@ const boxSize = 'SMALL' as SizeType;
<h1 class="font-medium text-4xl">Connecting...</h1>
</div>
<div v-else-if="tickerStore.status.overall == 'CONNECTED'" class="flex flex-wrap justify-center m-auto w-full h-full">
<div v-for="id in tickerStore.tickerKeys" :key="id" class="w-1/2 h-1/2">
<div v-for="id in tickerIds" :key="id" class="w-1/2 h-1/2">
<TickerBox :ticker-id="id" :box-size="boxSize" :r-link="id"></TickerBox>
</div>
</div>
Expand Down

0 comments on commit 4f32f75

Please sign in to comment.