OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-media / client.js

68 lines · javascript ref:
import e from "ready-resource"
import {
  isImageSupported as r,
  isMediaSupported as t,
  isVideoSupported as s,
} from "bare-media/types"
import i from "./shared/spec/hrpc/index.js"
const o = [
  "createChatImagePreview",
  "createChatVideoPreview",
  "createChatLinkPreview",
  "createProfileAvatar",
  "createRoomAvatar",
  "decodeImage",
]
export class WorkerClient extends e {
  spawn = null
  worker = null
  rpc = null
  opts = null
  constructor(e) {
    super()
    for (const e of o) this.#e(e)
    this.initialize(e)
  }
  #e(e) {
    this[e] = async (...r) => (await this.ready(), this.rpc[e](...r))
  }
  initialize({
    spawn: e,
    filename: r = "node_modules/@holepunchto/keet-media/worker/index.js",
    requireSource: t,
    args: s,
  } = {}) {
    ;((this.spawn = e), (this.opts = { filename: r, requireSource: t, args: s }))
  }
  async _open() {
    this.#r()
  }
  async _close() {
    this.worker?.IPC.end()
  }
  #t() {
    ;((this.opening = null),
      (this.closing = null),
      (this.opened = !1),
      (this.closed = !1),
      (this.worker = null),
      (this.rpc = null))
  }
  #r() {
    const { filename: e, requireSource: r, args: t } = this.opts,
      s = r?.()
    this.worker = this.spawn(e, s, t)
    const o = this.worker.IPC
    ;(o.on("end", () => o.end()),
      o.on("close", () => {
        ;(this.#t(),
          this.onClose?.(),
          console.error("[keet-media] Worker has exited. IPC channel closed unexpectedly."))
      }),
      (this.rpc = new i(o)))
  }
  isImageSupported = r
  isVideoSupported = s
  isMediaSupported = t
}