From 0f119fe23e837e0d8c2a8c882b1aaf3b62cbc7d2 Mon Sep 17 00:00:00 2001 From: streamich Date: Tue, 4 Feb 2020 01:05:13 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20don't=20throw=20in=20useM?= =?UTF-8?q?ediaDevices=20on=20missing=20browser=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/useMediaDevices.ts | 25 +++++++++++++------------ src/util.ts | 4 ++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/useMediaDevices.ts b/src/useMediaDevices.ts index 6130e02cec..d9601274d8 100644 --- a/src/useMediaDevices.ts +++ b/src/useMediaDevices.ts @@ -10,17 +10,16 @@ const useMediaDevices = () => { let mounted = true; const onChange = () => { - navigator.mediaDevices && - navigator.mediaDevices - .enumerateDevices() - .then(devices => { - if (mounted) { - setState({ - devices: devices.map(({ deviceId, groupId, kind, label }) => ({ deviceId, groupId, kind, label })), - }); - } - }) - .catch(noop); + navigator.mediaDevices + .enumerateDevices() + .then(devices => { + if (mounted) { + setState({ + devices: devices.map(({ deviceId, groupId, kind, label }) => ({ deviceId, groupId, kind, label })), + }); + } + }) + .catch(noop); }; on(navigator.mediaDevices, 'devicechange', onChange); @@ -35,4 +34,6 @@ const useMediaDevices = () => { return state; }; -export default useMediaDevices; +const useMediaDevicesMock = () => ({}); + +export default typeof navigator === 'object' && !!navigator.mediaDevices ? useMediaDevices : useMediaDevicesMock; diff --git a/src/util.ts b/src/util.ts index 76d6778c18..5e44fdcf75 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,5 +1,5 @@ export const isClient = typeof window === 'object'; -export const on = (obj: any, ...args: any[]) => obj && obj.addEventListener(...args); +export const on = (obj: any, ...args: any[]) => obj.addEventListener(...args); -export const off = (obj: any, ...args: any[]) => obj && obj.removeEventListener(...args); +export const off = (obj: any, ...args: any[]) => obj.removeEventListener(...args);