Skip to content

Commit

Permalink
mark stuff as @internal or private to avoid "X does not have any docu…
Browse files Browse the repository at this point in the history
…mentation" error

if we were to export interfaces instead of classes then we could avoid
this, but such a change feels outside the scope of this work

this is done based on looking at what's mentioned in documentation to
try and guess public interface
  • Loading branch information
lawrence-forooghian committed Sep 26, 2023
1 parent cba87b1 commit ccafd10
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/Cursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class Cursors extends EventEmitter<CursorsEventMap> {

public channel?: Types.RealtimeChannelPromise;

/** @internal */
constructor(private space: Space) {
super();

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

/** @internal */
constructor(private space: Space, private presenceUpdate: Space['presenceUpdate']) {
super();
}

/** @internal */
async processPresenceMessage(message: PresenceMember) {
// Only an update action is currently a valid location update.
if (message.action !== 'update') return;
Expand Down
12 changes: 8 additions & 4 deletions src/Locks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default class Locks extends EventEmitter<LocksEventMap> {
// have requested.
private locks: Map<string, Map<string, Lock>>;

/** @internal */
constructor(private space: Space, private presenceUpdate: Space['presenceUpdate']) {
super();
this.locks = new Map();
Expand Down Expand Up @@ -169,6 +170,7 @@ export default class Locks extends EventEmitter<LocksEventMap> {
}
}

/** @internal */
async processPresenceMessage(message: Types.PresenceMessage) {
const member = await this.space.members.getByConnectionId(message.connectionId);
if (!member) return;
Expand Down Expand Up @@ -222,7 +224,7 @@ export default class Locks extends EventEmitter<LocksEventMap> {
//
// TODO: remove this once the Ably system processes PENDING requests
// internally using this same logic.
processPending(member: SpaceMember, pendingLock: Lock) {
private processPending(member: SpaceMember, pendingLock: Lock) {
// if the requested lock ID is not currently locked, then mark the PENDING
// lock request as LOCKED
const lock = this.get(pendingLock.id);
Expand Down Expand Up @@ -266,18 +268,19 @@ export default class Locks extends EventEmitter<LocksEventMap> {
pendingLock.reason = ERR_LOCK_IS_LOCKED();
}

updatePresence(self: SpaceMember) {
private updatePresence(self: SpaceMember) {
const update = new SpaceUpdate({ self, extras: this.getLockExtras(self.connectionId) });
return this.presenceUpdate(update.noop());
}

/** @internal */
getLock(id: string, connectionId: string): Lock | undefined {
const locks = this.locks.get(id);
if (!locks) return;
return locks.get(connectionId);
}

setLock(lock: Lock) {
private setLock(lock: Lock) {
let locks = this.locks.get(lock.id);
if (!locks) {
locks = new Map();
Expand All @@ -286,7 +289,7 @@ export default class Locks extends EventEmitter<LocksEventMap> {
locks.set(lock.member.connectionId, lock);
}

deleteLock(id: string, connectionId: string) {
private deleteLock(id: string, connectionId: string) {
const locks = this.locks.get(id);
if (!locks) return;
return locks.delete(connectionId);
Expand All @@ -306,6 +309,7 @@ export default class Locks extends EventEmitter<LocksEventMap> {
return requests;
}

/** @internal */
getLockExtras(connectionId: string): PresenceMember['extras'] {
const locks = this.getLocksForConnectionId(connectionId);
if (locks.length === 0) return;
Expand Down
7 changes: 5 additions & 2 deletions src/Members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ class Members extends EventEmitter<MembersEventMap> {
private lastMemberUpdate: Record<string, PresenceMember['data']['profileUpdate']['id']> = {};
private leavers: Leavers;

/** @internal */
constructor(private space: Space) {
super();
this.leavers = new Leavers(this.space.options.offlineTimeout);
}

/** @internal */
async processPresenceMessage(message: PresenceMember) {
const { action, connectionId } = message;
const isLeaver = !!this.leavers.getByConnectionId(connectionId);
Expand Down Expand Up @@ -103,12 +105,13 @@ class Members extends EventEmitter<MembersEventMap> {
}
}

/** @internal */
async getByConnectionId(connectionId: string): Promise<SpaceMember | null> {
const members = await this.getAll();
return members.find((m) => m.connectionId === connectionId) ?? null;
}

createMember(message: PresenceMember): SpaceMember {
private createMember(message: PresenceMember): SpaceMember {
return {
clientId: message.clientId,
connectionId: message.connectionId,
Expand All @@ -122,7 +125,7 @@ class Members extends EventEmitter<MembersEventMap> {
};
}

async onMemberOffline(member: SpaceMember) {
private async onMemberOffline(member: SpaceMember) {
this.leavers.removeLeaver(member.connectionId);

this.emit('remove', member);
Expand Down
3 changes: 2 additions & 1 deletion src/Space.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type SpaceEventMap = {
};

class Space extends EventEmitter<SpaceEventMap> {
readonly channelName: string;
private readonly channelName: string;
readonly connectionId: string | undefined;
readonly options: SpaceOptions;
readonly locations: Locations;
Expand All @@ -44,6 +44,7 @@ class Space extends EventEmitter<SpaceEventMap> {
readonly locks: Locks;
readonly name: string;

/** @internal */
constructor(name: string, readonly client: Types.RealtimePromise, options?: Subset<SpaceOptions>) {
super();

Expand Down
8 changes: 7 additions & 1 deletion src/utilities/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ export type EventKey<T extends EventMap> = string & keyof T;
export type EventListener<T> = (params: T) => void;

export default class EventEmitter<T extends EventMap> {
/** @internal */
any: Array<Function>;
/** @internal */
events: Record<string, Function[]>;
/** @internal */
anyOnce: Array<Function>;
/** @internal */
eventsOnce: Record<string, Function[]>;

constructor() {
Expand Down Expand Up @@ -188,6 +192,8 @@ export default class EventEmitter<T extends EventMap> {
}

/**
* @internal
*
* Emit an event
* @param event the event name
* @param arg the arguments to pass to the listener
Expand Down Expand Up @@ -266,7 +272,7 @@ export default class EventEmitter<T extends EventMap> {
}

/**
* Private API
* @internal
*
* Listen for a single occurrence of a state event and fire immediately if currentState matches targetState
* @param targetState the name of the state event to listen to
Expand Down

0 comments on commit ccafd10

Please sign in to comment.