-
Notifications
You must be signed in to change notification settings - Fork 2
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
style: enable strict and type-checked lints on eslint #228
Changes from all commits
c4b4b1a
f4f9017
5b5c199
896f3d4
59cd92b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,7 @@ interface PresenceEventsMap { | |
/** | ||
* Type for PresenceData | ||
*/ | ||
export type PresenceData = { | ||
[key: string]: unknown; | ||
}; | ||
export type PresenceData = Record<string, unknown>; | ||
|
||
/** | ||
* Type for AblyPresenceData | ||
|
@@ -209,13 +207,27 @@ export class DefaultPresence extends EventEmitter<PresenceEventsMap> implements | |
async get(params?: Ably.RealtimePresenceParams): Promise<PresenceMember[]> { | ||
this._logger.trace('Presence.get()', { params }); | ||
const userOnPresence = await this.subscriptionManager.channel.presence.get(params); | ||
const userDataToReturn = (data: string) => { | ||
try { | ||
const parsedData = JSON.parse(data) as AblyPresenceData; | ||
if (!parsedData.userCustomData) { | ||
return undefined; | ||
} | ||
|
||
return parsedData.userCustomData; | ||
} catch (error) { | ||
this._logger.error('Presence.get(); error parsing user data', { error }); | ||
return undefined; | ||
} | ||
}; | ||
|
||
// ably-js never emits the 'absent' event, so we can safely ignore it here. | ||
return userOnPresence.map((user) => ({ | ||
clientId: user.clientId, | ||
action: user.action as PresenceEvents, | ||
data: user.data ? (JSON.parse(user.data).userCustomData as PresenceData) : undefined, | ||
data: userDataToReturn(user.data as string), | ||
timestamp: user.timestamp, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
extras: user.extras, | ||
id: user.id, | ||
})); | ||
|
@@ -286,7 +298,7 @@ export class DefaultPresence extends EventEmitter<PresenceEventsMap> implements | |
this._logger.trace('Presence.subscribe(); listenerOrEvents', { listenerOrEvents }); | ||
if (!listenerOrEvents && !listener) { | ||
this._logger.error('could not subscribe to presence; invalid arguments'); | ||
throw new Ably.ErrorInfo('could not subscribe listener: invalid arguments', 40000, 400); | ||
throw new Ably.ErrorInfo('could not subscribe listener: invalid arguments', 40000, 400) as unknown as Error; | ||
} | ||
const hasListeners = this.hasListeners(); | ||
if (!listener) { | ||
|
@@ -298,7 +310,9 @@ export class DefaultPresence extends EventEmitter<PresenceEventsMap> implements | |
this._logger.debug('Presence.subscribe(); adding internal listener'); | ||
return this.subscriptionManager.presenceSubscribe(this.subscribeToEvents); | ||
} | ||
return this.subscriptionManager.channel.attach().then(() => {}); | ||
return this.subscriptionManager.channel.attach().then(() => { | ||
return Promise.resolve(); | ||
}); | ||
} | ||
|
||
/** | ||
|
@@ -320,7 +334,7 @@ export class DefaultPresence extends EventEmitter<PresenceEventsMap> implements | |
this._logger.trace('Presence.unsubscribe(); listenerOrEvents', { listenerOrEvents }); | ||
if (!listenerOrEvents && !listener) { | ||
this._logger.error('could not unsubscribe from presence; invalid arguments'); | ||
throw new Ably.ErrorInfo('could not unsubscribe listener: invalid arguments', 40000, 400); | ||
throw new Ably.ErrorInfo('could not unsubscribe listener: invalid arguments', 40000, 400) as unknown as Error; | ||
} | ||
if (!listener) { | ||
this.off(listenerOrEvents); | ||
|
@@ -342,11 +356,10 @@ export class DefaultPresence extends EventEmitter<PresenceEventsMap> implements | |
*/ | ||
subscribeToEvents = (member: Ably.PresenceMessage) => { | ||
try { | ||
const parsedData = JSON.parse(member.data); | ||
|
||
const parsedData = JSON.parse(member.data as string) as AblyPresenceData; | ||
// ably-js never emits the 'absent' event, so we can safely ignore it here. | ||
this.emit(PresenceEvents[member.action as PresenceEvents], { | ||
action: PresenceEvents[member.action as PresenceEvents], | ||
this.emit(member.action as PresenceEvents, { | ||
action: member.action as PresenceEvents, | ||
clientId: member.clientId, | ||
timestamp: member.timestamp, | ||
data: parsedData.userCustomData, | ||
|
@@ -358,7 +371,7 @@ export class DefaultPresence extends EventEmitter<PresenceEventsMap> implements | |
50000, | ||
500, | ||
(error as Error).message, | ||
); | ||
) as unknown as Error; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what's going on here, but it's odd to have to Maybe a potential fix is to make a constructor function that does the casting? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is because I've already merged the fix in ably/ably-js#1805 so we can remove this in the next version of ably-js There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
} | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this too evil?
Edit to explain: what's the rule that this was breaking? I know it might not be this line, but just curious what can't we do? Is it the type of
extras
being not specific enough?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because
data
andextras
areany
types, so it's trying to say that you shouldn't setany
to a typed variable because that may go unnoticed and leak into your codebase:https://typescript-eslint.io/rules/no-unsafe-assignment/