@@ -79,6 +79,7 @@ type TabCoordinatorMessage =
7979const CHANNEL_NAME = 'playground-tab-coordinator' ;
8080const ONE_DAY_MS = 24 * 60 * 60 * 1000 ;
8181const PING_TIMEOUT_MS = 150 ;
82+ const MIN_CHECK_INTERVAL_MS = 1000 ;
8283
8384let channel : BroadcastChannel | null = null ;
8485let currentTabInfo : TabInfo | null = null ;
@@ -87,6 +88,8 @@ let takeoverCallback: (() => void) | null = null;
8788let backupRequestCallback : ( ( ) => Promise < boolean > ) | null = null ;
8889let siteResetCallback : ( ( ) => void ) | null = null ;
8990let beforeUnloadHandler : ( ( ) => void ) | null = null ;
91+ let isCheckingTabs = false ;
92+ let lastCheckTime = 0 ;
9093
9194/**
9295 * Initialize the tab coordinator for a specific site.
@@ -168,6 +171,8 @@ export function destroyTabCoordinator(): void {
168171 takeoverCallback = null ;
169172 backupRequestCallback = null ;
170173 siteResetCallback = null ;
174+ isCheckingTabs = false ;
175+ lastCheckTime = 0 ;
171176}
172177
173178/**
@@ -185,8 +190,15 @@ export async function checkForExistingTabs(siteSlug: string): Promise<{
185190 return { existingTabs : [ ] , hasFreshTab : false , hasStaleTab : false } ;
186191 }
187192
188- const existingTabs : TabInfo [ ] = [ ] ;
193+ // Safeguard: prevent concurrent checks and rate limit
189194 const now = Date . now ( ) ;
195+ if ( isCheckingTabs || now - lastCheckTime < MIN_CHECK_INTERVAL_MS ) {
196+ return { existingTabs : [ ] , hasFreshTab : false , hasStaleTab : false } ;
197+ }
198+ isCheckingTabs = true ;
199+ lastCheckTime = now ;
200+
201+ const existingTabs : TabInfo [ ] = [ ] ;
190202
191203 const pongHandler = ( event : MessageEvent < TabCoordinatorMessage > ) => {
192204 const message = event . data ;
@@ -212,13 +224,15 @@ export async function checkForExistingTabs(siteSlug: string): Promise<{
212224
213225 channel . removeEventListener ( 'message' , pongHandler ) ;
214226
227+ const checkTime = Date . now ( ) ;
215228 const hasFreshTab = existingTabs . some (
216- ( tab ) => now - tab . createdAt < ONE_DAY_MS && ! tab . isDependentMode
229+ ( tab ) => checkTime - tab . createdAt < ONE_DAY_MS && ! tab . isDependentMode
217230 ) ;
218231 const hasStaleTab = existingTabs . some (
219- ( tab ) => now - tab . createdAt >= ONE_DAY_MS && ! tab . isDependentMode
232+ ( tab ) => checkTime - tab . createdAt >= ONE_DAY_MS && ! tab . isDependentMode
220233 ) ;
221234
235+ isCheckingTabs = false ;
222236 return { existingTabs, hasFreshTab, hasStaleTab } ;
223237}
224238
0 commit comments