@@ -2,52 +2,50 @@ import { RegistryContext, useAtomValue } from "@effect/atom-react";
22import { chooseLoadBalancedEnvironment } from "@t3tools/client-runtime/load-balancing" ;
33import type { EnvironmentId } from "@t3tools/contracts" ;
44import { Atom } from "effect/reactivity" ;
5- import { useCallback , useContext , useMemo } from "react" ;
5+ import { useContext } from "react" ;
66
77import { serverEnvironment } from "../state/server" ;
88
9+ // Keyed by content: callers derive the list every render.
10+ const hostResourcesAtom = Atom . family ( ( environmentIdsKey : string ) =>
11+ Atom . make ( ( get ) => ( {
12+ // Staleness is judged when a snapshot is read, which keeps render pure.
13+ evaluatedAt : Date . now ( ) ,
14+ resources : ( environmentIdsKey === ""
15+ ? [ ]
16+ : ( environmentIdsKey . split ( "\n" ) as EnvironmentId [ ] )
17+ ) . map ( ( environmentId ) => {
18+ const result = get ( serverEnvironment . hostResources ( { environmentId, input : { } } ) ) ;
19+ return {
20+ environmentId,
21+ resources : result . _tag === "Success" ? result . value : null ,
22+ receivedAt : result . _tag === "Success" ? result . timestamp : 0 ,
23+ pending : result . _tag === "Initial" || result . waiting ,
24+ failed : result . _tag === "Failure" ,
25+ } ;
26+ } ) ,
27+ } ) ) ,
28+ ) ;
29+
930/** Only mounted for unresolved automatic drafts, so idle clients do not poll hosts. */
1031export function useLoadBalancedEnvironment (
1132 environmentIds : readonly EnvironmentId [ ] ,
1233 weights : Readonly < Record < string , number > > ,
1334) {
1435 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 ) ;
36+ const refresh = ( ids : readonly EnvironmentId [ ] ) => {
37+ for ( const environmentId of ids ) {
38+ registry . refresh ( serverEnvironment . hostResources ( { environmentId, input : { } } ) ) ;
39+ }
40+ } ;
41+ const { evaluatedAt, resources } = useAtomValue ( hostResourcesAtom ( environmentIds . join ( "\n" ) ) ) ;
4442 const pending = resources . some ( ( resource ) => resource . pending ) ;
4543 const environmentId = chooseLoadBalancedEnvironment (
4644 resources . map ( ( resource ) => ( {
4745 ...resource ,
4846 weight : weights [ resource . environmentId ] ?? 50 ,
4947 } ) ) ,
50- Date . now ( ) ,
48+ evaluatedAt ,
5149 ) as EnvironmentId | null ;
5250 return {
5351 refresh,
0 commit comments