Skip to content

Commit

Permalink
Change cursors batch time formula on presence update
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoDiasAbly committed Sep 17, 2024
1 parent 50d8899 commit f2f239a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Cursors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CursorsTestContext>('batchTime is updated when multiple people are present', async ({
Expand All @@ -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', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/Cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ export default class Cursors extends EventEmitter<CursorsEventMap> {
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() {
Expand Down

0 comments on commit f2f239a

Please sign in to comment.