-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor structure of presence updates
This commit changes how we handle updates for presence spaces. An presence update becomes a PresenceMember: export type PresenceMember = { data: { profileUpdate: { id: string | null; current: ProfileData; }; locationUpdate: { id: string | null; previous: unknown; current: unknown; }; }; } & Omit<Types.PresenceMessage, 'data'>; Which then gets translated for the developer to a SpaceMember: export type SpaceMember = { clientId: string; connectionId: string; isConnected: boolean; profileData: ProfileData; location: unknown; lastEvent: { name: Types.PresenceAction; timestamp: number; }; }; data on PresenceMember contains the last update for profileData an location. The current key is the value of these properties on SpaceMember. profileUpdate and locationUpdate contain an id. This id is set on publish, but only when we are providing new data, not copying already set data. The handlers check the id to decide if an update should be emitted (it will still be applied, and it should be the same).
- Loading branch information
Dominik Piatek
committed
Aug 3, 2023
1 parent
c296711
commit d501224
Showing
21 changed files
with
895 additions
and
861 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const nanoidId = 'NanoidID'; | ||
|
||
function nanoid(): string { | ||
return nanoidId; | ||
} | ||
|
||
export { nanoid, nanoidId }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type Space from './Space.js'; | ||
import type { SpaceMember } from './types.js'; | ||
|
||
type SpaceLeaver = SpaceMember & { | ||
timeoutId: ReturnType<typeof setTimeout>; | ||
}; | ||
|
||
class Leavers { | ||
private leavers: SpaceLeaver[] = []; | ||
|
||
constructor(private space: Space) {} | ||
|
||
getByConnectionId(connectionId: string): SpaceLeaver | undefined { | ||
return this.leavers.find((leaver) => leaver.connectionId === connectionId); | ||
} | ||
|
||
addLeaver(connectionId: string) { | ||
const timeoutCallback = () => { | ||
this.space.members.removeMember(connectionId); | ||
}; | ||
|
||
const member = this.space.members.getByConnectionId(connectionId); | ||
|
||
if (member) { | ||
this.leavers.push({ | ||
...member, | ||
timeoutId: setTimeout(timeoutCallback, this.space.options.offlineTimeout), | ||
}); | ||
} | ||
} | ||
|
||
removeLeaver(connectionId: string) { | ||
const leaverIndex = this.leavers.findIndex((leaver) => leaver.connectionId === connectionId); | ||
|
||
if (leaverIndex >= 0) { | ||
clearTimeout(this.leavers[leaverIndex].timeoutId); | ||
this.leavers.splice(leaverIndex, 1); | ||
} | ||
} | ||
|
||
refreshTimeout(connectionId: string) { | ||
this.removeLeaver(connectionId); | ||
this.addLeaver(connectionId); | ||
} | ||
} | ||
|
||
export default Leavers; |
Oops, something went wrong.