Skip to content

Commit

Permalink
Add getSelf and getOthers methods to locks
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Piatek authored and dpiatek committed Sep 1, 2023
1 parent 32ce87e commit 9883e1f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 19 deletions.
84 changes: 66 additions & 18 deletions src/Locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ describe('Locks (mockClient)', () => {
});
});

describe('getAll', () => {
it<SpaceTestContext>('returns all locks in the LOCKED state', async ({ space }) => {
describe('get*', () => {
beforeEach<SpaceTestContext>(async ({ space }) => {
await space.locks.processPresenceMessage(
Realtime.PresenceMessage.fromValues({
action: 'update',
Expand Down Expand Up @@ -381,31 +381,79 @@ describe('Locks (mockClient)', () => {
},
}),
);
});

const member1 = await space.members.getByConnectionId('1')!;
const member2 = await space.members.getByConnectionId('2')!;
it<SpaceTestContext>('correctly sets up locks', ({ space }) => {
const lock1 = space.locks.get('lock1');
expect(lock1).toBeDefined();

const lock2 = space.locks.get('lock2');
expect(lock2).toBeDefined();

const lock3 = space.locks.get('lock3');
expect(lock3).toBeDefined();
});

const locks = space.locks.getAll();
expect(locks.length).toEqual(3);
for (const lock of locks) {
switch (lock.id) {
case 'lock1':
case 'lock2':
expect(lock.member).toEqual(member1);
break;
case 'lock3':
expect(lock.member).toEqual(member2);
break;
default:
throw new Error(`unexpected lock id: ${lock.id}`);
describe('getSelf', () => {
it<SpaceTestContext>('returns all locks in the LOCKED state that belong to self', async ({ space }) => {
const member1 = await space.members.getByConnectionId('1')!;

const locks = await space.locks.getSelf();
expect(locks.length).toEqual(2);

for (const lock of locks) {
switch (lock.id) {
case 'lock1':
case 'lock2':
expect(lock.member).toEqual(member1);
break;
default:
throw new Error(`unexpected lock id: ${lock.id}`);
}
}
}
});
});

describe('getOthers', () => {
it<SpaceTestContext>('returns all locks in the LOCKED state for every member but self', async ({ space }) => {
const member2 = await space.members.getByConnectionId('2')!;

const locks = await space.locks.getOthers();
expect(locks.length).toEqual(1);

for (const lock of locks) {
switch (lock.id) {
case 'lock3':
expect(lock.member).toEqual(member2);
break;
default:
throw new Error(`unexpected lock id: ${lock.id}`);
}
}
});
});

describe('getAll', () => {
it<SpaceTestContext>('returns all locks in the LOCKED state', async ({ space }) => {
const member1 = await space.members.getByConnectionId('1')!;
const member2 = await space.members.getByConnectionId('2')!;

const locks = await space.locks.getAll();
expect(locks.length).toEqual(3);
for (const lock of locks) {
switch (lock.id) {
case 'lock1':
case 'lock2':
expect(lock.member).toEqual(member1);
break;
case 'lock3':
expect(lock.member).toEqual(member2);
break;
default:
throw new Error(`unexpected lock id: ${lock.id}`);
}
}
});
});
});
});
21 changes: 20 additions & 1 deletion src/Locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export default class Locks extends EventEmitter<LockEventMap> {
}
}

getAll(): Lock[] {
// This will be async in the future, when pending requests are no longer processed
// in the library.
async getAll(): Promise<Lock[]> {
const allLocks: Lock[] = [];

for (const locks of this.locks.values()) {
Expand All @@ -72,6 +74,23 @@ export default class Locks extends EventEmitter<LockEventMap> {
return allLocks;
}

async getSelf(): Promise<Lock[]> {
const self = await this.space.members.getSelf();

if (!self) return [];

return this.getLocksForConnectionId(self.connectionId).filter((lock) => lock.status === 'locked');
}

async getOthers(): Promise<Lock[]> {
const self = await this.space.members.getSelf();
const allLocks = await this.getAll();

if (!self) return allLocks;

return allLocks.filter((lock) => lock.member.connectionId !== self.connectionId);
}

async acquire(id: string, opts?: LockOptions): Promise<Lock> {
const self = await this.space.members.getSelf();
if (!self) {
Expand Down

0 comments on commit 9883e1f

Please sign in to comment.