Skip to content

Commit 72ff5b7

Browse files
committed
Simplify primaryRoom and enforceConnectionLimit
1 parent d36cb64 commit 72ff5b7

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

packages/sync/src/providers/http-polling/polling-manager.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

302302
let areListenersRegistered = false;
303+
let hasCheckedConnectionLimit = false;
303304
let hasCollaborators = false;
304305
let isActiveBrowser = 'visible' === document.visibilityState;
305306
let isPolling = false;
306307
let isUnloadPending = false;
307308
let pollInterval = POLLING_INTERVAL_IN_MS;
308309
let 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

Comments
 (0)