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

[MMB-266] Add getSelf and getOthers methods to locks #155

Merged
merged 1 commit into from
Sep 1, 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
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
Loading