@@ -66,7 +66,7 @@ interface RoomState {
6666 clientId : number ;
6767 createCompactionUpdate : ( ) => SyncUpdate ;
6868 endCursor : number ;
69- enforceConnectionLimit : boolean ;
69+ isPrimaryRoom : boolean ;
7070 localAwarenessState : LocalAwarenessState ;
7171 log : LogFunction ;
7272 onStatusChange : ( status : ConnectionStatus ) => void ;
@@ -267,12 +267,12 @@ function checkConnectionLimit(
267267 awareness : AwarenessState ,
268268 roomState : RoomState
269269) : boolean {
270- if ( ! roomState . enforceConnectionLimit ) {
270+ if ( ! roomState . isPrimaryRoom || hasCheckedConnectionLimit ) {
271271 return false ;
272272 }
273273
274274 // Limits are only enforced on the initial connection.
275- roomState . enforceConnectionLimit = false ;
275+ hasCheckedConnectionLimit = true ;
276276
277277 const maxClientsPerRoom = applyFilters (
278278 'sync.pollingProvider.maxClientsPerRoom' ,
@@ -300,13 +300,13 @@ function checkConnectionLimit(
300300}
301301
302302let areListenersRegistered = false ;
303+ let hasCheckedConnectionLimit = false ;
303304let hasCollaborators = false ;
304305let isActiveBrowser = 'visible' === document . visibilityState ;
305306let isPolling = false ;
306307let isUnloadPending = false ;
307308let pollInterval = POLLING_INTERVAL_IN_MS ;
308309let pollingTimeoutId : ReturnType < typeof setTimeout > | null = null ;
309- let primaryRoom : string | null = null ;
310310
311311/**
312312 * Mark that a page unload has been requested. This fires on
@@ -451,7 +451,7 @@ function poll(): void {
451451 // frequency. We only check the primary room to avoid false
452452 // positives from shared collection rooms (e.g. taxonomy/category).
453453 if (
454- room . room === primaryRoom &&
454+ roomState . isPrimaryRoom &&
455455 Object . keys ( room . awareness ) . length > 1
456456 ) {
457457 hasCollaborators = true ;
@@ -585,14 +585,7 @@ function registerRoom( {
585585 * How might this approach be improved? We could develop some way to annotate
586586 * entity loading so that the consumer can indicate which entity is primary.
587587 */
588- const enforceConnectionLimit = 0 === roomStates . size ;
589-
590- // The first room registered is treated as the "primary" entity.
591- // Used to scope the collaborator check to only this room, avoiding
592- // awareness results from shared collection rooms (e.g. taxonomy/category).
593- if ( ! primaryRoom ) {
594- primaryRoom = room ;
595- }
588+ const isPrimaryRoom = 0 === roomStates . size ;
596589
597590 function onAwarenessUpdate ( ) : void {
598591 roomState . localAwarenessState = awareness . getLocalState ( ) ?? { } ;
@@ -644,7 +637,7 @@ function registerRoom( {
644637 SyncUpdateType . COMPACTION
645638 ) ,
646639 endCursor : 0 ,
647- enforceConnectionLimit ,
640+ isPrimaryRoom ,
648641 localAwarenessState : awareness . getLocalState ( ) ?? { } ,
649642 log,
650643 onStatusChange,
@@ -691,10 +684,6 @@ function unregisterRoom( room: string ): void {
691684 postSyncUpdateNonBlocking ( { rooms } ) ;
692685 state . unregister ( ) ;
693686 roomStates . delete ( room ) ;
694-
695- if ( room === primaryRoom ) {
696- primaryRoom = null ;
697- }
698687 }
699688
700689 if ( 0 === roomStates . size && areListenersRegistered ) {
@@ -705,6 +694,7 @@ function unregisterRoom( room: string ): void {
705694 handleVisibilityChange
706695 ) ;
707696 areListenersRegistered = false ;
697+ hasCheckedConnectionLimit = false ;
708698 }
709699}
710700
0 commit comments