Skip to content

Commit 33672c4

Browse files
perf(web): the load-balancing hook compiles with the React Compiler (pingdotgg#20565)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2520821 commit 33672c4

1 file changed

Lines changed: 42 additions & 31 deletions

File tree

‎apps/web/src/hooks/useLoadBalancedEnvironment.ts‎

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,63 @@ import { RegistryContext, useAtomValue } from "@effect/atom-react";
22
import { chooseLoadBalancedEnvironment } from "@t3tools/client-runtime/load-balancing";
33
import type { EnvironmentId } from "@t3tools/contracts";
44
import { Atom } from "effect/reactivity";
5-
import { useCallback, useContext, useMemo } from "react";
5+
import { useContext } from "react";
66

77
import { serverEnvironment } from "../state/server";
88

9+
// Mirrors the freshness window in chooseLoadBalancedEnvironment.
10+
const HOST_RESOURCES_FRESH_MS = 15_000;
11+
12+
// Keyed by content: callers derive the list every render.
13+
const hostResourcesAtom = Atom.family((environmentIdsKey: string) =>
14+
Atom.make((get) => {
15+
// Staleness is judged when a snapshot is read, which keeps render pure.
16+
const evaluatedAt = Date.now();
17+
const resources = (
18+
environmentIdsKey === "" ? [] : (environmentIdsKey.split("\n") as EnvironmentId[])
19+
).map((environmentId) => {
20+
const result = get(serverEnvironment.hostResources({ environmentId, input: {} }));
21+
return {
22+
environmentId,
23+
resources: result._tag === "Success" ? result.value : null,
24+
receivedAt: result._tag === "Success" ? result.timestamp : 0,
25+
pending: result._tag === "Initial" || result.waiting,
26+
failed: result._tag === "Failure",
27+
};
28+
});
29+
// Re-read when the next fresh snapshot ages out, so a frozen evaluatedAt never selects it.
30+
const nextExpiry = Math.min(
31+
...resources
32+
.map((resource) => resource.receivedAt + HOST_RESOURCES_FRESH_MS)
33+
.filter((expiresAt) => expiresAt >= evaluatedAt),
34+
);
35+
if (Number.isFinite(nextExpiry)) {
36+
const timer = setTimeout(() => get.refreshSelf(), nextExpiry - evaluatedAt + 1);
37+
get.addFinalizer(() => clearTimeout(timer));
38+
}
39+
return { evaluatedAt, resources };
40+
}),
41+
);
42+
943
/** Only mounted for unresolved automatic drafts, so idle clients do not poll hosts. */
1044
export function useLoadBalancedEnvironment(
1145
environmentIds: readonly EnvironmentId[],
1246
weights: Readonly<Record<string, number>>,
1347
) {
1448
const registry = useContext(RegistryContext);
15-
const refresh = useCallback(
16-
(ids: readonly EnvironmentId[]) => {
17-
for (const environmentId of ids) {
18-
registry.refresh(serverEnvironment.hostResources({ environmentId, input: {} }));
19-
}
20-
},
21-
[registry],
22-
);
23-
// Keyed by content: callers derive the list every render.
24-
const environmentIdsKey = environmentIds.join("\n");
25-
const resourcesAtom = useMemo(
26-
() =>
27-
Atom.make((get) =>
28-
(environmentIdsKey === "" ? [] : (environmentIdsKey.split("\n") as EnvironmentId[])).map(
29-
(environmentId) => {
30-
const result = get(serverEnvironment.hostResources({ environmentId, input: {} }));
31-
return {
32-
environmentId,
33-
resources: result._tag === "Success" ? result.value : null,
34-
receivedAt: result._tag === "Success" ? result.timestamp : 0,
35-
pending: result._tag === "Initial" || result.waiting,
36-
failed: result._tag === "Failure",
37-
};
38-
},
39-
),
40-
),
41-
[environmentIdsKey],
42-
);
43-
const resources = useAtomValue(resourcesAtom);
49+
const refresh = (ids: readonly EnvironmentId[]) => {
50+
for (const environmentId of ids) {
51+
registry.refresh(serverEnvironment.hostResources({ environmentId, input: {} }));
52+
}
53+
};
54+
const { evaluatedAt, resources } = useAtomValue(hostResourcesAtom(environmentIds.join("\n")));
4455
const pending = resources.some((resource) => resource.pending);
4556
const environmentId = chooseLoadBalancedEnvironment(
4657
resources.map((resource) => ({
4758
...resource,
4859
weight: weights[resource.environmentId] ?? 50,
4960
})),
50-
Date.now(),
61+
evaluatedAt,
5162
) as EnvironmentId | null;
5263
return {
5364
refresh,

0 commit comments

Comments
 (0)