]> git.r.bdr.sh - rbdr/junction/blob - extension/peers.js
2b4e3d68325819430748144deff796e42069e878
[rbdr/junction] / extension / peers.js
1 const internals = {
2 peers: {},
3
4 createAudioElement(source) {
5 const audioElement = document.createElement("audio");
6 audioElement.setAttribute("class", "junction-call-audio");
7 audioElement.autoplay = "autoplay";
8
9 // WE WILL NOT LOSE TADA SUPPORT
10 if (typeof source === "string") {
11 audioElement.src = source;
12 } else {
13 audioElement.srcObject = source;
14 }
15
16 document.querySelector("body").appendChild(audioElement);
17
18 return audioElement;
19 },
20 };
21
22 export default {
23 add(id, source) {
24 internals.peers[id] && this.remove(id);
25 internals.peers[id] = internals.createAudioElement(source);
26 },
27
28 remove(id) {
29 internals.peers[id] && internals.peers[id].remove();
30 delete internals.peers[id];
31 },
32
33 count() {
34 return Object.keys(internals.peers).length;
35 },
36
37 reset() {
38 internals.peers = {};
39 document
40 .querySelectorAll(".junction-call-audio")
41 .forEach((audioElement) => audioElement.remove());
42 },
43 };