WIP III improve UX [snapshot task 2]

This commit is contained in:
Schluffe committed 2026-10-16 16:09:43 +02:00
1 parent b2bab26981
commit 28c90d1d71
5 files changed
+97 -2

No files matched your search

+25
View File
@@ -43,6 +43,9 @@ export function summarizeStats(stats: Stats): string[] {
lines.push(`pair ${endpoint(pair.localCandidateId)} → ${endpoint(pair.remoteCandidateId)} ${category(pair.state, pairStates)}` +
` selected=${isSelected(pair)} nominated=${pair.nominated === true} ` + counters.map(key => `${key}=${count(pair[key])}`).join(' '));
}
stats.forEach((entry: StatsEntry) => {
if (entry.type === 'transport') lines.push(`transport dtls=${category(entry.dtlsState, ['new', 'connecting', 'connected', 'closed', 'failed'])}`);
});
if (pairs.length > 12) lines.push(`pairs omitted=${pairs.length - 12}`);
return lines;
}
@@ -51,6 +54,8 @@ export class ConnectionDiagnostics {
readonly lines: string[] = [];
private readonly started = performance.now();
private stopped = false;
private sampleTimers: ReturnType<typeof setTimeout>[] = [];
private samplingStarted = false;
private finishing: Promise<void> | undefined;
private readonly listeners: Array<[EventTarget, string, EventListener]> = [];
private readonly pc: RTCPeerConnection;
@@ -62,6 +67,7 @@ export class ConnectionDiagnostics {
this.listeners.push([target, event, listener]);
};
const state = () => {
this.sampleWhileChecking();
this.record(`pc=${category(pc.connectionState, ['new', 'connecting', 'connected', 'disconnected', 'failed', 'closed'])} ice=${category(pc.iceConnectionState, ['new', 'checking', 'connected', 'completed', 'disconnected', 'failed', 'closed'])} gathering=${category(pc.iceGatheringState, ['new', 'gathering', 'complete'])}`);
};
for (const event of ['connectionstatechange', 'iceconnectionstatechange', 'icegatheringstatechange']) listen(pc, event, state);
@@ -73,6 +79,23 @@ export class ConnectionDiagnostics {
}
state();
}
private sampleWhileChecking() {
if (this.stopped || this.finishing) return;
if (this.pc.iceConnectionState !== 'checking') {
for (const timer of this.sampleTimers) clearTimeout(timer);
this.sampleTimers = [];
return;
}
if (this.samplingStarted) return;
this.samplingStarted = true;
// Preserve evidence before browsers discard failed candidate pairs. Four
// bounded snapshots cover short setup failures without polling indefinitely.
this.sampleTimers = [3000, 6000, 9000, 12000].map(delay => setTimeout(() => {
if (this.stopped || this.finishing || this.pc.iceConnectionState !== 'checking') return;
this.record('connectivity sample');
void this.capture();
}, delay));
}
record(message: string) {
if (this.stopped) return;
this.lines.push(`${((performance.now() - this.started) / 1000).toFixed(1)}s ${message}`);
@@ -98,6 +121,8 @@ export class ConnectionDiagnostics {
// Request final stats while the connection is alive. Always close, even when
// getStats rejects or hangs. Reset can start a new session during this wait.
finish(close: () => void) {
for (const timer of this.sampleTimers) clearTimeout(timer);
this.sampleTimers = [];
return this.finishing ??= this.capture().finally(() => {
this.stopped = true;
for (const [target, event, listener] of this.listeners) target.removeEventListener(event, listener);