-
Notifications
You must be signed in to change notification settings - Fork 0
실시간 알림(SSE) 버그 수정 ( issue #310 ) #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4bc193d
bug : 실시간 알림 실행 중에 API 모든 요청이 다운되는 버그 수정
layout-SY f2dd243
feat : 실시간 알림 데이터 context화
layout-SY e71f9e3
Merge branch 'develop' of https://github.com/devpalsPlus/frontend int…
layout-SY f8bea52
feat : 헤더 알림에 실시간 알림 테스트 코드 추가
layout-SY 08969a5
Merge branch 'develop' of https://github.com/devpalsPlus/frontend int…
layout-SY da0a3f8
feat : 리뷰 사항 반영
layout-SY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/components/user/notificationLive/NotificationInitializer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import useNotification from '../../../hooks/user/useNotification'; | ||
|
|
||
| const NotificationInitializer = () => { | ||
| useNotification(); | ||
|
|
||
| return null; | ||
| }; | ||
|
|
||
| export default NotificationInitializer; | ||
16 changes: 16 additions & 0 deletions
16
src/components/user/notificationLive/NotificationProvider.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { ReactNode, useState } from 'react'; | ||
| import { AlarmLive } from '../../../models/alarm'; | ||
| import { SseContext } from '../../../context/SseContext'; | ||
|
|
||
| export const NotificationProvider = ({ children }: { children: ReactNode }) => { | ||
| const [signalData, setSignalData] = useState<AlarmLive | null>(null); | ||
|
|
||
| const clearSignal = () => setSignalData(null); | ||
| const setSignal = (data: AlarmLive | null) => setSignalData(data); | ||
|
|
||
| return ( | ||
| <SseContext.Provider value={{ signalData, clearSignal, setSignal }}> | ||
| {children} | ||
| </SseContext.Provider> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { createContext, useContext } from 'react'; | ||
| import { AlarmLive } from '../models/alarm'; | ||
|
|
||
| export interface SseContextProps { | ||
| signalData: AlarmLive | null; | ||
| clearSignal: () => void; | ||
| setSignal: (data: AlarmLive | null) => void; | ||
| } | ||
|
|
||
| export const SseContext = createContext<SseContextProps>({ | ||
| signalData: null, | ||
| clearSignal: () => {}, | ||
| setSignal: () => {}, | ||
| }); | ||
|
|
||
| export const useNotificationContext = () => useContext(SseContext); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
초기화 컴포넌트 패턴이 적절합니다.
전역 SSE 연결 초기화를 위한 깔끔한 접근 방식입니다.
useNotification훅에서 SSE 연결의 적절한 정리(cleanup)가 이루어지는지 확인해주세요. EventSource 연결이 컴포넌트 언마운트 시 제대로 닫히지 않으면 메모리 누수가 발생할 수 있습니다.🏁 Script executed:
Length of output: 3614
useNotification 훅 정의 위치와 EventSource 사용 여부를 확인하기 위해 다음 스크립트를 실행해주세요:
🏁 Script executed:
Length of output: 1094
useNotification 훅의
useEffect내부를 확인하기 위해 아래 스크립트를 실행해주세요:🏁 Script executed:
Length of output: 1413
useNotification 훅의 전체 cleanup 로직을 확인하기 위해 아래 스크립트를 실행해주세요:
🏁 Script executed:
Length of output: 67
useNotification 훅의 useEffect 전체 블록(cleanup 반환 함수 포함)을 확인하기 위해 아래 스크립트를 실행해주세요:
🏁 Script executed:
Length of output: 2278
useNotification 훅에 SSE 연결 정리(cleanup) 로직 추가가 필요합니다.
현재
useEffect내부에 EventSource를 생성만 하고, 컴포넌트 언마운트 시나 의존성 변경 시 연결을 닫는 반환 함수가 없습니다. 이로 인해 메모리 누수가 발생할 수 있습니다.다음 부분에 cleanup 반환 함수를 추가해주세요.
src/hooks/user/useNotification.tsuseEffect(() => { … }, [queryClient, userId]);내부 끝부분예시 수정안:
useEffect(() => { if (!userId) { if (eventSourceRef.current) { eventSourceRef.current.close(); eventSourceRef.current = null; } return; } if (eventSourceRef.current) { return; } else { const eventSource = new EventSource(/* … */); eventSourceRef.current = eventSource; // 이벤트 핸들러 등록… } + // 컴포넌트 언마운트 또는 userId/queryClient 변경 시 SSE 연결 닫기 + return () => { + if (eventSourceRef.current) { + eventSourceRef.current.close(); + eventSourceRef.current = null; + } + }; }, [queryClient, userId]);위 변경으로 언마운트 시점 및 의존성 변경 시 EventSource가 정상적으로 종료됩니다.
🤖 Prompt for AI Agents