From f2f239ac354f3b80f140bf3349ee1ff87ccb187e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro=20Monteiro=20Morgado=20Dias?= Date: Wed, 4 Sep 2024 16:41:58 +0100 Subject: [PATCH] Change cursors batch time formula on presence update --- src/Cursors.test.ts | 4 ++-- src/Cursors.ts | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Cursors.test.ts b/src/Cursors.test.ts index 383f4ae8..1f0c8802 100644 --- a/src/Cursors.test.ts +++ b/src/Cursors.test.ts @@ -116,7 +116,7 @@ describe('Cursors', () => { vi.spyOn(channel.presence, 'get').mockImplementation(createPresenceCount(2)); await cursors['onPresenceUpdate'](); expect(batching.shouldSend).toBeTruthy(); - expect(batching.batchTime).toEqual(50); + expect(batching.batchTime).toEqual(25); }); it('batchTime is updated when multiple people are present', async ({ @@ -126,7 +126,7 @@ describe('Cursors', () => { }) => { vi.spyOn(channel.presence, 'get').mockImplementation(createPresenceCount(2)); await cursors['onPresenceUpdate'](); - expect(batching.batchTime).toEqual(50); + expect(batching.batchTime).toEqual(25); }); describe('pushCursorPosition', () => { diff --git a/src/Cursors.ts b/src/Cursors.ts index 7548f463..8cf17a16 100644 --- a/src/Cursors.ts +++ b/src/Cursors.ts @@ -105,7 +105,11 @@ export default class Cursors extends EventEmitter { const channel = this.getChannel(); const cursorsMembers = await channel.presence.get(); this.cursorBatching.setShouldSend(cursorsMembers.length > 1); - this.cursorBatching.setBatchTime(cursorsMembers.length * this.options.outboundBatchInterval); + /** + * Since server-side batching is automically enabled for cursors channels, we can now adjust the client-side batching interval more granularly. + * E.g. multiply the configured outboundBatchInterval by groups of 100 members instead of the total number of members. + */ + this.cursorBatching.setBatchTime(Math.ceil(cursorsMembers.length / 100) * this.options.outboundBatchInterval); } private isUnsubscribed() {