@@ -17,7 +17,7 @@ import * as Stream from "effect/Stream";
1717import * as SubscriptionRef from "effect/SubscriptionRef" ;
1818import { AsyncResult , Atom , AtomRegistry } from "effect/reactivity" ;
1919
20- import type { ConnectionAttemptError } from "../connection/model.ts" ;
20+ import type { ConnectionAttemptError , SupervisorConnectionState } from "../connection/model.ts" ;
2121import * as EnvironmentRegistry from "../connection/registry.ts" ;
2222import {
2323 type EnvironmentRpcInput ,
@@ -560,22 +560,28 @@ const refreshEveryUntilFinal =
560560export const selectEnvironmentQueryData = < A , E > ( result : AsyncResult . AsyncResult < A , E > ) : A | null =>
561561 Option . getOrNull ( AsyncResult . value ( result ) ) ;
562562
563- export function createEnvironmentQueryAtomFamily < R , ER , Input , A , E > (
563+ type EnvironmentConnection = readonly [
564+ SupervisorConnectionState ,
565+ Option . Option < RpcSession . RpcSession > ,
566+ ] ;
567+
568+ const connectionAtomsByRuntime = new WeakMap <
569+ object ,
570+ (
571+ environmentId : EnvironmentIdType ,
572+ ) => Atom . Atom < AsyncResult . AsyncResult < EnvironmentConnection | null > >
573+ > ( ) ;
574+
575+ /**
576+ * One connection atom per environment for every query on a runtime, so a reader of the session
577+ * generation and a query it re-runs see the same connection change at once.
578+ */
579+ function environmentConnectionAtoms < R , ER > (
564580 runtime : Atom . AtomRuntime < EnvironmentRegistry . EnvironmentRegistry | R , ER > ,
565- options : EnvironmentQueryAtomOptions <
566- Input ,
567- A ,
568- E ,
569- | EnvironmentSupervisor . EnvironmentSupervisor
570- | EnvironmentRegistry . EnvironmentRegistry
571- | AtomRegistry . AtomRegistry
572- | R
573- > ,
574- ) : ( target : {
575- readonly environmentId : EnvironmentIdType ;
576- readonly input : Input ;
577- } ) => Atom . Atom < AsyncResult . AsyncResult < A , E | ER | Error > > {
578- const connectionAtom = Atom . family ( ( environmentId : EnvironmentIdType ) =>
581+ ) {
582+ const existing = connectionAtomsByRuntime . get ( runtime ) ;
583+ if ( existing !== undefined ) return existing ;
584+ const family = Atom . family ( ( environmentId : EnvironmentIdType ) =>
579585 runtime . atom (
580586 followStreamInEnvironment (
581587 environmentId ,
@@ -591,7 +597,61 @@ export function createEnvironmentQueryAtomFamily<R, ER, Input, A, E>(
591597 ) ,
592598 { initialValue : null } ,
593599 ) ,
594- ) ;
600+ ) as (
601+ environmentId : EnvironmentIdType ,
602+ ) => Atom . Atom < AsyncResult . AsyncResult < EnvironmentConnection | null > > ;
603+ connectionAtomsByRuntime . set ( runtime , family ) ;
604+ return family ;
605+ }
606+
607+ const sessionGenerationAtomsByRuntime = new WeakMap <
608+ object ,
609+ ( environmentId : EnvironmentIdType ) => Atom . Atom < number >
610+ > ( ) ;
611+
612+ /**
613+ * The generation of the environment's latest established session, kept while it reconnects.
614+ * It changes only when a new session comes up, so a one-shot read that failed while the
615+ * environment was unreachable can watch it to run again once it is back.
616+ */
617+ export function environmentSessionGenerationAtom < R , ER > (
618+ runtime : Atom . AtomRuntime < EnvironmentRegistry . EnvironmentRegistry | R , ER > ,
619+ environmentId : EnvironmentIdType ,
620+ ) : Atom . Atom < number > {
621+ let family = sessionGenerationAtomsByRuntime . get ( runtime ) ;
622+ if ( family === undefined ) {
623+ const connectionAtom = environmentConnectionAtoms ( runtime ) ;
624+ family = Atom . family ( ( id : EnvironmentIdType ) =>
625+ Atom . make ( ( get ) : number => {
626+ const connection = Option . getOrNull ( AsyncResult . value ( get ( connectionAtom ( id ) ) ) ) ;
627+ return connection !== null &&
628+ connection [ 0 ] . phase === "connected" &&
629+ Option . isSome ( connection [ 1 ] )
630+ ? connection [ 0 ] . generation
631+ : Option . getOrElse ( get . self < number > ( ) , ( ) => 0 ) ;
632+ } ) . pipe ( Atom . withLabel ( `environment-session-generation:${ id } ` ) ) ,
633+ ) ;
634+ sessionGenerationAtomsByRuntime . set ( runtime , family ) ;
635+ }
636+ return family ( environmentId ) ;
637+ }
638+
639+ export function createEnvironmentQueryAtomFamily < R , ER , Input , A , E > (
640+ runtime : Atom . AtomRuntime < EnvironmentRegistry . EnvironmentRegistry | R , ER > ,
641+ options : EnvironmentQueryAtomOptions <
642+ Input ,
643+ A ,
644+ E ,
645+ | EnvironmentSupervisor . EnvironmentSupervisor
646+ | EnvironmentRegistry . EnvironmentRegistry
647+ | AtomRegistry . AtomRegistry
648+ | R
649+ > ,
650+ ) : ( target : {
651+ readonly environmentId : EnvironmentIdType ;
652+ readonly input : Input ;
653+ } ) => Atom . Atom < AsyncResult . AsyncResult < A , E | ER | Error > > {
654+ const connectionAtom = environmentConnectionAtoms ( runtime ) ;
595655 const family = Atom . family ( ( key : string ) => {
596656 const target = parseEnvironmentRpcKey < Input > ( key ) ;
597657 const idleTtlMs = options . idleTtlMs ?? 5 * 60_000 ;
0 commit comments