44 type EnvironmentId ,
55 type ServerConfig ,
66} from "@t3tools/contracts" ;
7+ import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime" ;
78import { useEffect , useRef } from "react" ;
89
910import { useClientSettings , useClientSettingsHydrated } from "~/hooks/useSettings" ;
@@ -13,6 +14,10 @@ import { previewEnvironment } from "~/state/preview";
1314import { useEnvironmentsWithScope } from "~/state/session" ;
1415import { useAtomCommand } from "~/state/use-atom-command" ;
1516
17+ /** A failed report retries this many times, waiting longer each time. */
18+ const MAX_REPORT_RETRIES = 3 ;
19+ const REPORT_RETRY_DELAY_MS = 5_000 ;
20+
1621const selectProfiles = ( settings : { readonly browserProfiles : ReadonlyArray < BrowserProfile > } ) =>
1722 settings . browserProfiles ;
1823const selectDefaultProfileId = ( settings : { readonly browserDefaultProfileId : string } ) =>
@@ -44,32 +49,64 @@ export function BrowserProfileReporter() {
4449 }
4550 > ( ) ,
4651 ) ;
52+ /** Consecutive failed reports per connected environment. */
53+ const failures = useRef ( new Map < EnvironmentId , number > ( ) ) ;
54+ const retryTimer = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
55+ /** The latest report pass, for a retry to run with current settings. */
56+ const sweep = useRef < ( ) => void > ( ( ) => { } ) ;
57+
58+ useEffect (
59+ ( ) => ( ) => {
60+ if ( retryTimer . current !== null ) clearTimeout ( retryTimer . current ) ;
61+ } ,
62+ [ ] ,
63+ ) ;
4764
4865 useEffect ( ( ) => {
4966 if ( ! hydrated ) return ;
50- for ( const environmentId of reported . current . keys ( ) ) {
51- if ( ! connected . includes ( environmentId ) ) reported . current . delete ( environmentId ) ;
52- }
53- for ( const environmentId of connected ) {
54- const config = serverConfigs . get ( environmentId ) ;
55- if ( ! config ?. environment . capabilities . serverBrowser || ! operable . has ( environmentId ) ) continue ;
56- const last = reported . current . get ( environmentId ) ;
57- if (
58- last ?. config === config &&
59- last . profiles === profiles &&
60- last . defaultProfileId === defaultProfileId
61- ) {
62- continue ;
67+ const reportChanged = ( ) => {
68+ for ( const environmentId of reported . current . keys ( ) ) {
69+ if ( ! connected . includes ( environmentId ) ) reported . current . delete ( environmentId ) ;
6370 }
64- const entry = { config, profiles, defaultProfileId } ;
65- reported . current . set ( environmentId , entry ) ;
66- void report ( { environmentId, input : { profiles, defaultProfileId } } ) . then ( ( result ) => {
67- // Unsent: the next run of this effect tries again.
68- if ( result . _tag === "Failure" && reported . current . get ( environmentId ) === entry ) {
69- reported . current . delete ( environmentId ) ;
71+ for ( const environmentId of failures . current . keys ( ) ) {
72+ if ( ! connected . includes ( environmentId ) ) failures . current . delete ( environmentId ) ;
73+ }
74+ for ( const environmentId of connected ) {
75+ const config = serverConfigs . get ( environmentId ) ;
76+ if ( ! config ?. environment . capabilities . serverBrowser || ! operable . has ( environmentId ) )
77+ continue ;
78+ const last = reported . current . get ( environmentId ) ;
79+ if (
80+ last ?. config === config &&
81+ last . profiles === profiles &&
82+ last . defaultProfileId === defaultProfileId
83+ ) {
84+ continue ;
7085 }
71- } ) ;
72- }
86+ const entry = { config, profiles, defaultProfileId } ;
87+ reported . current . set ( environmentId , entry ) ;
88+ void report ( { environmentId, input : { profiles, defaultProfileId } } ) . then ( ( result ) => {
89+ if ( result . _tag === "Success" ) {
90+ failures . current . delete ( environmentId ) ;
91+ return ;
92+ }
93+ // A newer report replaced this one, which needs no retry.
94+ if ( isAtomCommandInterrupted ( result ) || reported . current . get ( environmentId ) !== entry ) {
95+ return ;
96+ }
97+ reported . current . delete ( environmentId ) ;
98+ const attempt = ( failures . current . get ( environmentId ) ?? 0 ) + 1 ;
99+ failures . current . set ( environmentId , attempt ) ;
100+ if ( attempt > MAX_REPORT_RETRIES || retryTimer . current !== null ) return ;
101+ retryTimer . current = setTimeout ( ( ) => {
102+ retryTimer . current = null ;
103+ sweep . current ( ) ;
104+ } , REPORT_RETRY_DELAY_MS * attempt ) ;
105+ } ) ;
106+ }
107+ } ;
108+ sweep . current = reportChanged ;
109+ reportChanged ( ) ;
73110 } , [ connected , defaultProfileId , hydrated , operable , profiles , report , serverConfigs ] ) ;
74111
75112 return null ;
0 commit comments