refactor: extract host runtime controller for connection management

This commit is contained in:
Mohamed Boudra
2026-02-19 21:19:51 +07:00
parent bff1a5accc
commit f8e3204572
9 changed files with 1077 additions and 215 deletions
+9 -15
View File
@@ -12,14 +12,13 @@ export type ConnectionProbeState =
export type SelectBestConnectionInput = {
candidates: ConnectionCandidate[];
preferredConnectionId: string | null;
probeByConnectionId: Map<string, ConnectionProbeState>;
};
export function selectBestConnection(
input: SelectBestConnectionInput
): string | null {
const { candidates, preferredConnectionId, probeByConnectionId } = input;
const { candidates, probeByConnectionId } = input;
if (candidates.length === 0) {
return null;
}
@@ -29,8 +28,9 @@ export function selectBestConnection(
.filter(({ candidate }) => {
const probe = probeByConnectionId.get(candidate.connectionId);
return probe?.status === "available";
})
.sort((left, right) => {
});
const byLatency = (left: { candidate: ConnectionCandidate; index: number }, right: { candidate: ConnectionCandidate; index: number }) => {
const leftProbe = probeByConnectionId.get(left.candidate.connectionId);
const rightProbe = probeByConnectionId.get(right.candidate.connectionId);
@@ -48,18 +48,12 @@ export function selectBestConnection(
}
return leftLatency - rightLatency;
});
};
if (available.length > 0) {
return available[0]!.candidate.connectionId;
if (available.length === 0) {
return null;
}
const preferred = candidates.find(
(candidate) => candidate.connectionId === preferredConnectionId
);
if (preferred) {
return preferred.connectionId;
}
return candidates[0]!.connectionId;
const sorted = available.sort(byLatency);
return sorted[0]!.candidate.connectionId;
}