1- import { useAtomRefresh , useAtomValue } from "@effect/atom-react" ;
1+ import { RegistryContext , useAtomRefresh , useAtomValue } from "@effect/atom-react" ;
22import * as Cause from "effect/Cause" ;
33import * as Option from "effect/Option" ;
44import { AsyncResult , Atom } from "effect/reactivity" ;
5+ import { useContext } from "react" ;
56
67const EMPTY_ASYNC_RESULT_ATOM = Atom . make ( AsyncResult . initial < never , never > ( false ) ) . pipe (
78 Atom . withLabel ( "web-environment-query:empty" ) ,
@@ -24,6 +25,14 @@ export function formatEnvironmentQueryError(cause: Cause.Cause<unknown>): string
2425 : "The environment request failed." ;
2526}
2627
28+ function queryUpdatedAt ( result : AsyncResult . AsyncResult < unknown , unknown > ) : number {
29+ return result . _tag === "Success"
30+ ? result . timestamp
31+ : result . _tag === "Failure"
32+ ? ( Option . getOrNull ( result . previousSuccess ) ?. timestamp ?? 0 )
33+ : 0 ;
34+ }
35+
2736export function useEnvironmentQuery < A , E > (
2837 atom : Atom . Atom < AsyncResult . AsyncResult < A , E > > | null ,
2938) : EnvironmentQueryView < A , E > {
@@ -32,12 +41,7 @@ export function useEnvironmentQuery<A, E>(
3241 const refresh = useAtomRefresh ( selectedAtom ) ;
3342 return {
3443 data : Option . getOrNull ( AsyncResult . value ( result ) ) ,
35- dataUpdatedAt :
36- result . _tag === "Success"
37- ? result . timestamp
38- : result . _tag === "Failure"
39- ? ( Option . getOrNull ( result . previousSuccess ) ?. timestamp ?? 0 )
40- : 0 ,
44+ dataUpdatedAt : queryUpdatedAt ( result ) ,
4145 error : result . _tag === "Failure" ? formatEnvironmentQueryError ( result . cause ) : null ,
4246 failure :
4347 result . _tag === "Failure" ? Option . getOrNull ( Cause . findErrorOption ( result . cause ) ) : null ,
@@ -46,3 +50,59 @@ export function useEnvironmentQuery<A, E>(
4650 refresh,
4751 } ;
4852}
53+
54+ /**
55+ * Reads one part of a query's result. Every refetch publishes a new result (a
56+ * new timestamp, `waiting` on and off), and this re-renders only when the
57+ * selected part changes. `select` must be defined outside the component: a new
58+ * function per render would rebuild the derived atom every render.
59+ */
60+ export function useEnvironmentQuerySelector < A , E , B > (
61+ atom : Atom . Atom < AsyncResult . AsyncResult < A , E > > | null ,
62+ select : ( result : AsyncResult . AsyncResult < A , E > ) => B ,
63+ ) : B {
64+ return useAtomValue ( atom ?? EMPTY_ASYNC_RESULT_ATOM , select ) ;
65+ }
66+
67+ type QueryResultAtom = Atom . Atom < AsyncResult . AsyncResult < unknown , unknown > > ;
68+ // Lives as long as both queries do, so a row keeps the same comparison atom across renders.
69+ const queryIsNewerAtoms = new WeakMap <
70+ QueryResultAtom ,
71+ WeakMap < QueryResultAtom , Atom . Atom < boolean > >
72+ > ( ) ;
73+ function queryIsNewerAtom ( query : QueryResultAtom , than : QueryResultAtom ) : Atom . Atom < boolean > {
74+ let byThan = queryIsNewerAtoms . get ( query ) ;
75+ if ( byThan === undefined ) {
76+ byThan = new WeakMap ( ) ;
77+ queryIsNewerAtoms . set ( query , byThan ) ;
78+ }
79+ let atom = byThan . get ( than ) ;
80+ if ( atom === undefined ) {
81+ atom = Atom . make ( ( get ) => queryUpdatedAt ( get ( query ) ) >= queryUpdatedAt ( get ( than ) ) ) ;
82+ byThan . set ( than , atom ) ;
83+ }
84+ return atom ;
85+ }
86+
87+ /** Whether `query` last succeeded no earlier than `than`, without re-rendering on every refetch. */
88+ export function useEnvironmentQueryIsNewer (
89+ query : QueryResultAtom | null ,
90+ than : QueryResultAtom | null ,
91+ ) : boolean {
92+ return useAtomValue (
93+ queryIsNewerAtom ( query ?? EMPTY_ASYNC_RESULT_ATOM , than ?? EMPTY_ASYNC_RESULT_ATOM ) ,
94+ ) ;
95+ }
96+
97+ /**
98+ * The query's refresh, plus its pending state read when called rather than
99+ * subscribed to, for callers that only gate a refresh on it.
100+ */
101+ export function useEnvironmentQueryRefresh < A , E > (
102+ atom : Atom . Atom < AsyncResult . AsyncResult < A , E > > | null ,
103+ ) : { readonly refresh : ( ) => void ; readonly isPending : ( ) => boolean } {
104+ const registry = useContext ( RegistryContext ) ;
105+ const selectedAtom = atom ?? EMPTY_ASYNC_RESULT_ATOM ;
106+ const refresh = useAtomRefresh ( selectedAtom ) ;
107+ return { refresh, isPending : ( ) => atom !== null && registry . get ( selectedAtom ) . waiting } ;
108+ }
0 commit comments