| 12345678910111213141516171819202122232425262728293031 |
- 'use client';
- import { useEffect, useRef } from 'react';
- import { useSignalRContext } from '@/contexts/signalrProvider';
- export default function useMarketData(market: string) {
- const { cryptoConnection, cryptoConnected } = useSignalRContext();
- const prevMarketRef = useRef<string | null>(null);
- useEffect(() => {
- if (!cryptoConnection || !cryptoConnected || !market) {
- return;
- }
- // 이전 마켓 구독 해제
- if (prevMarketRef.current && prevMarketRef.current !== market) {
- cryptoConnection.invoke('UnsubscribeMarket', prevMarketRef.current).catch(() => {});
- }
- // 새 마켓 구독
- cryptoConnection.invoke('SubscribeMarket', market).catch(console.error);
- prevMarketRef.current = market;
- return () => {
- if (prevMarketRef.current) {
- cryptoConnection.invoke('UnsubscribeMarket', prevMarketRef.current).catch(() => {});
- prevMarketRef.current = null;
- }
- };
- }, [cryptoConnection, cryptoConnected, market]);
- }
|