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

locks: Release all locks when a member leaves #131

Merged
merged 1 commit into from
Aug 24, 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
44 changes: 44 additions & 0 deletions src/Locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ describe('Locks (mockClient)', () => {
const emitSpy = vi.spyOn(space.locks, 'emit');

const msg = Realtime.PresenceMessage.fromValues({
action: 'update',
connectionId: member.connectionId,
extras: {
locks: [
Expand Down Expand Up @@ -179,6 +180,7 @@ describe('Locks (mockClient)', () => {
// process a PENDING request for the other member, which should
// transition to LOCKED
let msg = Realtime.PresenceMessage.fromValues({
action: 'update',
connectionId: otherConnId,
extras: {
locks: [
Expand All @@ -198,6 +200,7 @@ describe('Locks (mockClient)', () => {
// result matches what is expected
const emitSpy = vi.spyOn(space.locks, 'emit');
msg = Realtime.PresenceMessage.fromValues({
action: 'update',
connectionId: client.connection.id,
extras: {
locks: [
Expand Down Expand Up @@ -233,6 +236,7 @@ describe('Locks (mockClient)', () => {
const member = (await space.members.getSelf())!;

let msg = Realtime.PresenceMessage.fromValues({
action: 'update',
connectionId: member.connectionId,
extras: {
locks: [
Expand All @@ -249,6 +253,7 @@ describe('Locks (mockClient)', () => {
const emitSpy = vi.spyOn(space.locks, 'emit');

msg = Realtime.PresenceMessage.fromValues({
action: 'update',
connectionId: member.connectionId,
extras: undefined,
});
Expand All @@ -258,6 +263,45 @@ describe('Locks (mockClient)', () => {
expect(lock).not.toBeDefined();
expect(emitSpy).toHaveBeenCalledWith('update', lockEvent(member, 'unlocked'));
});

it<SpaceTestContext>('sets all locks to UNLOCKED when a member leaves', async ({ space }) => {
await space.enter();
const member = (await space.members.getSelf())!;

let msg = Realtime.PresenceMessage.fromValues({
action: 'update',
connectionId: member.connectionId,
extras: {
locks: [
{
id: 'lock1',
status: 'pending',
timestamp: Date.now(),
},
{
id: 'lock2',
status: 'pending',
timestamp: Date.now(),
},
],
},
});
await space.locks.processPresenceMessage(msg);
let lock1 = space.locks.get('lock1');
expect(lock1).toBeDefined();
expect(lock1!.member).toEqual(member);
let lock2 = space.locks.get('lock2');
expect(lock2).toBeDefined();
expect(lock2!.member).toEqual(member);

msg.action = 'leave';
await space.locks.processPresenceMessage(msg);

lock1 = space.locks.get('lock1');
expect(lock1).not.toBeDefined();
lock2 = space.locks.get('lock2');
expect(lock2).not.toBeDefined();
});
});

describe('release', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/Locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ export default class Locks extends EventEmitter<LockEventMap> {
const member = await this.space.members.getByConnectionId(message.connectionId);
if (!member) return;

if (!Array.isArray(message?.extras?.locks)) {
// there are no locks in presence, so release any existing locks for the
// member
if (message.action === 'leave' || !Array.isArray(message?.extras?.locks)) {
// the member has left, or they have no locks in presence, so release any
// existing locks for that member
for (const locks of this.locks.values()) {
const lock = locks.get(member.connectionId);
if (lock) {
Expand Down
Loading