@@ -11,6 +11,7 @@ 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" ;
1415import * as Option from "effect/Option" ;
1516import * as Schema from "effect/Schema" ;
1617import * as Stream from "effect/Stream" ;
@@ -96,6 +97,13 @@ interface EnvironmentQueryAtomOptions<Input, A, E, R> extends Omit<
9697 readonly persistence ?: QueryPersistence < A > ;
9798 readonly staleTimeMs ?: number ;
9899 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 ;
99107 readonly refreshIntervalMs ?: number ;
100108 readonly refreshTrigger ?: ( target : {
101109 readonly environmentId : EnvironmentIdType ;
@@ -673,6 +681,12 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
673681 const family = Atom . family ( ( key : string ) => {
674682 const target = parseEnvironmentRpcKey < Input > ( key ) ;
675683 const idleTtlMs = options . idleTtlMs ?? 5 * 60_000 ;
684+ // `Atom.swr` checks staleness only when its node builds. To revalidate on
685+ // remount, the wrappers drop on unmount and the source alone keeps the
686+ // cached value warm.
687+ const keepWarm = Atom . setIdleTTL ( idleTtlMs ) ;
688+ const keepSourceWarm = options . revalidateOnRemount ? keepWarm : identity ;
689+ const keepWrapperWarm = options . revalidateOnRemount ? identity : keepWarm ;
676690 const queryAtom = runtime
677691 . atom <
678692 A ,
@@ -797,11 +811,12 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
797811 } ) ;
798812 } )
799813 . pipe (
814+ keepSourceWarm ,
800815 Atom . swr ( {
801816 staleTime : options . staleTimeMs ?? 30_000 ,
802817 revalidateOnMount : true ,
803818 } ) ,
804- Atom . setIdleTTL ( idleTtlMs ) ,
819+ keepWrapperWarm ,
805820 ) ;
806821 const isFinal = ( result : AsyncResult . AsyncResult < A , unknown > ) =>
807822 options . isFinal !== undefined && Option . exists ( AsyncResult . value ( result ) , options . isFinal ) ;
@@ -814,7 +829,7 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
814829 refreshTrigger === undefined
815830 ? intervalQuery
816831 : intervalQuery . pipe ( refreshOnSignalWhileRead ( refreshTrigger , isFinal ) )
817- ) . pipe ( Atom . setIdleTTL ( idleTtlMs ) , Atom . withLabel ( `${ options . label } :${ key } ` ) ) ;
832+ ) . pipe ( keepWrapperWarm , Atom . withLabel ( `${ options . label } :${ key } ` ) ) ;
818833 } ) ;
819834 return ( target ) => family ( environmentRpcKey ( target ) ) ;
820835}
@@ -912,6 +927,7 @@ export function createEnvironmentRpcQueryAtomFamily<
912927 readonly persistence ?: QueryPersistence < EnvironmentRpcSuccess < TTag > > ;
913928 readonly staleTimeMs ?: number ;
914929 readonly idleTtlMs ?: number ;
930+ readonly revalidateOnRemount ?: boolean ;
915931 readonly refreshIntervalMs ?: number ;
916932 readonly refreshTrigger ?: ( target : {
917933 readonly environmentId : EnvironmentIdType ;
@@ -926,6 +942,9 @@ export function createEnvironmentRpcQueryAtomFamily<
926942 ...( options . persistence === undefined ? { } : { persistence : options . persistence } ) ,
927943 ...( options . staleTimeMs === undefined ? { } : { staleTimeMs : options . staleTimeMs } ) ,
928944 ...( options . idleTtlMs === undefined ? { } : { idleTtlMs : options . idleTtlMs } ) ,
945+ ...( options . revalidateOnRemount === undefined
946+ ? { }
947+ : { revalidateOnRemount : options . revalidateOnRemount } ) ,
929948 ...( options . refreshIntervalMs === undefined
930949 ? { }
931950 : { refreshIntervalMs : options . refreshIntervalMs } ) ,
0 commit comments