Skip to content

Commit

Permalink
fix: camera scanner is not working
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov committed Oct 30, 2024
1 parent f534267 commit 2743153
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions src/components/camera-qr-scanner/camera-qr-scanner.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,16 @@
import { onDestroy, onMount } from 'svelte';
let videoEl: HTMLVideoElement | null = $state(null);
let qrScanner: QrScanner;
let qrScanner: QrScanner | null;
let { scanned }: { scanned: (arg: { data: string }) => void } = $props();
let availableCameraDevices: MediaDeviceInfo[] | null = $state(null);
let selectedCamera: MediaDeviceInfo;
let selectedCamera: MediaDeviceInfo | null = $state(null);
let isScanning = $state(true);
let scannerStartingPromise: Promise<void> | null;
async function startQrScanner(camera: MediaDeviceInfo) {
if (qrScanner && camera) {
if (scannerStartingPromise) {
scannerStartingPromise.then(() => startQrScanner(camera));
return;
}
await qrScanner.setCamera(camera.deviceId);
await qrScanner.start();
scannerStartingPromise = null;
}
}
$effect(() => {
if (isScanning) {
startQrScanner(selectedCamera);
} else {
qrScanner?.stop();
}
});
$effect(() => {
if (videoEl) {
if (qrScanner) {
qrScanner.stop();
Expand All @@ -52,6 +32,23 @@
},
);
}
if (qrScanner && camera) {
if (scannerStartingPromise) {
scannerStartingPromise.then(() => startQrScanner(camera));
return;
}
await qrScanner.setCamera(camera.deviceId);
await qrScanner.start();
scannerStartingPromise = null;
}
}
$effect(() => {
if (isScanning && selectedCamera) {
startQrScanner(selectedCamera);
} else {
qrScanner?.stop();
}
});
async function loadCameraDevices() {
Expand Down

0 comments on commit 2743153

Please sign in to comment.