Skip to content

Commit

Permalink
fix: Allow getCameraDevice to return undefined when no Devices ar…
Browse files Browse the repository at this point in the history
…e available (e.g. iOS Simulator) (#1848)

fix: Allow `getCameraDevice` to return `undefined` when no Devices are available
  • Loading branch information
mrousavy authored Sep 26, 2023
1 parent bdc3fd0 commit f7428f2
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions package/src/devices/getCameraDevice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CameraDevice, CameraPosition, PhysicalCameraDeviceType } from '../CameraDevice';
import { CameraRuntimeError } from '../CameraError';

export interface DeviceFilter {
/**
Expand All @@ -24,7 +23,7 @@ export interface DeviceFilter {
}

/**
* Get the best matching Camera device that best satisfies your requirements using a sorting filter.
* Get the best matching Camera device that best satisfies your requirements using a sorting filter, or `undefined` if {@linkcode devices} does not contain any devices.
* @param position The position of the Camera device relative to the phone.
* @param filter The filter you want to use. The Camera device that matches your filter the closest will be returned
* @returns The Camera device that matches your filter the closest, or `undefined` if no such Camera Device exists on the given {@linkcode position}.
Expand All @@ -36,13 +35,13 @@ export interface DeviceFilter {
* })
* ```
*/
export function getCameraDevice(devices: CameraDevice[], position: CameraPosition, filter: DeviceFilter = {}): CameraDevice {
export function getCameraDevice(devices: CameraDevice[], position: CameraPosition, filter: DeviceFilter = {}): CameraDevice | undefined {
const explicitlyWantsNonWideAngle = filter.physicalDevices != null && !filter.physicalDevices.includes('wide-angle-camera');

const filtered = devices.filter((d) => d.position === position);

let bestDevice = filtered[0];
if (bestDevice == null) throw new CameraRuntimeError('device/invalid-device', 'No Camera Device could be found!');
if (bestDevice == null) return undefined;

This comment has been minimized.

Copy link
@Romick2005

Romick2005 Oct 2, 2023

Contributor

Wouldn't it better to have it like:
if (! bestDevice) return null;
Because at first glance let bestDevice = filtered[0]; if there are no items in filtered array -> bestDevice will contain undefined value by default.


// Compare each device using a point scoring system
for (const device of devices) {
Expand Down

1 comment on commit f7428f2

@vercel
Copy link

@vercel vercel bot commented on f7428f2 Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.