@@ -135,6 +135,12 @@ interface ActiveRemotePoller {
135135 readonly demandCwds : Ref . Ref < ReadonlyMap < string , number > > ;
136136}
137137
138+ interface InFlightRefresh {
139+ readonly fiber : Fiber . Fiber < VcsStatusResult , GitManagerServiceError > ;
140+ readonly startedGeneration : number ;
141+ readonly pendingRerun : boolean ;
142+ }
143+
138144interface StreamStatusOptions {
139145 readonly automaticRemoteRefreshInterval ?: Effect . Effect < Duration . Duration , never > ;
140146}
@@ -162,6 +168,7 @@ export class VcsStatusBroadcaster extends Context.Service<
162168 readonly refreshLocalStatus : (
163169 cwd : string ,
164170 ) => Effect . Effect < VcsStatusLocalResult , GitManagerServiceError > ;
171+ readonly invalidateStatus : ( cwd : string ) => Effect . Effect < void > ;
165172 readonly refreshStatus : ( cwd : string ) => Effect . Effect < VcsStatusResult , GitManagerServiceError > ;
166173 readonly streamStatus : (
167174 input : VcsStatusInput ,
@@ -193,9 +200,8 @@ export const make = Effect.gen(function* () {
193200 ) ;
194201 const cacheRef = yield * Ref . make ( new Map < string , CachedVcsStatus > ( ) ) ;
195202 const pollersRef = yield * SynchronizedRef . make ( new Map < string , ActiveRemotePoller > ( ) ) ;
196- const inFlightRefreshesRef = yield * SynchronizedRef . make (
197- new Map < string , Fiber . Fiber < VcsStatusResult , GitManagerServiceError > > ( ) ,
198- ) ;
203+ const inFlightRefreshesRef = yield * SynchronizedRef . make ( new Map < string , InFlightRefresh > ( ) ) ;
204+ const invalidateGenerationsRef = yield * Ref . make ( new Map < string , number > ( ) ) ;
199205
200206 const getCachedStatus = Effect . fn ( "VcsStatusBroadcaster.getCachedStatus" ) ( function * (
201207 cwd : string ,
@@ -381,35 +387,120 @@ export const make = Effect.gen(function* () {
381387 return yield * updateCachedStatus ( cwd , local , remote , { publish : true } ) ;
382388 } ) ;
383389
384- const refreshStatus : VcsStatusBroadcaster [ "Service" ] [ "refreshStatus" ] = Effect . fn (
385- "VcsStatusBroadcaster.refreshStatus" ,
390+ const invalidateGenerationOf = ( cwd : string ) =>
391+ Ref . get ( invalidateGenerationsRef ) . pipe ( Effect . map ( ( generations ) => generations . get ( cwd ) ?? 0 ) ) ;
392+
393+ function startRefreshFiber (
394+ cwd : string ,
395+ startedGeneration : number ,
396+ ) : Effect . Effect < Fiber . Fiber < VcsStatusResult , GitManagerServiceError > > {
397+ return refreshStatusCore ( cwd ) . pipe (
398+ Effect . ensuring ( settleRefreshFiber ( cwd , startedGeneration ) ) ,
399+ Effect . forkIn ( broadcasterScope ) ,
400+ ) ;
401+ }
402+
403+ function settleRefreshFiber ( cwd : string , startedGeneration : number ) {
404+ return Effect . gen ( function * ( ) {
405+ const shouldRerun = yield * SynchronizedRef . modify ( inFlightRefreshesRef , ( inFlight ) => {
406+ const existing = inFlight . get ( cwd ) ;
407+ if ( existing === undefined || existing . startedGeneration !== startedGeneration ) {
408+ return [ false , inFlight ] as const ;
409+ }
410+ if ( ! existing . pendingRerun ) {
411+ const next = new Map ( inFlight ) ;
412+ next . delete ( cwd ) ;
413+ return [ false , next ] as const ;
414+ }
415+ const next = new Map ( inFlight ) ;
416+ next . set ( cwd , { ...existing , pendingRerun : false } ) ;
417+ return [ true , next ] as const ;
418+ } ) ;
419+ if ( ! shouldRerun ) {
420+ return ;
421+ }
422+
423+ const nextGeneration = yield * invalidateGenerationOf ( cwd ) ;
424+ const nextFiber = yield * startRefreshFiber ( cwd , nextGeneration ) ;
425+ yield * SynchronizedRef . update ( inFlightRefreshesRef , ( inFlight ) => {
426+ const existing = inFlight . get ( cwd ) ;
427+ if ( existing === undefined || existing . startedGeneration !== startedGeneration ) {
428+ return inFlight ;
429+ }
430+ const next = new Map ( inFlight ) ;
431+ next . set ( cwd , {
432+ fiber : nextFiber ,
433+ startedGeneration : nextGeneration ,
434+ pendingRerun : existing . pendingRerun ,
435+ } ) ;
436+ return next ;
437+ } ) ;
438+ } ) ;
439+ }
440+
441+ const invalidateStatus : VcsStatusBroadcaster [ "Service" ] [ "invalidateStatus" ] = Effect . fn (
442+ "VcsStatusBroadcaster.invalidateStatus" ,
386443 ) ( function * ( rawCwd ) {
387444 const cwd = yield * withFileSystem ( normalizeCwd ( rawCwd ) ) ;
388- // Overlapping callers share one invalidate+recompute. The work is forked
389- // into the broadcaster scope so interrupting a waiter cannot cancel it.
390- const fiber = yield * SynchronizedRef . modifyEffect ( inFlightRefreshesRef , ( inFlight ) => {
445+ yield * workflow . invalidateStatus ( cwd ) ;
446+ yield * Ref . update ( invalidateGenerationsRef , ( generations ) => {
447+ const next = new Map ( generations ) ;
448+ next . set ( cwd , ( generations . get ( cwd ) ?? 0 ) + 1 ) ;
449+ return next ;
450+ } ) ;
451+ // A mutation after an in-flight refresh started must not reuse that run.
452+ yield * SynchronizedRef . update ( inFlightRefreshesRef , ( inFlight ) => {
391453 const existing = inFlight . get ( cwd ) ;
392- if ( existing ) {
393- return Effect . succeed ( [ existing , inFlight ] as const ) ;
454+ if ( existing === undefined || existing . pendingRerun ) {
455+ return inFlight ;
394456 }
457+ const next = new Map ( inFlight ) ;
458+ next . set ( cwd , { ...existing , pendingRerun : true } ) ;
459+ return next ;
460+ } ) ;
461+ } ) ;
395462
396- return refreshStatusCore ( cwd ) . pipe (
397- Effect . ensuring (
398- SynchronizedRef . update ( inFlightRefreshesRef , ( current ) => {
463+ const refreshStatus : VcsStatusBroadcaster [ "Service" ] [ "refreshStatus" ] = Effect . fn (
464+ "VcsStatusBroadcaster.refreshStatus" ,
465+ ) ( function * ( rawCwd ) {
466+ const cwd = yield * withFileSystem ( normalizeCwd ( rawCwd ) ) ;
467+ // Same-generation callers share one invalidate+recompute. The work is
468+ // forked into the broadcaster scope so interrupting a waiter cannot
469+ // cancel it. A later invalidate marks that run stale and this waiter
470+ // joins one follow-up refresh after it completes.
471+ while ( true ) {
472+ const requestGeneration = yield * invalidateGenerationOf ( cwd ) ;
473+ const inFlight = yield * SynchronizedRef . modifyEffect ( inFlightRefreshesRef , ( current ) => {
474+ const existing = current . get ( cwd ) ;
475+ if ( existing ) {
476+ if ( existing . startedGeneration >= requestGeneration ) {
477+ return Effect . succeed ( [ existing , current ] as const ) ;
478+ }
479+ const joined = { ...existing , pendingRerun : true } ;
480+ const next = new Map ( current ) ;
481+ next . set ( cwd , joined ) ;
482+ return Effect . succeed ( [ joined , next ] as const ) ;
483+ }
484+
485+ return startRefreshFiber ( cwd , requestGeneration ) . pipe (
486+ Effect . map ( ( started ) => {
487+ const created : InFlightRefresh = {
488+ fiber : started ,
489+ startedGeneration : requestGeneration ,
490+ pendingRerun : false ,
491+ } ;
399492 const next = new Map ( current ) ;
400- next . delete ( cwd ) ;
401- return next ;
493+ next . set ( cwd , created ) ;
494+ return [ created , next ] as const ;
402495 } ) ,
403- ) ,
404- Effect . forkIn ( broadcasterScope ) ,
405- Effect . map ( ( started ) => {
406- const next = new Map ( inFlight ) ;
407- next . set ( cwd , started ) ;
408- return [ started , next ] as const ;
409- } ) ,
410- ) ;
411- } ) ;
412- return yield * Fiber . join ( fiber ) ;
496+ ) ;
497+ } ) ;
498+ const result = yield * Fiber . join ( inFlight . fiber ) ;
499+ const latestGeneration = yield * invalidateGenerationOf ( cwd ) ;
500+ if ( inFlight . startedGeneration >= latestGeneration ) {
501+ return result ;
502+ }
503+ }
413504 } ) ;
414505
415506 const makeRemoteRefreshLoop = (
@@ -621,6 +712,7 @@ export const make = Effect.gen(function* () {
621712 return VcsStatusBroadcaster . of ( {
622713 getStatus,
623714 refreshLocalStatus,
715+ invalidateStatus,
624716 refreshStatus,
625717 streamStatus,
626718 } ) ;
0 commit comments