|
| 1 | +import type { RoomAnnounced } from "@dist/rust"; |
| 2 | +import { Room, RoomAction } from "@dist/rust"; |
| 3 | +import type { MoqPublishElement } from "./publish"; |
| 4 | +import { MoqWatchElement } from "./watch"; |
| 5 | + |
| 6 | +export class MoqMeetElement extends HTMLElement { |
| 7 | + #room: Room; |
| 8 | + #publish?: MoqPublishElement; |
| 9 | + |
| 10 | + #broadcasts: HTMLDivElement; |
| 11 | + |
| 12 | + // Work-around to make sure we only have one instance of each user. |
| 13 | + #unique: Map<string, number> = new Map(); |
| 14 | + |
| 15 | + static get observedAttributes() { |
| 16 | + return ["room"]; |
| 17 | + } |
| 18 | + |
| 19 | + constructor() { |
| 20 | + super(); |
| 21 | + |
| 22 | + this.#room = new Room(); |
| 23 | + |
| 24 | + const announced = this.#room.announced(); |
| 25 | + this.#runAnnounced(announced).finally(() => announced.free()); |
| 26 | + |
| 27 | + const shadow = this.attachShadow({ mode: "open" }); |
| 28 | + shadow.innerHTML = ` |
| 29 | + <style type="text/css"> |
| 30 | + .broadcasts { |
| 31 | + display: flex; |
| 32 | + gap: 8px; |
| 33 | + align-items: center; |
| 34 | +
|
| 35 | + moq-watch, moq-publish { |
| 36 | + border-radius: 0.375rem; |
| 37 | + overflow: hidden; |
| 38 | + } |
| 39 | + } |
| 40 | +
|
| 41 | + </style> |
| 42 | + `; |
| 43 | + |
| 44 | + this.#broadcasts = document.createElement("div"); |
| 45 | + this.#broadcasts.className = "broadcasts"; |
| 46 | + |
| 47 | + shadow.appendChild(this.#broadcasts); |
| 48 | + } |
| 49 | + |
| 50 | + connectedCallback() { |
| 51 | + for (const name of MoqMeetElement.observedAttributes) { |
| 52 | + const value = this.getAttribute(name); |
| 53 | + if (value !== undefined) { |
| 54 | + this.attributeChangedCallback(name, null, value); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + disconnectedCallback() {} |
| 60 | + |
| 61 | + attributeChangedCallback(name: string, old: string | null, value: string | null) { |
| 62 | + if (old === value) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + switch (name) { |
| 67 | + case "room": |
| 68 | + this.#room.url = value; |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + async #runAnnounced(announced: RoomAnnounced) { |
| 74 | + while (true) { |
| 75 | + const announce = await announced.next(); |
| 76 | + if (!announce) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + console.log(announce); |
| 81 | + |
| 82 | + switch (announce.action) { |
| 83 | + case RoomAction.Join: |
| 84 | + this.#join(announce.name); |
| 85 | + break; |
| 86 | + case RoomAction.Leave: |
| 87 | + this.#leave(announce.name); |
| 88 | + break; |
| 89 | + case RoomAction.Live: |
| 90 | + // TODO |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + #join(name: string) { |
| 97 | + const count = this.#unique.get(name); |
| 98 | + if (count) { |
| 99 | + this.#unique.set(name, count + 1); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + this.#unique.set(name, 1); |
| 104 | + |
| 105 | + const watch = new MoqWatchElement(); |
| 106 | + watch.id = name; |
| 107 | + watch.url = `${this.room}/${name}`; |
| 108 | + this.#broadcasts.appendChild(watch); |
| 109 | + } |
| 110 | + |
| 111 | + #leave(name: string) { |
| 112 | + let count = this.#unique.get(name); |
| 113 | + const watch = this.#broadcasts.querySelector(`#${name}`) as MoqWatchElement | null; |
| 114 | + |
| 115 | + if (!count || !watch) { |
| 116 | + throw new Error("user not found"); |
| 117 | + } |
| 118 | + |
| 119 | + count -= 1; |
| 120 | + |
| 121 | + if (count === 0) { |
| 122 | + this.#unique.delete(name); |
| 123 | + watch.remove(); |
| 124 | + } else { |
| 125 | + this.#unique.set(name, count); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + get room(): string | null { |
| 130 | + return this.getAttribute("room"); |
| 131 | + } |
| 132 | + |
| 133 | + set room(value: string | null) { |
| 134 | + if (value === null || value === "") { |
| 135 | + this.removeAttribute("room"); |
| 136 | + } else { |
| 137 | + this.setAttribute("room", value); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +customElements.define("moq-meet", MoqMeetElement); |
| 143 | + |
| 144 | +declare global { |
| 145 | + interface HTMLElementTagNameMap { |
| 146 | + "moq-meet": MoqMeetElement; |
| 147 | + } |
| 148 | +} |
0 commit comments