fix(web): satisfy React hook lint rules
This commit is contained in:
1 parent
bb68be14fa
commit
a4215f86fb
8 files changed
+374
-256
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { io, type Socket } from 'socket.io-client';
|
||||
import { API_BASE_URL, useAuthUserId } from '../config';
|
||||
import { acquireAccessToken } from '../lib/auth/token-provider';
|
||||
@@ -140,6 +140,9 @@ function mergeChatMessages(
|
||||
|
||||
export function useAuctionSocket(matchId: string | undefined) {
|
||||
const authUserId = useAuthUserId();
|
||||
const connectionKey = matchId
|
||||
? JSON.stringify([matchId, authUserId ?? null])
|
||||
: null;
|
||||
const socketRef = useRef<Socket | null>(null);
|
||||
const pendingBidTimeoutsRef = useRef<
|
||||
Record<string, ReturnType<typeof setTimeout>>
|
||||
@@ -166,9 +169,14 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
Record<string, { action: string }>
|
||||
>({});
|
||||
const [resetAt, setResetAt] = useState<number | null>(null);
|
||||
const [connectionStatus, setConnectionStatus] =
|
||||
useState<ConnectionStatus>('idle');
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [connectionState, setConnectionState] = useState<{
|
||||
key: string;
|
||||
status: ConnectionStatus;
|
||||
}>({ key: '', status: 'idle' });
|
||||
const [errorState, setErrorState] = useState<{
|
||||
key: string;
|
||||
message: string | null;
|
||||
}>({ key: '', message: null });
|
||||
const [serverTimeOffsetMs, setServerTimeOffsetMs] = useState(0);
|
||||
const [serverRoundTripMs, setServerRoundTripMs] = useState<number | null>(
|
||||
null,
|
||||
@@ -176,53 +184,50 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
const [bidLatencyDiagnostics, setBidLatencyDiagnostics] =
|
||||
useState<BidLatencyDiagnostics | null>(null);
|
||||
|
||||
function syncLatestConfirmedBidPointAmountFromAuction(
|
||||
nextAuction: ApiAuction,
|
||||
) {
|
||||
latestConfirmedBidPointAmountRef.current =
|
||||
resolveCurrentBidPointAmount(nextAuction);
|
||||
}
|
||||
const commitAuctionState = useCallback(
|
||||
(nextAuction: ApiAuction, source = 'socket-state'): boolean => {
|
||||
const currentAuction = latestAuctionRef.current;
|
||||
const applied = shouldApplyIncomingAuctionState(
|
||||
currentAuction,
|
||||
nextAuction,
|
||||
);
|
||||
const currentRevision =
|
||||
typeof currentAuction?.revision === 'number'
|
||||
? currentAuction.revision
|
||||
: -1;
|
||||
const incomingRevision =
|
||||
typeof nextAuction?.revision === 'number' ? nextAuction.revision : -1;
|
||||
// 단조성을 깨고 적용되는 경우(=revision이 같거나 뒤로 가는데도 반영됨).
|
||||
// force-resync/lifecycle 교체로만 발생하며, "2~3 버전 롤백"의 직접 증거가 된다.
|
||||
const isBackwardApply =
|
||||
applied &&
|
||||
currentRevision >= 0 &&
|
||||
incomingRevision >= 0 &&
|
||||
incomingRevision <= currentRevision;
|
||||
addNewRelicPageAction('AuctionClientStateApply', {
|
||||
matchId: matchId ?? 'none',
|
||||
source,
|
||||
applied,
|
||||
isBackwardApply,
|
||||
revisionDelta: incomingRevision - currentRevision,
|
||||
...describeAuctionForTelemetry('current', currentAuction),
|
||||
...describeAuctionForTelemetry('incoming', nextAuction),
|
||||
});
|
||||
|
||||
function commitAuctionState(
|
||||
nextAuction: ApiAuction,
|
||||
source = 'socket-state',
|
||||
): boolean {
|
||||
const currentAuction = latestAuctionRef.current;
|
||||
const applied = shouldApplyIncomingAuctionState(currentAuction, nextAuction);
|
||||
const currentRevision =
|
||||
typeof currentAuction?.revision === 'number'
|
||||
? currentAuction.revision
|
||||
: -1;
|
||||
const incomingRevision =
|
||||
typeof nextAuction?.revision === 'number' ? nextAuction.revision : -1;
|
||||
// 단조성을 깨고 적용되는 경우(=revision이 같거나 뒤로 가는데도 반영됨).
|
||||
// force-resync/lifecycle 교체로만 발생하며, "2~3 버전 롤백"의 직접 증거가 된다.
|
||||
const isBackwardApply =
|
||||
applied &&
|
||||
currentRevision >= 0 &&
|
||||
incomingRevision >= 0 &&
|
||||
incomingRevision <= currentRevision;
|
||||
addNewRelicPageAction('AuctionClientStateApply', {
|
||||
matchId: matchId ?? 'none',
|
||||
source,
|
||||
applied,
|
||||
isBackwardApply,
|
||||
revisionDelta: incomingRevision - currentRevision,
|
||||
...describeAuctionForTelemetry('current', currentAuction),
|
||||
...describeAuctionForTelemetry('incoming', nextAuction),
|
||||
});
|
||||
if (!applied) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!applied) {
|
||||
return false;
|
||||
}
|
||||
latestAuctionRef.current = nextAuction;
|
||||
latestConfirmedBidPointAmountRef.current =
|
||||
resolveCurrentBidPointAmount(nextAuction);
|
||||
setAuction(nextAuction);
|
||||
return true;
|
||||
},
|
||||
[matchId],
|
||||
);
|
||||
|
||||
latestAuctionRef.current = nextAuction;
|
||||
syncLatestConfirmedBidPointAmountFromAuction(nextAuction);
|
||||
setAuction(nextAuction);
|
||||
return true;
|
||||
}
|
||||
|
||||
function commitBidDelta(payload: BidDeltaPayload): boolean {
|
||||
const commitBidDelta = useCallback((payload: BidDeltaPayload): boolean => {
|
||||
const currentAuction = latestAuctionRef.current;
|
||||
const nextAuction = applyBidDelta(currentAuction, payload);
|
||||
if (!nextAuction || nextAuction === currentAuction) {
|
||||
@@ -230,10 +235,18 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
}
|
||||
|
||||
latestAuctionRef.current = nextAuction;
|
||||
syncLatestConfirmedBidPointAmountFromAuction(nextAuction);
|
||||
latestConfirmedBidPointAmountRef.current =
|
||||
resolveCurrentBidPointAmount(nextAuction);
|
||||
setAuction(nextAuction);
|
||||
return true;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const setError = useCallback(
|
||||
(message: string | null) => {
|
||||
setErrorState({ key: connectionKey ?? '', message });
|
||||
},
|
||||
[connectionKey],
|
||||
);
|
||||
|
||||
function applyFallbackServerTimestamp(currentServerTimestampMS?: number) {
|
||||
if (hasSyncedServerTimeRef.current) return;
|
||||
@@ -345,14 +358,47 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
});
|
||||
}
|
||||
|
||||
const clearPendingBidRequest = useCallback((bidRequestId: string) => {
|
||||
const timeoutId = pendingBidTimeoutsRef.current[bidRequestId];
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
delete pendingBidTimeoutsRef.current[bidRequestId];
|
||||
setPendingBidRequests((current) => {
|
||||
const next = { ...current };
|
||||
delete next[bidRequestId];
|
||||
return next;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const clearPendingActionRequest = useCallback(
|
||||
(auctionActionRequestId: string) => {
|
||||
const timeoutId =
|
||||
pendingActionTimeoutsRef.current[auctionActionRequestId];
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
delete pendingActionTimeoutsRef.current[auctionActionRequestId];
|
||||
setPendingActionRequests((current) => {
|
||||
const next = { ...current };
|
||||
delete next[auctionActionRequestId];
|
||||
return next;
|
||||
});
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
const clearPendingActionRequests = useCallback(() => {
|
||||
Object.values(pendingActionTimeoutsRef.current).forEach((timeoutId) =>
|
||||
clearTimeout(timeoutId),
|
||||
);
|
||||
pendingActionTimeoutsRef.current = {};
|
||||
setPendingActionRequests({});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!matchId) {
|
||||
if (!matchId || !connectionKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
const activeConnectionKey = connectionKey;
|
||||
let disposed = false;
|
||||
setConnectionStatus('connecting');
|
||||
setError(null);
|
||||
|
||||
void (async () => {
|
||||
const accessToken = await acquireAccessToken().catch((error) => {
|
||||
@@ -373,7 +419,11 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
socketRef.current = socket;
|
||||
|
||||
socket.on('connect', () => {
|
||||
setConnectionStatus('connected');
|
||||
setConnectionState({
|
||||
key: activeConnectionKey,
|
||||
status: 'connected',
|
||||
});
|
||||
setError(null);
|
||||
socket.emit('join', { matchId });
|
||||
requestServerTimeSync(socket);
|
||||
if (timeSyncIntervalRef.current) {
|
||||
@@ -387,7 +437,10 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
clearTimeSyncTimers();
|
||||
setConnectionStatus('disconnected');
|
||||
setConnectionState({
|
||||
key: activeConnectionKey,
|
||||
status: 'disconnected',
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('state', (nextAuction: ApiAuction) => {
|
||||
@@ -520,42 +573,20 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
hasSyncedServerTimeRef.current = false;
|
||||
setPendingBidRequests({});
|
||||
setPendingActionRequests({});
|
||||
setConnectionStatus('idle');
|
||||
setServerTimeOffsetMs(0);
|
||||
setServerRoundTripMs(null);
|
||||
setBidLatencyDiagnostics(null);
|
||||
};
|
||||
}, [authUserId, matchId]);
|
||||
|
||||
function clearPendingBidRequest(bidRequestId: string) {
|
||||
const timeoutId = pendingBidTimeoutsRef.current[bidRequestId];
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
delete pendingBidTimeoutsRef.current[bidRequestId];
|
||||
setPendingBidRequests((current) => {
|
||||
const next = { ...current };
|
||||
delete next[bidRequestId];
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
function clearPendingActionRequest(auctionActionRequestId: string) {
|
||||
const timeoutId = pendingActionTimeoutsRef.current[auctionActionRequestId];
|
||||
if (timeoutId) clearTimeout(timeoutId);
|
||||
delete pendingActionTimeoutsRef.current[auctionActionRequestId];
|
||||
setPendingActionRequests((current) => {
|
||||
const next = { ...current };
|
||||
delete next[auctionActionRequestId];
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
function clearPendingActionRequests() {
|
||||
Object.values(pendingActionTimeoutsRef.current).forEach((timeoutId) =>
|
||||
clearTimeout(timeoutId),
|
||||
);
|
||||
pendingActionTimeoutsRef.current = {};
|
||||
setPendingActionRequests({});
|
||||
}
|
||||
}, [
|
||||
authUserId,
|
||||
clearPendingActionRequests,
|
||||
clearPendingBidRequest,
|
||||
commitAuctionState,
|
||||
commitBidDelta,
|
||||
connectionKey,
|
||||
matchId,
|
||||
setError,
|
||||
]);
|
||||
|
||||
function emit(eventName: string, payload?: unknown) {
|
||||
const socket = socketRef.current;
|
||||
@@ -737,9 +768,16 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
const currentAuction = auction;
|
||||
const visibleAuction =
|
||||
currentAuction && currentAuction.matchId === matchId ? currentAuction : null;
|
||||
const visibleConnectionStatus: ConnectionStatus = matchId
|
||||
? connectionStatus
|
||||
: 'idle';
|
||||
const visibleConnectionStatus: ConnectionStatus =
|
||||
connectionKey === null
|
||||
? 'idle'
|
||||
: connectionState.key === connectionKey
|
||||
? connectionState.status
|
||||
: 'connecting';
|
||||
const visibleError =
|
||||
connectionKey !== null && errorState.key === connectionKey
|
||||
? errorState.message
|
||||
: null;
|
||||
|
||||
return {
|
||||
auction: visibleAuction,
|
||||
@@ -750,7 +788,7 @@ export function useAuctionSocket(matchId: string | undefined) {
|
||||
pendingBidRequests,
|
||||
resetAt,
|
||||
connectionStatus: visibleConnectionStatus,
|
||||
error,
|
||||
error: visibleError,
|
||||
bidLatencyDiagnostics,
|
||||
serverRoundTripMs: matchId ? serverRoundTripMs : null,
|
||||
serverTimeOffsetMs: matchId ? serverTimeOffsetMs : 0,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { io, type Socket } from 'socket.io-client';
|
||||
import { io } from 'socket.io-client';
|
||||
import { API_BASE_URL } from '../config';
|
||||
import type { ApiRiotRefreshEvent } from './types';
|
||||
|
||||
@@ -19,37 +19,43 @@ export function useRiotRefreshSocket({
|
||||
parentJobId?: string;
|
||||
onEvent: (event: ApiRiotRefreshEvent) => void;
|
||||
}) {
|
||||
const socketRef = useRef<Socket | null>(null);
|
||||
const subscriptionKey =
|
||||
puuid || parentJobId ? JSON.stringify([puuid, parentJobId]) : null;
|
||||
const onEventRef = useRef(onEvent);
|
||||
const [connectionStatus, setConnectionStatus] =
|
||||
useState<ConnectionStatus>('idle');
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [connectionState, setConnectionState] = useState<{
|
||||
key: string;
|
||||
status: ConnectionStatus;
|
||||
error: string | null;
|
||||
}>({ key: '', status: 'idle', error: null });
|
||||
|
||||
useEffect(() => {
|
||||
onEventRef.current = onEvent;
|
||||
}, [onEvent]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!puuid && !parentJobId) {
|
||||
setConnectionStatus('idle');
|
||||
if (!subscriptionKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
setConnectionStatus('connecting');
|
||||
setError(null);
|
||||
|
||||
const socket = io(`${API_BASE_URL}/riot-refresh`, {
|
||||
transports: ['websocket'],
|
||||
});
|
||||
socketRef.current = socket;
|
||||
|
||||
socket.on('connect', () => {
|
||||
setConnectionStatus('connected');
|
||||
setConnectionState({
|
||||
key: subscriptionKey,
|
||||
status: 'connected',
|
||||
error: null,
|
||||
});
|
||||
socket.emit('subscribe', { puuid, parentJobId });
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
setConnectionStatus('disconnected');
|
||||
setConnectionState((current) => ({
|
||||
key: subscriptionKey,
|
||||
status: 'disconnected',
|
||||
error: current.key === subscriptionKey ? current.error : null,
|
||||
}));
|
||||
});
|
||||
|
||||
socket.on('riot-refresh', (event: ApiRiotRefreshEvent) => {
|
||||
@@ -57,17 +63,29 @@ export function useRiotRefreshSocket({
|
||||
});
|
||||
|
||||
socket.on('error', (payload: { message?: string }) => {
|
||||
setConnectionStatus('error');
|
||||
setError(payload.message ?? '전적 갱신 소켓 연결 중 오류가 발생했습니다.');
|
||||
setConnectionState({
|
||||
key: subscriptionKey,
|
||||
status: 'error',
|
||||
error:
|
||||
payload.message ?? '전적 갱신 소켓 연결 중 오류가 발생했습니다.',
|
||||
});
|
||||
});
|
||||
|
||||
return () => {
|
||||
socket.removeAllListeners();
|
||||
socket.disconnect();
|
||||
socketRef.current = null;
|
||||
setConnectionStatus('idle');
|
||||
};
|
||||
}, [parentJobId, puuid]);
|
||||
}, [parentJobId, puuid, subscriptionKey]);
|
||||
|
||||
return { connectionStatus, error };
|
||||
const hasCurrentConnection =
|
||||
subscriptionKey !== null && connectionState.key === subscriptionKey;
|
||||
return {
|
||||
connectionStatus:
|
||||
subscriptionKey === null
|
||||
? 'idle'
|
||||
: hasCurrentConnection
|
||||
? connectionState.status
|
||||
: 'connecting',
|
||||
error: hasCurrentConnection ? connectionState.error : null,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user