1+ import { initialState , Task , TaskStatus } from "@lit/task" ;
12import type { ReactiveController , ReactiveControllerHost } from "lit" ;
23import type { GatewayBrowserClient } from "../api/gateway.ts" ;
34import type { ApplicationGatewaySnapshot } from "../app/context.ts" ;
@@ -29,10 +30,8 @@ export class SessionPullRequestIndicatorsController implements ReactiveControlle
2930 private client : GatewayBrowserClient | null = null ;
3031 private agentId : string | null = null ;
3132 private connected = false ;
32- private epoch = 0 ;
3333 private eligibleSignature = "" ;
34- private refresh : Promise < void > | null = null ;
35- private refreshAgain = false ;
34+ private readonly refreshTask : Task ;
3635 private refreshTimer : ReturnType < typeof globalThis . setTimeout > | null = null ;
3736 private refreshScheduled = false ;
3837
@@ -41,6 +40,62 @@ export class SessionPullRequestIndicatorsController implements ReactiveControlle
4140 private readonly options : SessionPullRequestIndicatorsOptions ,
4241 ) {
4342 host . addController ( this ) ;
43+ this . refreshTask = new Task ( host , {
44+ autoRun : false ,
45+ // Rows are represented by a deterministic primitive so Lit can shallow-compare args.
46+ args : ( ) => [ null as GatewayBrowserClient | null , "" , "" ] as const ,
47+ task : async ( [ client , selectedAgentId , signature ] , { signal } ) => {
48+ if ( ! client || ! signature ) {
49+ return initialState ;
50+ }
51+ const eligibleRows = this . options
52+ . getRows ( )
53+ . filter ( ( session ) => ! session . isChild && session . worktreeId ) ;
54+ const currentSignature = JSON . stringify (
55+ eligibleRows . map ( ( session ) => [ session . key , session . worktreeId ] ) ,
56+ ) ;
57+ if ( currentSignature !== signature ) {
58+ return initialState ;
59+ }
60+ const entries : Array < readonly [ string , IndicatorEntry ] > = [ ] ;
61+ for ( const session of eligibleRows ) {
62+ if ( signal . aborted ) {
63+ break ;
64+ }
65+ try {
66+ const state = await fetchSessionPullRequestIndicatorState ( {
67+ client,
68+ pullRequestsAvailable : true ,
69+ sessionKey : session . key ,
70+ agentId : parseAgentSessionKey ( session . key ) ?. agentId ?? selectedAgentId ,
71+ } ) ;
72+ if ( state !== null && session . worktreeId ) {
73+ entries . push ( [ session . key , { state, worktreeId : session . worktreeId } ] ) ;
74+ }
75+ } catch {
76+ // Optional metadata: preserve the last-known indicator and retry next poll.
77+ }
78+ }
79+ return { client, entries } ;
80+ } ,
81+ onComplete : ( { client, entries } ) => {
82+ if ( this . options . getSnapshot ( ) ?. client !== client ) {
83+ return ;
84+ }
85+ let changed = false ;
86+ for ( const [ sessionKey , entry ] of entries ) {
87+ const current = this . states . get ( sessionKey ) ;
88+ if ( current ?. state !== entry . state || current . worktreeId !== entry . worktreeId ) {
89+ this . states . set ( sessionKey , entry ) ;
90+ changed = true ;
91+ }
92+ }
93+ if ( changed ) {
94+ this . host . requestUpdate ( ) ;
95+ }
96+ this . scheduleRefreshTimer ( ) ;
97+ } ,
98+ } ) ;
4499 }
45100
46101 hostConnected ( ) : void {
@@ -95,9 +150,11 @@ export class SessionPullRequestIndicatorsController implements ReactiveControlle
95150 }
96151
97152 private reset ( requestUpdate : boolean ) : void {
98- this . epoch += 1 ;
153+ const shouldInvalidate = this . eligibleSignature !== "" ;
99154 this . eligibleSignature = "" ;
100- this . refreshAgain = false ;
155+ if ( shouldInvalidate ) {
156+ void this . refreshTask . run ( [ null , "" , "" ] ) ;
157+ }
101158 this . clearRefreshTimer ( ) ;
102159 if ( this . states . size === 0 ) {
103160 return ;
@@ -140,102 +197,26 @@ export class SessionPullRequestIndicatorsController implements ReactiveControlle
140197 this . host . requestUpdate ( ) ;
141198 }
142199 if ( eligibleRows . length === 0 ) {
200+ const shouldInvalidate = this . eligibleSignature !== "" ;
143201 this . eligibleSignature = "" ;
144202 this . clearRefreshTimer ( ) ;
203+ if ( shouldInvalidate ) {
204+ void this . refreshTask . run ( [ null , "" , "" ] ) ;
205+ }
145206 return ;
146207 }
147208
148209 const signature = JSON . stringify (
149210 eligibleRows . map ( ( session ) => [ session . key , session . worktreeId ] ) ,
150211 ) ;
151212 if ( ! force && signature === this . eligibleSignature ) {
152- if ( this . refresh === null ) {
213+ if ( this . refreshTask . status !== TaskStatus . PENDING ) {
153214 this . scheduleRefreshTimer ( ) ;
154215 }
155216 return ;
156217 }
157218 this . eligibleSignature = signature ;
158- if ( this . refresh ) {
159- this . refreshAgain = true ;
160- return ;
161- }
162-
163219 this . clearRefreshTimer ( ) ;
164- const epoch = this . epoch ;
165- const refresh = this . load ( {
166- client : snapshot . client ,
167- selectedAgentId,
168- eligibleRows,
169- epoch,
170- signature,
171- } ) ;
172- this . refresh = refresh ;
173- void refresh . finally ( ( ) => {
174- if ( this . refresh !== refresh ) {
175- return ;
176- }
177- this . refresh = null ;
178- if ( ! this . connected ) {
179- return ;
180- }
181- if ( this . refreshAgain ) {
182- this . refreshAgain = false ;
183- this . refreshVisible ( true ) ;
184- return ;
185- }
186- if ( epoch === this . epoch && signature === this . eligibleSignature ) {
187- this . scheduleRefreshTimer ( ) ;
188- }
189- } ) ;
190- }
191-
192- private async load ( params : {
193- client : GatewayBrowserClient ;
194- selectedAgentId : string ;
195- eligibleRows : readonly SidebarRecentSession [ ] ;
196- epoch : number ;
197- signature : string ;
198- } ) : Promise < void > {
199- for ( const session of params . eligibleRows ) {
200- if ( ! this . isCurrent ( params ) ) {
201- return ;
202- }
203- try {
204- const indicatorState = await fetchSessionPullRequestIndicatorState ( {
205- client : params . client ,
206- pullRequestsAvailable : true ,
207- sessionKey : session . key ,
208- agentId : parseAgentSessionKey ( session . key ) ?. agentId ?? params . selectedAgentId ,
209- } ) ;
210- if ( indicatorState === null || ! this . isCurrent ( params ) ) {
211- continue ;
212- }
213- const worktreeId = session . worktreeId ;
214- const current = this . states . get ( session . key ) ;
215- if (
216- worktreeId &&
217- ( current ?. state !== indicatorState || current . worktreeId !== worktreeId )
218- ) {
219- this . states . set ( session . key , { state : indicatorState , worktreeId } ) ;
220- this . host . requestUpdate ( ) ;
221- }
222- } catch {
223- // Optional metadata: preserve the last-known indicator and retry next poll.
224- }
225- }
226- }
227-
228- private isCurrent ( params : {
229- client : GatewayBrowserClient ;
230- epoch : number ;
231- signature : string ;
232- } ) : boolean {
233- return (
234- this . connected &&
235- this . options . getConnected ( ) &&
236- params . epoch === this . epoch &&
237- params . signature === this . eligibleSignature &&
238- this . options . getSnapshot ( ) ?. client === params . client
239- ) ;
220+ void this . refreshTask . run ( [ snapshot . client , selectedAgentId , signature ] ) ;
240221 }
241222}
0 commit comments