Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle undefined mediaDevices #830

Merged
merged 3 commits into from Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/useMediaDevices.ts
Expand Up @@ -10,16 +10,17 @@ const useMediaDevices = () => {
let mounted = true;

const onChange = () => {
navigator.mediaDevices
.enumerateDevices()
.then(devices => {
if (mounted) {
setState({
devices: devices.map(({ deviceId, groupId, kind, label }) => ({ deviceId, groupId, kind, label })),
});
}
})
.catch(noop);
navigator.mediaDevices &&
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 Down
4 changes: 2 additions & 2 deletions src/util.ts
@@ -1,5 +1,5 @@
export const isClient = typeof window === 'object';

export const on = (obj: any, ...args: any[]) => obj.addEventListener(...args);
export const on = (obj: any, ...args: any[]) => obj && obj.addEventListener(...args);
Copy link
Contributor

@xobotyi xobotyi Jan 30, 2020

Choose a reason for hiding this comment

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

Not sure if these functions should be touched at all. With your changes in some cases handler bind will silently fail and development or debug will be much harder. Better to have error in that case.

You should leave only changes in src/useMediaDevices.ts, update the PR and ill merge it.

Copy link
Contributor

Choose a reason for hiding this comment

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

As you forbid to make changes in your fork repo it should be like so:

useMediaDevices:

import { useEffect, useState } from 'react';
import { noop, off, on } from './util';

const useMediaDevices = () => {
  const [state, setState] = useState({});

  useEffect(() => {
    if (!navigator.mediaDevices) return;

    let mounted = true;

    const onChange = () => {
      navigator.mediaDevices
        .enumerateDevices()
        .then(devices => {
          mounted &&
            setState({
              devices: devices.map(({ deviceId, groupId, kind, label }) => ({ deviceId, groupId, kind, label })),
            });
        })
        .catch(noop);
    };

    on(navigator.mediaDevices, 'devicechange', onChange);
    onChange();

    return () => {
      mounted = false;
      off(navigator.mediaDevices, 'devicechange', onChange);
    };
  }, []);

  return state;
};

export default useMediaDevices;

util

export const isClient = typeof window === 'object';

export const on = (obj: any, ...args: any[]) => obj.addEventListener(...args);

export const off = (obj: any, ...args: any[]) => obj.removeEventListener(...args);

export const noop = () => {};


export const off = (obj: any, ...args: any[]) => obj.removeEventListener(...args);
export const off = (obj: any, ...args: any[]) => obj && obj.removeEventListener(...args);