Files
client/app/renderer/js/stream/directory.js
T
omar 7287fdb63e
ci / linux-x64 (push) Failing after 1m26s
ci / linux-arm64-cross (push) Failing after 2m20s
TeamsPeach desktop client: Electron UI, Rust core bridge, TS6 stream viewer, CI
2026-10-24 03:31:54 +03:00

92 lines
2.5 KiB
JavaScript

// Which clients are currently streaming, built only from server notifications
// (notifystreamstarted / notifystreaminfo / notifystreamupdated / notifystreamstopped).
export class StreamDirectory {
constructor() {
/** @type {Map<string, {id:string, clientId:number, name:string, fields:Record<string,string>}>} */
this.streams = new Map();
this.sfuSeen = false;
}
/** Returns true when the visible set changed. */
handle(name, items) {
let changed = false;
for (const item of items) {
switch (name) {
case 'notifystreamstarted':
case 'notifystreaminfo':
case 'notifystreamupdated':
changed = this._upsert(item, name !== 'notifystreamupdated') || changed;
break;
case 'notifystreamstopped':
changed = this._remove(item) || changed;
break;
case 'notifysfuaccessinfo':
if (!this.sfuSeen) changed = true;
this.sfuSeen = true;
break;
default:
break;
}
}
return changed;
}
_upsert(item, mayCreate) {
if (!item || !item.id) return false;
const id = String(item.id);
const existing = this.streams.get(id);
const clientId = item.clid !== undefined && item.clid !== '' ? Number(item.clid) : existing?.clientId;
if (!existing && (!mayCreate || !Number.isInteger(clientId))) return false;
const fields = { ...(existing?.fields ?? {}), ...item };
this.streams.set(id, {
id,
clientId,
name: typeof fields.name === 'string' ? fields.name : '',
fields,
});
return true;
}
_remove(item) {
if (item && item.id && this.streams.delete(String(item.id))) return true;
if (item && item.clid !== undefined) {
let removed = false;
for (const [id, s] of this.streams) {
if (s.clientId === Number(item.clid)) {
this.streams.delete(id);
removed = true;
}
}
return removed;
}
return false;
}
/** Drop streams of clients that are no longer on the server. */
prune(presentClientIds) {
let changed = false;
for (const [id, s] of this.streams) {
if (!presentClientIds.has(s.clientId)) {
this.streams.delete(id);
changed = true;
}
}
return changed;
}
byClient(clientId) {
for (const s of this.streams.values()) if (s.clientId === clientId) return s;
return null;
}
get(id) {
return this.streams.get(String(id)) ?? null;
}
clear() {
this.streams.clear();
this.sfuSeen = false;
}
}