Update useCameraDevice.ts

This commit is contained in:
Marc Rousavy 2021-02-20 23:23:38 +01:00
parent 04fd5bb069
commit 93ec6a49b0

View File

@ -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 <Camera device={device} />
*/
@ -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();