Skip to content

Commit

Permalink
locks: Process locks on space enter
Browse files Browse the repository at this point in the history
Signed-off-by: Lewis Marshall <lewis.marshall@ably.com>
  • Loading branch information
lmars committed Aug 6, 2023
1 parent 5e2903e commit f5ff5e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Space.mockClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Space, { SpaceMember } from './Space.js';
import { createPresenceEvent, createPresenceMessage } from './utilities/test/fakes.js';
import Locations from './Locations.js';
import Cursors from './Cursors.js';
import { LockStatus } from './Locks.js';
import { MEMBERS_UPDATE } from './utilities/Constants.js';

interface SpaceTestContext {
Expand Down Expand Up @@ -89,6 +90,40 @@ describe('Space (mockClient)', () => {
const noMember = space.getMemberFromConnection('nonExistentConnectionId');
expect(noMember).toBe(undefined);
});

it<SpaceTestContext>('initialises locks', async ({ presence, space }) => {
vi.spyOn(presence, 'get').mockImplementationOnce(async () => [
createPresenceMessage('enter', {
connectionId: '1',
extras: {
locks: [
{ id: 'lock1', status: LockStatus.LOCKED },
{ id: 'lock2', status: LockStatus.PENDING },
],
},
}),
createPresenceMessage('enter', {
connectionId: '2',
extras: {
locks: [
{ id: 'lock1', status: LockStatus.UNLOCKED },
{ id: 'lock3', status: LockStatus.LOCKED },
],
},
}),
]);
await space.enter();

const member1 = space.getMemberFromConnection('1');
expect(member1.locks.size).toEqual(2);
expect(member1.locks.get('lock1').status).toBe(LockStatus.LOCKED);
expect(member1.locks.get('lock2').status).toBe(LockStatus.LOCKED);

const member2 = space.getMemberFromConnection('2');
expect(member2.locks.size).toEqual(2);
expect(member2.locks.get('lock1').status).toBe(LockStatus.UNLOCKED);
expect(member2.locks.get('lock3').status).toBe(LockStatus.LOCKED);
});
});

describe('updateProfileData', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ class Space extends EventEmitter<SpaceEventsMap> {
const presenceMessages = await presence.get();
this.members = this.mapPresenceMembersToSpaceMembers(presenceMessages);

presenceMessages.forEach((msg) => this.locks.processPresenceMessage(msg));

resolve(this.members);
});
});
Expand Down

0 comments on commit f5ff5e9

Please sign in to comment.