Skip to content

Commit

Permalink
fix: optimize device enumeration
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlaz committed Oct 10, 2023
1 parent a2e4446 commit 85d9cc5
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/client/src/devices/devices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const getDevices = (
kind: MediaDeviceKind,
) => {
return new Observable<MediaDeviceInfo[]>((subscriber) => {
const logger = getLogger(['devices']);
const run = async () => {
const enumerate = async () => {
let devices = await navigator.mediaDevices.enumerateDevices();
// some browsers report empty device labels (Firefox).
// in that case, we need to request permissions (via getUserMedia)
Expand All @@ -42,15 +41,20 @@ const getDevices = (
if (mediaStream) disposeOfMediaStream(mediaStream);
}
}
// notify observers and complete
subscriber.next(devices);
subscriber.complete();
return devices;
};

run().catch((error) => {
logger('error', 'Failed to enumerate devices', error);
subscriber.error(error);
});
enumerate()
.then((devices) => {
// notify subscribers and complete
subscriber.next(devices);
subscriber.complete();
})
.catch((error) => {
const logger = getLogger(['devices']);
logger('error', 'Failed to enumerate devices', error);
subscriber.error(error);
});
});
};

Expand Down

0 comments on commit 85d9cc5

Please sign in to comment.