Skip to content

Commit a2cbf30

Browse files
committed
refactor: AbortError가 아닌 경우 log 출력
1 parent d943f47 commit a2cbf30

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/hooks/useCamera.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ function useCamera(setPhoto: (photo: string | null) => void) {
66
const videoRef = useRef<HTMLVideoElement>(null)
77
const canvasRef = useRef<HTMLCanvasElement>(null)
88
const streamRef = useRef<MediaStream | null>(null)
9+
const isMountedRef = useRef(true)
910

1011
const openCamera = useCallback(async () => {
1112
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
@@ -27,14 +28,32 @@ function useCamera(setPhoto: (photo: string | null) => void) {
2728
}
2829

2930
const stream = await navigator.mediaDevices.getUserMedia(constraints)
31+
32+
if (!isMountedRef.current) {
33+
stream.getTracks().forEach((track) => track.stop())
34+
return
35+
}
36+
3037
streamRef.current = stream
3138

32-
if (videoRef.current) {
39+
if (videoRef.current && isMountedRef.current) {
3340
videoRef.current.srcObject = stream
34-
await videoRef.current.play()
41+
try {
42+
await videoRef.current.play()
43+
} catch (playError) {
44+
if (playError instanceof Error && playError.name !== 'AbortError') {
45+
console.error('비디오 재생 중 오류 발생:', playError)
46+
}
47+
}
3548
}
3649
} catch (err) {
37-
console.error('카메라를 열 수 없습니다:', err)
50+
if (
51+
isMountedRef.current &&
52+
err instanceof Error &&
53+
err.name !== 'AbortError'
54+
) {
55+
console.error('카메라를 열 수 없습니다:', err)
56+
}
3857
}
3958
}, [isRearCamera])
4059

@@ -103,8 +122,10 @@ function useCamera(setPhoto: (photo: string | null) => void) {
103122
}, [])
104123

105124
useEffect(() => {
125+
isMountedRef.current = true
106126
openCamera()
107127
return () => {
128+
isMountedRef.current = false
108129
if (streamRef.current) {
109130
streamRef.current.getTracks().forEach((track) => track.stop())
110131
}

0 commit comments

Comments
 (0)