From 8ef4febfe71fa4743e72182941f18d0839c36e28 Mon Sep 17 00:00:00 2001 From: Dominik Piatek Date: Tue, 29 Aug 2023 14:05:07 +0100 Subject: [PATCH] Prefer null over undefined consistently --- src/Cursors.test.ts | 4 ++-- src/Cursors.ts | 8 ++++---- src/Members.ts | 8 ++++---- src/Space.test.ts | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Cursors.test.ts b/src/Cursors.test.ts index 3330edde..42b06b14 100644 --- a/src/Cursors.test.ts +++ b/src/Cursors.test.ts @@ -455,7 +455,7 @@ describe('Cursors', () => { vi.spyOn(space.cursors, 'getAll').mockImplementation(async () => ({})); const self = await space.cursors.getSelf(); - expect(self).toBeUndefined(); + expect(self).toBeNull(); }); it('returns the cursor update for self', async ({ @@ -472,7 +472,7 @@ describe('Cursors', () => { it('returns an empty object if self is not present in cursors', async ({ space }) => { vi.spyOn(space.cursors, 'getAll').mockResolvedValue({}); - vi.spyOn(space.members, 'getSelf').mockResolvedValue(undefined); + vi.spyOn(space.members, 'getSelf').mockResolvedValue(null); const others = await space.cursors.getOthers(); expect(others).toEqual({}); diff --git a/src/Cursors.ts b/src/Cursors.ts index f973e311..b7101723 100644 --- a/src/Cursors.ts +++ b/src/Cursors.ts @@ -149,12 +149,12 @@ export default class Cursors extends EventEmitter { } } - async getSelf(): Promise { + async getSelf(): Promise { const self = await this.space.members.getSelf(); - if (!self) return; + if (!self) return null; const allCursors = await this.getAll(); - return allCursors[self.connectionId] as CursorUpdate; + return allCursors[self.connectionId]; } async getOthers(): Promise> { @@ -167,7 +167,7 @@ export default class Cursors extends EventEmitter { return allCursorsFiltered; } - async getAll() { + async getAll(): Promise> { const channel = this.getChannel(); return await this.cursorHistory.getLastCursorUpdate(channel, this.options.paginationLimit); } diff --git a/src/Members.ts b/src/Members.ts index c4f79ee4..d0d56796 100644 --- a/src/Members.ts +++ b/src/Members.ts @@ -50,8 +50,8 @@ class Members extends EventEmitter { } } - async getSelf(): Promise { - return this.space.connectionId ? await this.getByConnectionId(this.space.connectionId) : undefined; + async getSelf(): Promise { + return this.space.connectionId ? await this.getByConnectionId(this.space.connectionId) : null; } async getAll(): Promise { @@ -99,9 +99,9 @@ class Members extends EventEmitter { } } - async getByConnectionId(connectionId: string): Promise { + async getByConnectionId(connectionId: string): Promise { const members = await this.getAll(); - return members.find((m) => m.connectionId === connectionId); + return members.find((m) => m.connectionId === connectionId) ?? null; } createMember(message: PresenceMember): SpaceMember { diff --git a/src/Space.test.ts b/src/Space.test.ts index 656f8cdd..67db9d9a 100644 --- a/src/Space.test.ts +++ b/src/Space.test.ts @@ -77,7 +77,7 @@ describe('Space', () => { expect(member).toEqual(createSpaceMember()); const noMember = await space.members.getByConnectionId('nonExistentConnectionId'); - expect(noMember).toBe(undefined); + expect(noMember).toBeNull(); }); it('initialises locks', async ({ presenceMap, space }) => {