UNPKG

electron-window-rtc

Version:

Exchange media streams between Electron windows with WebRTC.

116 lines (115 loc) 3.49 kB
var c = Object.defineProperty; var g = (t, n, e) => n in t ? c(t, n, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[n] = e; var w = (t, n, e) => g(t, typeof n != "symbol" ? n + "" : n, e); import { ipcMain as d } from "electron"; var r = /* @__PURE__ */ ((t) => (t.Log = "electron-window-rtc:log", t.Signal = "electron-window-rtc:signal", t.GetOwnWindowName = "electron-window-rtc:registered-window:own-name", t.GetRegisteredWindows = "electron-window-rtc:registered-windows:get", t))(r || {}); const o = class o { constructor() { /** * An `Array` of registered `BrowserWindow` with unique names. */ w(this, "registeredWindows", []); if (o.instance) return o.instance; d.handle( r.GetOwnWindowName, this.getOwnNameCallback.bind(this) ), d.handle( r.GetRegisteredWindows, this.getWindowsCallback.bind(this) ), d.handle(r.Signal, this.signalCallback.bind(this)), o.instance = this; } /** * Dispose the singleton. */ dispose() { d.removeHandler(r.GetOwnWindowName), d.removeHandler(r.GetRegisteredWindows), d.removeHandler(r.Signal), o.instance = null; } /** * Register a `BrowserWindow` for message forwarding. * * @param { string } name The name the window will be registered with. * @param { BrowserWindow } window The window. */ register(n, e) { const i = n.trim(); if (this.registeredWindows.find( (s) => s.name === i || s.window === e )) throw new Error( `A window with name '${i}' is already registered.` ); this.registeredWindows.push({ name: i, window: e }), e.on("close", () => { this.unregister(i); }); } /** * Unregister a `BrowserWindow` by its name. * * @param { string } name The name the window has been registered with. */ unregister(n) { const e = n.trim(), i = this.registeredWindows.find( (s) => s.name === e ); if (!i) return; for (const s of this.registeredWindows) s.name !== e && s.window.webContents.send(r.Signal, { channel: "peer-left", sender: e, receiver: s.name, payload: void 0 }); const a = this.registeredWindows.indexOf(i); this.registeredWindows.splice(a, 1); } /** * Callback for `WindowRTCIpcChannel.GetOwnName` * @returns { Promise<string>} The name of the requesting window. */ async getOwnNameCallback(n) { const e = this.registeredWindows.find( (i) => i.window.webContents === n.sender ); if (!e) throw new Error( "Unable to find window own name. It may not be properly registered." ); return e.name; } /** * Callback for `WindowRTCIpcChannel.GetWindows` * @returns { Promise<string[]>} The registered windows names. */ async getWindowsCallback() { return this.registeredWindows.map( (n) => n.name ); } /** * Callback for `WindowRTCIpcChannel.Signal` * @returns { Promise<undefined | Error>} An `Error` if signaling message couldn't be forwarded (window was not found). */ async signalCallback(n, e) { const i = this.registeredWindows.find( (a) => a.name === e.receiver ); if (!i) return new Error( `Receiver window with name '${e.receiver}' could not be found.` ); i.window.webContents.send(r.Signal, e); } }; /** * Singleton instance. */ w(o, "instance", null); let l = o; const f = new l(); export { f as WindowRTCMain };