fix: prevent race condition in camera initialization

- Add isInitializingCamera flag to prevent concurrent calls to startCameraPreview
- Use finally block to ensure flag is always reset after initialization
This commit is contained in:
Johnpaul
2025-11-27 04:04:23 +01:00
parent 7b109df599
commit 8031a832f4

View File

@@ -95,6 +95,7 @@ const props = defineProps<{
const isCameraOn = ref(false)
const isShowingPreview = ref(false)
const isInitializingCamera = ref(false)
const originalWidgets = ref<IBaseWidget[]>([])
const videoRef = ref<HTMLVideoElement>()
const videoContainerRef = ref<HTMLElement>()
@@ -553,6 +554,10 @@ async function handleRetake() {
async function startCameraPreview() {
if (props.readonly) return
// Prevent concurrent camera initialization attempts
if (isInitializingCamera.value) return
isInitializingCamera.value = true
capturedImageUrl.value = null
try {
@@ -638,6 +643,8 @@ async function startCameraPreview() {
stopStreamTracks()
isShowingPreview.value = false
} finally {
isInitializingCamera.value = false
}
}