Skip to content

Commit

Permalink
Refactor fetching member by connectionId
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Piatek authored and dpiatek committed Jul 12, 2023
1 parent f6fb062 commit c0b78a5
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type SpaceMember = {

type SpaceLeaver = {
clientId: string;
connectionId: string;
timeoutId: ReturnType<typeof setTimeout>;
};

Expand Down Expand Up @@ -68,10 +69,14 @@ class Space extends EventEmitter<SpaceEventsMap> {
return this.channelName;
}

getMemberFromConnection(connectionId?: string) {
getMemberFromConnection(connectionId: string): SpaceMember | undefined {
return this.members.find((m) => m.connectionId === connectionId);
}

getMemberIndexFromConnection(connectionId: string): number {
return this.members.findIndex((m) => m.connectionId === connectionId);
}

private updateOrCreateMember(message: Types.PresenceMessage): SpaceMember {
const member = this.getMemberFromConnection(message.connectionId);
const lastEvent = {
Expand All @@ -81,7 +86,7 @@ class Space extends EventEmitter<SpaceEventsMap> {

if (!member) {
return {
clientId: message.clientId as string,
clientId: message.clientId,
connectionId: message.connectionId,
isConnected: message.action !== 'leave',
profileData: message.data.profileData,
Expand All @@ -106,7 +111,7 @@ class Space extends EventEmitter<SpaceEventsMap> {
const member = this.getMemberFromConnection(message.connectionId);

this.emit('leave', member);
this.removeMember(message.clientId);
this.removeMember(message.connectionId);
this.emit(MEMBERS_UPDATE, this.members);

if (member?.location) {
Expand All @@ -120,16 +125,18 @@ class Space extends EventEmitter<SpaceEventsMap> {

this.leavers.push({
clientId: message.clientId,
connectionId: message.connectionId,
timeoutId: setTimeout(timeoutCallback, this.options.offlineTimeout),
});
}
private removeLeaver(leaverIndex) {

private removeLeaver(leaverIndex: number): void {
clearTimeout(this.leavers[leaverIndex].timeoutId);
this.leavers.splice(leaverIndex, 1);
}

private updateLeavers(message: Types.PresenceMessage) {
const index = this.leavers.findIndex(({ clientId }) => clientId === message.clientId);
private updateLeavers(message: Types.PresenceMessage): void {
const index = this.leavers.findIndex(({ connectionId }) => message.connectionId === connectionId);

if (message.action === 'leave' && index < 0) {
this.addLeaver(message);
Expand All @@ -141,8 +148,8 @@ class Space extends EventEmitter<SpaceEventsMap> {
}
}

private updateMembers(message: Types.PresenceMessage) {
const index = this.members.findIndex(({ clientId }) => clientId === message.clientId);
private updateMembers(message: Types.PresenceMessage): void {
const index = this.getMemberIndexFromConnection(message.connectionId);
const spaceMember = this.updateOrCreateMember(message);

if (index >= 0) {
Expand All @@ -153,8 +160,8 @@ class Space extends EventEmitter<SpaceEventsMap> {
}
}

private removeMember(clientId) {
const index = this.members.findIndex((member) => member.clientId === clientId);
private removeMember(connectionId: string): void {
const index = this.getMemberIndexFromConnection(connectionId);

if (index >= 0) {
this.members.splice(index, 1);
Expand Down Expand Up @@ -193,7 +200,11 @@ class Space extends EventEmitter<SpaceEventsMap> {
}

getSelf(): SpaceMember | undefined {
return this.getMemberFromConnection(this.connectionId);
if (this.connectionId) {
return this.getMemberFromConnection(this.connectionId);
}

return;
}
}

Expand Down

0 comments on commit c0b78a5

Please sign in to comment.