OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-store / store / call / call-device.saga.js

72 lines · javascript ref:
import { put as e, all as d, select as i } from "redux-saga/effects"
import { getPreferencesData as c, PREFERENCES as l } from "../preferences"
import { userCustomNotification as t, NOTIFICATION_CODE as o } from "../notification"
import { taskErrorSaga as a } from "../error"
import {
  getAudioDevice as f,
  getAudioOutputDevice as n,
  getDeviceState as v,
  getVideoDevice as r,
  setAllDevices as s,
  setAudioDevice as u,
  setAudioOutputDevice as I,
  setVideoDevice as p,
  updateDevices as m,
} from "../media"
import { updateMyCameraStreamCmd as y } from "./call-settings.action"
import { getCallApi as D } from "../../backend"
import { apiTakeEvery as O } from "../lib"
export function* addDevicesHandler(d) {
  for (const i of d)
    yield e(t({ code: o.MEDIA_DEVICE_ADD, label: i.label, staticId: i.deviceStaticId }))
}
export function* updateDevicesHandler({ payload: { addedDevices: e } }) {
  yield addDevicesHandler(e)
}
export function* callDeviceHandler(t) {
  try {
    const { devicesMap: o, audio: a, video: D, audioOutput: O } = t,
      b = yield i(v),
      H = !b.isReady
    if ((yield e(s({ audio: a, audioOutput: O, devicesMap: o, video: D })), H)) return
    const x = Object.values(o),
      A = Object.values(b.devicesMap)
    yield e(
      m({
        addedDevices: x.filter((e) => !A.find((d) => d.deviceId === e.deviceId)),
        removedDevices: A.filter((e) => !x.find((d) => d.deviceId === e.deviceId)),
      }),
    )
    const [E, M, U, g] = yield d([i(c), i(r), i(f), i(n)]),
      _ = b.audioOutput.devices.find((e) => e.default),
      j = O.find((e) => e.default),
      S = (e) => e.deviceId === g.deviceId
    if (j && j.deviceId !== _?.deviceId) yield e(I(j))
    else if (!g || !O.find(S)) {
      const d = o[E[l.AUDIO_OUTPUT]]
      yield e(I(d || j || O[0] || null))
    }
    let T
    const V = b.video.devices.find((e) => e.default),
      h = D.find((e) => e.default)
    if (h && h.deviceId !== V?.deviceId) (yield e(p(h)), (T = !0))
    else if (!M || !D.find((e) => e.deviceId === M.deviceId)) {
      const d = o[E[l.VIDEO]]
      ;(yield e(p(d || h || D[0] || null)), (T = !0))
    }
    const k = b.audio.devices.find((e) => e.default),
      C = a.find((e) => e.default)
    if (C && C.deviceId !== k?.deviceId) (yield e(u(C)), (T = !0))
    else if (!U || !a.find((e) => e.deviceId === U.deviceId)) {
      const d = o[E[l.AUDIO]]
      ;(yield e(u(d || C || a[0] || null)), (T = !0))
    }
    T && (yield e(y()))
  } catch (e) {
    yield a("callDeviceHandler", e)
  }
}
export function* callDevicesSaga() {
  yield O([() => D().devices.subscribeDevices()], callDeviceHandler)
}