Skip to content

Commit

Permalink
fix: πŸ› don't throw in useMediaDevices on missing browser API
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 4, 2020
1 parent 2cf2ab0 commit 0f119fe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
25 changes: 13 additions & 12 deletions src/useMediaDevices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -35,4 +34,6 @@ const useMediaDevices = () => {
return state;
};

export default useMediaDevices;
const useMediaDevicesMock = () => ({});

export default typeof navigator === 'object' && !!navigator.mediaDevices ? useMediaDevices : useMediaDevicesMock;
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 0f119fe

Please sign in to comment.