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

space: Ensure lock state is maintained in presence data #118

Merged
merged 1 commit into from
Aug 22, 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
12 changes: 12 additions & 0 deletions src/Locations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ describe('Locations', () => {
previousLocation: 'location1',
});
});

it<SpaceTestContext>('maintains lock state in presence', async ({ space, presence, presenceMap }) => {
presenceMap.set('1', createPresenceMessage('enter'));

const lockID = 'test';
const lockReq = await space.locks.acquire(lockID);

const spy = vi.spyOn(presence, 'update');
await space.locations.set('location');
const extras = { locks: [lockReq] };
expect(spy).toHaveBeenCalledWith(expect.objectContaining({ extras }));
});
});

describe('location getters', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/Locations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ type LocationsEventMap = {
export default class Locations extends EventEmitter<LocationsEventMap> {
private lastLocationUpdate: Record<string, PresenceMember['data']['locationUpdate']['id']> = {};

constructor(private space: Space, private presenceUpdate: (update: PresenceMember['data']) => Promise<void>) {
constructor(
private space: Space,
private presenceUpdate: (update: PresenceMember['data'], extras?: PresenceMember['extras']) => Promise<void>,
) {
super();
}

Expand Down Expand Up @@ -69,7 +72,8 @@ export default class Locations extends EventEmitter<LocationsEventMap> {
},
};

await this.presenceUpdate(update);
const extras = this.space.locks.getLockExtras(self.connectionId);
await this.presenceUpdate(update, extras);
}

subscribe<K extends EventKey<LocationsEventMap>>(
Expand Down
13 changes: 7 additions & 6 deletions src/Locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,7 @@ export default class Locks extends EventEmitter<LockEventMap> {
previous: null,
},
};
let extras;
const locks = this.getLockRequests(member.connectionId);
if (locks.length > 0) {
extras = { locks };
}
return this.presenceUpdate(update, extras);
return this.presenceUpdate(update, this.getLockExtras(member.connectionId));
}

getLock(id: string, connectionId: string): Lock | undefined {
Expand Down Expand Up @@ -284,4 +279,10 @@ export default class Locks extends EventEmitter<LockEventMap> {
}
return requests;
}

getLockExtras(connectionId: string): PresenceMember['extras'] {
const locks = this.getLockRequests(connectionId);
if (locks.length === 0) return;
return { locks };
}
}
12 changes: 12 additions & 0 deletions src/Space.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ describe('Space', () => {
expect(updateSpy).toHaveBeenNthCalledWith(1, createProfileUpdate({ current: { name: 'Betty' } }));
});
});

it<SpaceTestContext>('maintains lock state in presence', async ({ space, presence, presenceMap }) => {
presenceMap.set('1', createPresenceMessage('enter'));

const lockID = 'test';
const lockReq = await space.locks.acquire(lockID);

const updateSpy = vi.spyOn(presence, 'update');
await space.updateProfileData({ name: 'Betty' });
const extras = { locks: [lockReq] };
expect(updateSpy).toHaveBeenCalledWith(expect.objectContaining({ extras }));
});
});

describe('leave', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ class Space extends EventEmitter<SpaceEventsMap> {
return;
}

return this.presenceUpdate(update);
const extras = this.locks.getLockExtras(self.connectionId);
return this.presenceUpdate(update, extras);
}

async leave(profileData: ProfileData = null) {
Expand Down
Loading