Skip to content

Commit

Permalink
Check sender of ENTER presence message in space.enter()
Browse files Browse the repository at this point in the history
It shouldn’t return as a result of somebody else entering the space.

I’m not sure whether this is the best way of predicating whether or not
to return — perhaps checking for some unique ID on the received ENTER
presence message would be better. But it seems like an OK low-thought
solution at least for the time being.
  • Loading branch information
lawrence-forooghian committed Oct 19, 2023
1 parent 1431c3c commit 1a43536
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Space.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ describe('Space', () => {
beforeEach<SpaceTestContext>(({ presence }) => {
vi.spyOn(presence, 'subscribe').mockImplementation(
async (_, listener?: (presenceMessage: Types.PresenceMessage) => void) => {
listener!(createPresenceMessage('enter' /* arbitrarily chosen */));
listener!(
createPresenceMessage('enter' /* arbitrarily chosen */, { clientId: 'MOCK_CLIENT_ID', connectionId: '1' }),
);
},
);
});
Expand Down
11 changes: 10 additions & 1 deletion src/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,16 @@ class Space extends EventEmitter<SpaceEventMap> {
return new Promise((resolve) => {
const presence = this.channel.presence;

const presenceListener = async () => {
const presenceListener = async (presenceMessage: Types.PresenceMessage) => {
if (
!(
presenceMessage.clientId == this.client.auth.clientId &&
presenceMessage.connectionId == this.client.connection.id
)
) {
return;
}

presence.unsubscribe(presenceListener);

const presenceMessages = await presence.get();
Expand Down

0 comments on commit 1a43536

Please sign in to comment.