Skip to content

Commit

Permalink
cursors: Fix cursor batching calculation
Browse files Browse the repository at this point in the history
The cursor batching algorithm was designed so that the overall channel
message rate remains lower than an upper bound limit, and the algorithm
expects the batching to multiply the batch interval by the number of
members, not the number of members minus one as we're currently doing.

Signed-off-by: Lewis Marshall <[email protected]>
  • Loading branch information
lmars authored and dpiatek committed Sep 10, 2023
1 parent 7cd8a52 commit bb90d74
Show file tree
Hide file tree
Showing 2 changed files with 3 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(100);
expect(batching.batchTime).toEqual(200);
});

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(100);
expect(batching.batchTime).toEqual(200);
});

describe('pushCursorPosition', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/Cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ 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 - 1) * this.options.outboundBatchInterval);
this.cursorBatching.setBatchTime(cursorsMembers.length * this.options.outboundBatchInterval);
}

private isUnsubscribed() {
Expand Down

0 comments on commit bb90d74

Please sign in to comment.