@@ -11,7 +11,6 @@ import {
1111import * as Cause from "effect/Cause" ;
1212import * as Effect from "effect/Effect" ;
1313import * as Exit from "effect/Exit" ;
14- import { identity } from "effect/Function" ;
1514import * as Option from "effect/Option" ;
1615import * as Schema from "effect/Schema" ;
1716import * as Stream from "effect/Stream" ;
@@ -97,13 +96,6 @@ interface EnvironmentQueryAtomOptions<Input, A, E, R> extends Omit<
9796 readonly persistence ?: QueryPersistence < A > ;
9897 readonly staleTimeMs ?: number ;
9998 readonly idleTtlMs ?: number ;
100- /**
101- * Check staleness each time the query is mounted again. By default the idle
102- * TTL keeps the stale-while-revalidate node warm, so a remount within it
103- * reads the cached value without checking. The cached value still shows
104- * while the query revalidates.
105- */
106- readonly revalidateOnRemount ?: boolean ;
10799 readonly refreshIntervalMs ?: number ;
108100 readonly refreshTrigger ?: ( target : {
109101 readonly environmentId : EnvironmentIdType ;
@@ -567,6 +559,45 @@ const refreshEveryUntilFinal =
567559 { initialValueTarget : self } ,
568560 ) ;
569561
562+ const isStale = ( result : AsyncResult . AsyncResult < unknown , unknown > , staleTimeMs : number ) => {
563+ if ( result . waiting ) return false ;
564+ const success =
565+ result . _tag === "Failure" ? Option . getOrUndefined ( result . previousSuccess ) : result ;
566+ if ( success ?. _tag !== "Success" ) return result . _tag === "Failure" ;
567+ // @effect -diagnostics-next-line globalDate:off - `AsyncResult` timestamps come from `Date.now`.
568+ return Date . now ( ) - success . timestamp >= staleTimeMs ;
569+ } ;
570+
571+ /**
572+ * Stale-while-revalidate on every mount. `Atom.swr` checks only when its node builds, which an idle
573+ * TTL skips on a remount; this layer has no TTL (a transform drops it) while `self` keeps the cache.
574+ * It reads `self` as a dependency, not a subscription, so `self` goes idle with its last reader.
575+ */
576+ const revalidateOnMount =
577+ ( staleTimeMs : number ) =>
578+ < A , E > (
579+ self : Atom . Atom < AsyncResult . AsyncResult < A , E > > ,
580+ ) : Atom . Atom < AsyncResult . AsyncResult < A , E > > =>
581+ Atom . transform (
582+ self ,
583+ ( get ) => {
584+ const current = get ( self ) ;
585+ if ( Option . isNone ( get . self ( ) ) && isStale ( current , staleTimeMs ) ) {
586+ let active = true ;
587+ get . addFinalizer ( ( ) => {
588+ active = false ;
589+ } ) ;
590+ // Deferred out of the build, which runs while React renders. A plain read drops this
591+ // node before the task runs, so only a mount refetches.
592+ get . registry . schedulerAsync . makeDispatcher ( ) . scheduleTask ( ( ) => {
593+ if ( active && isStale ( get . once ( self ) , staleTimeMs ) ) get . refresh ( self ) ;
594+ } , 0 ) ;
595+ }
596+ return current ;
597+ } ,
598+ { initialValueTarget : self } ,
599+ ) ;
600+
570601/**
571602 * A query result's data alone. A refetch always publishes a new result (a new
572603 * timestamp, `waiting` on and off), while equal data keeps its identity, so a
@@ -685,15 +716,12 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
685716 readonly input : Input ;
686717} ) => Atom . Atom < AsyncResult . AsyncResult < A , E | ER | Error > > {
687718 const connectionAtom = environmentConnectionAtoms ( runtime ) ;
719+ // `Atom.family` holds members weakly and the mounted layer has no TTL, so each member lives
720+ // as long as its cache: keyed weakly, this lets both go once the cache's TTL ends.
721+ const membersByCache = new WeakMap < Atom . Atom < unknown > , Atom . Atom < unknown > > ( ) ;
688722 const family = Atom . family ( ( key : string ) => {
689723 const target = parseEnvironmentRpcKey < Input > ( key ) ;
690724 const idleTtlMs = options . idleTtlMs ?? 5 * 60_000 ;
691- // `Atom.swr` checks staleness only when its node builds. To revalidate on
692- // remount, the wrappers drop on unmount and the source alone keeps the
693- // cached value warm.
694- const keepWarm = Atom . setIdleTTL ( idleTtlMs ) ;
695- const keepSourceWarm = options . revalidateOnRemount ? keepWarm : identity ;
696- const keepWrapperWarm = options . revalidateOnRemount ? identity : keepWarm ;
697725 const queryAtom = runtime
698726 . atom <
699727 A ,
@@ -817,26 +845,25 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
817845 ) ;
818846 } ) ;
819847 } )
820- . pipe (
821- keepSourceWarm ,
822- Atom . swr ( {
823- staleTime : options . staleTimeMs ?? 30_000 ,
824- revalidateOnMount : true ,
825- } ) ,
826- keepWrapperWarm ,
827- ) ;
848+ . pipe ( Atom . setIdleTTL ( idleTtlMs ) ) ;
828849 const isFinal = ( result : AsyncResult . AsyncResult < A , unknown > ) =>
829850 options . isFinal !== undefined && Option . exists ( AsyncResult . value ( result ) , options . isFinal ) ;
830851 const intervalQuery =
831852 options . refreshIntervalMs === undefined
832853 ? queryAtom
833854 : queryAtom . pipe ( refreshEveryUntilFinal ( options . refreshIntervalMs , isFinal ) ) ;
834855 const refreshTrigger = options . refreshTrigger ?.( target ) ;
835- return (
856+ const cache = (
836857 refreshTrigger === undefined
837858 ? intervalQuery
838859 : intervalQuery . pipe ( refreshOnSignalWhileRead ( refreshTrigger , isFinal ) )
839- ) . pipe ( keepWrapperWarm , Atom . withLabel ( `${ options . label } :${ key } ` ) ) ;
860+ ) . pipe ( Atom . setIdleTTL ( idleTtlMs ) ) ;
861+ const member = cache . pipe (
862+ revalidateOnMount ( options . staleTimeMs ?? 30_000 ) ,
863+ Atom . withLabel ( `${ options . label } :${ key } ` ) ,
864+ ) ;
865+ membersByCache . set ( cache , member ) ;
866+ return member ;
840867 } ) ;
841868 return ( target ) => family ( environmentRpcKey ( target ) ) ;
842869}
@@ -934,7 +961,6 @@ export function createEnvironmentRpcQueryAtomFamily<
934961 readonly persistence ?: QueryPersistence < EnvironmentRpcSuccess < TTag > > ;
935962 readonly staleTimeMs ?: number ;
936963 readonly idleTtlMs ?: number ;
937- readonly revalidateOnRemount ?: boolean ;
938964 readonly refreshIntervalMs ?: number ;
939965 readonly refreshTrigger ?: ( target : {
940966 readonly environmentId : EnvironmentIdType ;
@@ -949,9 +975,6 @@ export function createEnvironmentRpcQueryAtomFamily<
949975 ...( options . persistence === undefined ? { } : { persistence : options . persistence } ) ,
950976 ...( options . staleTimeMs === undefined ? { } : { staleTimeMs : options . staleTimeMs } ) ,
951977 ...( options . idleTtlMs === undefined ? { } : { idleTtlMs : options . idleTtlMs } ) ,
952- ...( options . revalidateOnRemount === undefined
953- ? { }
954- : { revalidateOnRemount : options . revalidateOnRemount } ) ,
955978 ...( options . refreshIntervalMs === undefined
956979 ? { }
957980 : { refreshIntervalMs : options . refreshIntervalMs } ) ,
0 commit comments