UNPKG

@fakes/media-devices

Version:

A interactive fake implementation of MediaDevices interface in the browser for testing

50 lines (49 loc) 1.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenMediaTracks = void 0; function assertUnreachable(_) { throw new Error("Didn't expect to get here"); } function toKind(toRemove) { if (toRemove === 'camera') { return 'videoinput'; } if (toRemove === 'microphone') { return 'audioinput'; } assertUnreachable(toRemove); } class OpenMediaTracks { constructor() { this.entries = []; } track(mediaDevice, mediaStreamTrack) { this.entries.push({ mediaDevice, mediaTrack: mediaStreamTrack }); } remove(toRemove) { const index = this.entries.findIndex((entry) => entry.mediaTrack === toRemove); if (index === -1) { return; } this.entries.splice(index, 1); } allFor(toRemove) { if (typeof toRemove === 'string') { const kind = toKind(toRemove); return this.entries .filter((entry) => { return entry.mediaDevice.kind === kind; }) .map((entry) => entry.mediaTrack); } return this.entries .filter((entry) => { const trackedDevice = entry.mediaDevice; return (trackedDevice.kind === toRemove.kind && trackedDevice.groupId === toRemove.groupId && trackedDevice.deviceId === toRemove.deviceId); }) .map((entry) => entry.mediaTrack); } } exports.OpenMediaTracks = OpenMediaTracks;