Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer null over undefined consistently #141

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Cursors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CursorsTestContext>('returns the cursor update for self', async ({
Expand All @@ -472,7 +472,7 @@ describe('Cursors', () => {

it<CursorsTestContext>('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({});
Expand Down
8 changes: 4 additions & 4 deletions src/Cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ export default class Cursors extends EventEmitter<CursorsEventMap> {
}
}

async getSelf(): Promise<CursorUpdate | undefined> {
async getSelf(): Promise<CursorUpdate | null> {
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<Record<string, null | CursorUpdate>> {
Expand All @@ -167,7 +167,7 @@ export default class Cursors extends EventEmitter<CursorsEventMap> {
return allCursorsFiltered;
}

async getAll() {
async getAll(): Promise<Record<string, null | CursorUpdate>> {
const channel = this.getChannel();
return await this.cursorHistory.getLastCursorUpdate(channel, this.options.paginationLimit);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class Members extends EventEmitter<MemberEventsMap> {
}
}

async getSelf(): Promise<SpaceMember | undefined> {
return this.space.connectionId ? await this.getByConnectionId(this.space.connectionId) : undefined;
async getSelf(): Promise<SpaceMember | null> {
return this.space.connectionId ? await this.getByConnectionId(this.space.connectionId) : null;
}

async getAll(): Promise<SpaceMember[]> {
Expand Down Expand Up @@ -99,9 +99,9 @@ class Members extends EventEmitter<MemberEventsMap> {
}
}

async getByConnectionId(connectionId: string): Promise<SpaceMember | undefined> {
async getByConnectionId(connectionId: string): Promise<SpaceMember | null> {
const members = await this.getAll();
return members.find((m) => m.connectionId === connectionId);
return members.find((m) => m.connectionId === connectionId) ?? null;
}

createMember(message: PresenceMember): SpaceMember {
Expand Down
2 changes: 1 addition & 1 deletion src/Space.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpaceTestContext>('initialises locks', async ({ presenceMap, space }) => {
Expand Down
Loading