1- import { describe , expect , it } from "@effect/vitest" ;
1+ import { describe , expect , it , vi } from "@effect/vitest" ;
22import { EnvironmentId } from "@t3tools/contracts" ;
33import * as Cause from "effect/Cause" ;
44import * as Deferred from "effect/Deferred" ;
@@ -81,6 +81,10 @@ function queryConnectionState(
8181
8282const makeEnvironmentQueryHarness = Effect . fn ( "TestEnvironmentQuery.makeHarness" ) ( function * < A , E > (
8383 execute : Effect . Effect < A , E > ,
84+ {
85+ refreshTrigger,
86+ idleTtlMs,
87+ } : { readonly refreshTrigger ?: Atom . Atom < unknown > ; readonly idleTtlMs ?: number } = { } ,
8488) {
8589 const supervisorState = yield * SubscriptionRef . make ( queryConnectionState ( ) ) ;
8690 const supervisorSession = yield * SubscriptionRef . make ( Option . some ( QUERY_RPC_SESSION ) ) ;
@@ -111,6 +115,8 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
111115 label : "test.environment-query" ,
112116 staleTimeMs : 60_000 ,
113117 execute : ( ) => execute ,
118+ ...( refreshTrigger === undefined ? { } : { refreshTrigger : ( ) => refreshTrigger } ) ,
119+ ...( idleTtlMs === undefined ? { } : { idleTtlMs } ) ,
114120 } ) ;
115121
116122 return {
@@ -120,6 +126,18 @@ const makeEnvironmentQueryHarness = Effect.fn("TestEnvironmentQuery.makeHarness"
120126 } ;
121127} ) ;
122128
129+ /**
130+ * Reads a query's settled value without leaving it mounted. `getResult` resumes from inside the
131+ * query's listener, so it only unsubscribes once the reader yields.
132+ */
133+ const readEnvironmentQuery = < A , E > (
134+ registry : AtomRegistry . AtomRegistry ,
135+ atom : Atom . Atom < AsyncResult . AsyncResult < A , E > > ,
136+ ) =>
137+ AtomRegistry . getResult ( registry , atom , { suspendOnWaiting : true } ) . pipe (
138+ Effect . tap ( ( ) => Effect . yieldNow ) ,
139+ ) ;
140+
123141const mountEnvironmentQuery = Effect . fn ( "TestEnvironmentQuery.mount" ) ( function * < A , E > (
124142 atom : Atom . Atom < AsyncResult . AsyncResult < A , E > > ,
125143) {
@@ -548,6 +566,92 @@ describe("environment query lifecycle", () => {
548566 } ) ,
549567 ) ,
550568 ) ;
569+
570+ it . effect ( "refreshes on its trigger while read, and once on its next read when idle" , ( ) =>
571+ Effect . scoped (
572+ Effect . gen ( function * ( ) {
573+ let executions = 0 ;
574+ const trigger = Atom . make ( 0 ) . pipe ( Atom . keepAlive ) ;
575+ const harness = yield * makeEnvironmentQueryHarness (
576+ Effect . sync ( ( ) => ( executions += 1 ) ) ,
577+ { refreshTrigger : trigger } ,
578+ ) ;
579+ const registry = AtomRegistry . make ( ) ;
580+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => registry . dispose ( ) ) ) ;
581+ const read = readEnvironmentQuery ( registry , harness . atom ) ;
582+
583+ const unmount = registry . mount ( harness . atom ) ;
584+ expect ( yield * read ) . toBe ( 1 ) ;
585+ registry . set ( trigger , 1 ) ;
586+ expect ( yield * read ) . toBe ( 2 ) ;
587+
588+ // Unmounted, the query stays alive on its idle TTL, but nothing reads it.
589+ unmount ( ) ;
590+ registry . set ( trigger , 2 ) ;
591+ registry . set ( trigger , 3 ) ;
592+ expect ( executions ) . toBe ( 2 ) ;
593+
594+ expect ( yield * read ) . toBe ( 3 ) ;
595+ } ) ,
596+ ) ,
597+ ) ;
598+
599+ it . effect ( "keeps an idle query when its trigger recomputes to the same value" , ( ) =>
600+ Effect . scoped (
601+ Effect . gen ( function * ( ) {
602+ let executions = 0 ;
603+ const settings = Atom . make ( { theme : "dark" } ) . pipe ( Atom . keepAlive ) ;
604+ const trigger = Atom . make ( ( get ) => JSON . stringify ( [ get ( settings ) !== null ] ) ) ;
605+ const harness = yield * makeEnvironmentQueryHarness (
606+ Effect . sync ( ( ) => ( executions += 1 ) ) ,
607+ { refreshTrigger : trigger } ,
608+ ) ;
609+ const registry = AtomRegistry . make ( ) ;
610+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => registry . dispose ( ) ) ) ;
611+ const read = readEnvironmentQuery ( registry , harness . atom ) ;
612+
613+ const unmount = registry . mount ( harness . atom ) ;
614+ expect ( yield * read ) . toBe ( 1 ) ;
615+ unmount ( ) ;
616+ registry . set ( settings , { theme : "light" } ) ;
617+ expect ( yield * read ) . toBe ( 1 ) ;
618+ expect ( executions ) . toBe ( 1 ) ;
619+ } ) ,
620+ ) ,
621+ ) ;
622+
623+ it . effect ( "refreshes an idle query the registry swept and rebuilt after its trigger moved" , ( ) =>
624+ Effect . scoped (
625+ Effect . gen ( function * ( ) {
626+ // Only the registry's idle-TTL sweeps run on these; effects keep their own scheduler.
627+ vi . useFakeTimers ( { toFake : [ "setTimeout" , "clearTimeout" , "Date" ] } ) ;
628+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => vi . useRealTimers ( ) ) ) ;
629+ let executions = 0 ;
630+ const trigger = Atom . make ( 0 ) . pipe ( Atom . keepAlive ) ;
631+ const harness = yield * makeEnvironmentQueryHarness (
632+ Effect . sync ( ( ) => ( executions += 1 ) ) ,
633+ { refreshTrigger : trigger , idleTtlMs : 1_000 } ,
634+ ) ;
635+ const registry = AtomRegistry . make ( { timeoutResolution : 100 } ) ;
636+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => registry . dispose ( ) ) ) ;
637+ const read = readEnvironmentQuery ( registry , harness . atom ) ;
638+
639+ const unmount = registry . mount ( harness . atom ) ;
640+ expect ( yield * read ) . toBe ( 1 ) ;
641+ unmount ( ) ;
642+ // Lets the registry start the query node's idle TTL.
643+ yield * Effect . yieldNow ;
644+ vi . advanceTimersByTime ( 500 ) ;
645+ registry . set ( trigger , 1 ) ;
646+ yield * Effect . yieldNow ;
647+ // Sweeps the query node but not the data it read, whose idle TTL started with the trigger.
648+ vi . advanceTimersByTime ( 700 ) ;
649+ expect ( executions ) . toBe ( 1 ) ;
650+
651+ expect ( yield * read ) . toBe ( 2 ) ;
652+ } ) ,
653+ ) ,
654+ ) ;
551655} ) ;
552656
553657describe ( "Atom.fn mutation semantics" , ( ) => {
0 commit comments