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

refactor: avoid early initialisation of common errors #163

Merged
merged 1 commit into from
Sep 5, 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
2 changes: 1 addition & 1 deletion src/Cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class Cursors extends EventEmitter<CursorsEventMap> {
const self = await this.space.members.getSelf();

if (!self) {
throw ERR_NOT_ENTERED_SPACE;
throw ERR_NOT_ENTERED_SPACE();
}

const channel = this.getChannel();
Expand Down
76 changes: 41 additions & 35 deletions src/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,44 @@ export class ErrorInfo extends Error {
}
}

export const ERR_SPACE_NAME_MISSING = new ErrorInfo({
message: 'must have a non-empty name for the space',
code: 101000,
statusCode: 400,
});

export const ERR_NOT_ENTERED_SPACE = new ErrorInfo({
message: 'must enter a space to perform this operation',
code: 101001,
statusCode: 400,
});

export const ERR_LOCK_REQUEST_EXISTS = new ErrorInfo({
message: 'lock request already exists',
code: 101002,
statusCode: 400,
});

export const ERR_LOCK_IS_LOCKED = new ErrorInfo({
message: 'lock is currently locked',
code: 101003,
statusCode: 400,
});

export const ERR_LOCK_INVALIDATED = new ErrorInfo({
message: 'lock was invalidated by a concurrent lock request which now holds the lock',
code: 101004,
statusCode: 400,
});

export const ERR_LOCK_RELEASED = new ErrorInfo({
message: 'lock was released',
code: 101005,
statusCode: 400,
});
export const ERR_SPACE_NAME_MISSING = () =>
new ErrorInfo({
message: 'must have a non-empty name for the space',
code: 101000,
statusCode: 400,
});

export const ERR_NOT_ENTERED_SPACE = () =>
new ErrorInfo({
message: 'must enter a space to perform this operation',
code: 101001,
statusCode: 400,
});

export const ERR_LOCK_REQUEST_EXISTS = () =>
new ErrorInfo({
message: 'lock request already exists',
code: 101002,
statusCode: 400,
});

export const ERR_LOCK_IS_LOCKED = () =>
new ErrorInfo({
message: 'lock is currently locked',
code: 101003,
statusCode: 400,
});

export const ERR_LOCK_INVALIDATED = () =>
new ErrorInfo({
message: 'lock was invalidated by a concurrent lock request which now holds the lock',
code: 101004,
statusCode: 400,
});

export const ERR_LOCK_RELEASED = () =>
new ErrorInfo({
message: 'lock was released',
code: 101005,
statusCode: 400,
});
2 changes: 1 addition & 1 deletion src/Locations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class Locations extends EventEmitter<LocationsEventMap> {
const self = await this.space.members.getSelf();

if (!self) {
throw ERR_NOT_ENTERED_SPACE;
throw ERR_NOT_ENTERED_SPACE();
}

const update: PresenceMember['data'] = {
Expand Down
14 changes: 7 additions & 7 deletions src/Locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ export default class Locks extends EventEmitter<LockEventMap> {
async acquire(id: string, opts?: LockOptions): Promise<Lock> {
const self = await this.space.members.getSelf();
if (!self) {
throw ERR_NOT_ENTERED_SPACE;
throw ERR_NOT_ENTERED_SPACE();
}

// check there isn't an existing PENDING or LOCKED request for the current
// member, since we do not support nested locks
let lock = this.getLock(id, self.connectionId);
if (lock && lock.status !== 'unlocked') {
throw ERR_LOCK_REQUEST_EXISTS;
throw ERR_LOCK_REQUEST_EXISTS();
}

// initialise a new PENDING request
Expand All @@ -127,7 +127,7 @@ export default class Locks extends EventEmitter<LockEventMap> {
async release(id: string): Promise<void> {
const self = await this.space.members.getSelf();
if (!self) {
throw ERR_NOT_ENTERED_SPACE;
throw ERR_NOT_ENTERED_SPACE();
}

this.deleteLock(id, self.connectionId);
Expand Down Expand Up @@ -181,7 +181,7 @@ export default class Locks extends EventEmitter<LockEventMap> {

if (lock) {
lock.status = 'unlocked';
lock.reason = ERR_LOCK_RELEASED;
lock.reason = ERR_LOCK_RELEASED();
locks.delete(member.connectionId);
this.emit('update', lock);
}
Expand Down Expand Up @@ -216,7 +216,7 @@ export default class Locks extends EventEmitter<LockEventMap> {

if (!message.extras.locks.some((l: Lock) => l.id === lock.id)) {
lock.status = 'unlocked';
lock.reason = ERR_LOCK_RELEASED;
lock.reason = ERR_LOCK_RELEASED();
locks.delete(member.connectionId);
this.emit('update', lock);
}
Expand Down Expand Up @@ -262,15 +262,15 @@ export default class Locks extends EventEmitter<LockEventMap> {
) {
pendingLock.status = 'locked';
lock.status = 'unlocked';
lock.reason = ERR_LOCK_INVALIDATED;
lock.reason = ERR_LOCK_INVALIDATED();
this.emit('update', lock);
return;
}

// the lock is LOCKED and the PENDING request did not invalidate it, so
// mark the PENDING request as UNLOCKED with a reason.
pendingLock.status = 'unlocked';
pendingLock.reason = ERR_LOCK_IS_LOCKED;
pendingLock.reason = ERR_LOCK_IS_LOCKED();
}

updatePresence(member: SpaceMember) {
Expand Down
2 changes: 1 addition & 1 deletion src/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Space extends EventEmitter<SpaceEventsMap> {
const self = await this.members.getSelf();

if (!self) {
throw ERR_NOT_ENTERED_SPACE;
throw ERR_NOT_ENTERED_SPACE();
}

const update = {
Expand Down
2 changes: 1 addition & 1 deletion src/Spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Spaces {

async get(name: string, options?: Subset<SpaceOptions>): Promise<Space> {
if (typeof name !== 'string' || name.length === 0) {
throw ERR_SPACE_NAME_MISSING;
throw ERR_SPACE_NAME_MISSING();
}

if (this.spaces[name]) return this.spaces[name];
Expand Down
Loading