Skip to content
Source
Sign inSign up

HostFreezeToastCoordinator.ts

33 lines1.2 KB

1import { hostFreezeNotice } from "@t3tools/client-runtime/connection";
2​
3import { appAtomRegistry } from "../rpc/atomRegistry";
4import { environmentSummaries } from "../state/presentation";
5import { environmentServerConfigsAtom } from "../state/server";
6import { toastManager } from "./ui/toast";
7​
8/** Tells the user why a host dropped, once per stall, when it reports one on reconnect. */
9export function startHostFreezeToasts(): () => void {
10 const shown = new Set<string>();
11 return appAtomRegistry.subscribe(
12 environmentServerConfigsAtom,
13 (configs) => {
14 for (const [environmentId, config] of configs) {
15 const stall = config.lastEventLoopStall;
16 if (stall === undefined) continue;
17 const key = `${environmentId}:${stall.detectedAt}`;
18 if (shown.has(key)) continue;
19 shown.add(key);
20 const label =
21 appAtomRegistry
22 .get(environmentSummaries.identitiesAtom)
23 .find((identity) => identity.environmentId === environmentId)?.label ??
24 config.environment.label;
25 const notice = hostFreezeNotice(label, stall);
26 if (notice !== null) {
27 toastManager.add({ type: "warning", ...notice });
28 }
29 }
30 },
31 { immediate: true },
32 );