92 lines
2.5 KiB
JavaScript
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;
|
|
}
|
|
}
|