@@ -29,6 +29,8 @@ const RECONNECT_DELAY_MS = 3_000;
2929/** How often to create a fresh session (sessions have 10-min TTL). */
3030const SESSION_REFRESH_MS = 8 * 60 * 1000 ; // 8 minutes — well before expiry
3131
32+ const WAITER_PREFIX = "__glance_waiter_pi_" ;
33+
3234interface SessionResponse {
3335 id : string ;
3436 url : string ;
@@ -56,6 +58,7 @@ let currentSession: SessionResponse | null = null;
5658let sessionCreatedAt = 0 ;
5759let abortController : AbortController | null = null ;
5860let running = false ;
61+ let waiterCounter = 0 ;
5962
6063async function createSession ( ) : Promise < SessionResponse > {
6164 const res = await fetch ( `${ BASE_URL } /api/session` , { method : "POST" } ) ;
@@ -128,6 +131,11 @@ function sleep(ms: number): Promise<void> {
128131 return new Promise ( ( r ) => setTimeout ( r , ms ) ) ;
129132}
130133
134+ function nextWaiterKey ( ) : string {
135+ waiterCounter += 1 ;
136+ return `${ WAITER_PREFIX } ${ Date . now ( ) } _${ waiterCounter } ` ;
137+ }
138+
131139// ── SSE listener (multi-image) ─────────────────────────────────────
132140
133141/**
@@ -222,7 +230,7 @@ function waitForNextImage(signal?: AbortSignal): Promise<ImageEvent | null> {
222230
223231 // Poll: check for new images by watching the background loop.
224232 // We do this by subscribing to a one-time callback.
225- const key = `__glance_waiter_ ${ Date . now ( ) } ` ;
233+ const key = nextWaiterKey ( ) ;
226234 ( globalThis as any ) [ key ] = ( image : ImageEvent ) => {
227235 clearTimeout ( timeout ) ;
228236 delete ( globalThis as any ) [ key ] ;
@@ -239,7 +247,7 @@ function waitForNextImage(signal?: AbortSignal): Promise<ImageEvent | null> {
239247
240248function getWaiterKeys ( ) : string [ ] {
241249 return Object . keys ( globalThis ) . filter ( ( key ) =>
242- key . startsWith ( "__glance_waiter_" ) ,
250+ key . startsWith ( WAITER_PREFIX ) ,
243251 ) ;
244252}
245253
@@ -274,6 +282,7 @@ export const __testing = {
274282 stopBackground ( ) ;
275283 sessionCreatedAt = 0 ;
276284 running = false ;
285+ waiterCounter = 0 ;
277286 clearWaiters ( ) ;
278287 } ,
279288 setSession ( session : SessionResponse | null , createdAt = Date . now ( ) ) {
0 commit comments