diff --git a/src/hooks/useCameraDevice.ts b/src/hooks/useCameraDevice.ts index 3361a70..b3e2db5 100644 --- a/src/hooks/useCameraDevice.ts +++ b/src/hooks/useCameraDevice.ts @@ -7,10 +7,10 @@ import { CameraDevice, LogicalCameraDeviceType, parsePhysicalDeviceTypes, Physic /** * Gets the best available `CameraDevice`. Devices with more cameras are preferred. * - * @returns A `CameraDevice` for the requested device type. + * @returns The best matching `CameraDevice`. * @throws `CameraRuntimeError` if no device was found. * @example - * const device = useCameraDevice('wide-angle-camera') + * const device = useCameraDevice() * // ... * return */ @@ -19,8 +19,8 @@ export function useCameraDevice(): CameraDevice; /** * Gets a `CameraDevice` for the requested device type. * - * @returns A `CameraDevice` for the requested device type, or `undefined` if no matching device was found - * + * @returns A `CameraDevice` for the requested device type. + * @throws `CameraRuntimeError` if no device was found. * @example * const device = useCameraDevice('wide-angle-camera') * // ... @@ -38,20 +38,20 @@ export function useCameraDevice(deviceType?: PhysicalCameraDeviceType | LogicalC const devices = await Camera.getAvailableCameraDevices(); if (!isMounted) return; + let bestMatch: CameraDevice | undefined; if (deviceType == null) { // use any device const sorted = devices.sort(sortDevices); - const bestMatch = sorted[0]; - if (bestMatch == null) throw new CameraRuntimeError('device/no-device', 'No Camera device was found!'); - setDevice(bestMatch); + bestMatch = sorted[0]; } else { // use specified device (type) - const bestMatch = devices.find((d) => { + bestMatch = devices.find((d) => { const parsedType = parsePhysicalDeviceTypes(d.devices); return parsedType === deviceType; }); - setDevice(bestMatch); } + if (bestMatch == null) throw new CameraRuntimeError('device/no-device', 'No Camera device was found!'); + setDevice(bestMatch); }; loadDevice();