Skip to content

Commit

Permalink
feat(Client): add conditional ready typings (#6073)
Browse files Browse the repository at this point in the history
  • Loading branch information
memikri authored Jul 14, 2021
1 parent 60148c6 commit 4206e35
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
11 changes: 10 additions & 1 deletion src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const VoiceRegion = require('../structures/VoiceRegion');
const Webhook = require('../structures/Webhook');
const Widget = require('../structures/Widget');
const Collection = require('../util/Collection');
const { Events, InviteScopes } = require('../util/Constants');
const { Events, InviteScopes, Status } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const Intents = require('../util/Intents');
const Options = require('../util/Options');
Expand Down Expand Up @@ -227,6 +227,15 @@ class Client extends BaseClient {
}
}

/**
* Returns whether the client has logged in, indicative of being able to access
* properties such as `user` and `application`.
* @returns {boolean}
*/
isReady() {
return this.ws.status === Status.READY;
}

/**
* Logs out, terminates the connection to Discord, and destroys the client.
* @returns {void}
Expand Down
3 changes: 2 additions & 1 deletion src/client/websocket/WebSocketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,9 @@ class WebSocketManager extends EventEmitter {
/**
* Emitted when the client becomes ready to start working.
* @event Client#ready
* @param {Client} client The client
*/
this.client.emit(Events.CLIENT_READY);
this.client.emit(Events.CLIENT_READY, this.client);

this.handlePacket();
}
Expand Down
19 changes: 11 additions & 8 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,25 @@ export class Channel extends Base {
public toString(): ChannelMention;
}

export class Client extends BaseClient {
type If<T extends boolean, A, B = null> = T extends true ? A : T extends false ? B : A | B;

export class Client<Ready extends boolean = boolean> extends BaseClient {
public constructor(options: ClientOptions);
private actions: unknown;
private _eval(script: string): unknown;
private _validateOptions(options: ClientOptions): void;

public application: ClientApplication | null;
public application: If<Ready, ClientApplication>;
public channels: ChannelManager;
public readonly emojis: BaseGuildEmojiManager;
public guilds: GuildManager;
public options: ClientOptions;
public readyAt: Date | null;
public readonly readyTimestamp: number | null;
public readyAt: If<Ready, Date>;
public readonly readyTimestamp: If<Ready, number>;
public shard: ShardClientUtil | null;
public token: string | null;
public readonly uptime: number | null;
public user: ClientUser | null;
public token: If<Ready, string, string | null>;
public uptime: If<Ready, number>;
public user: If<Ready, ClientUser>;
public users: UserManager;
public voice: ClientVoiceManager;
public ws: WebSocketManager;
Expand All @@ -307,6 +309,7 @@ export class Client extends BaseClient {
public fetchWidget(guild: GuildResolvable): Promise<Widget>;
public generateInvite(options?: InviteGenerationOptions): string;
public login(token?: string): Promise<string>;
public isReady(): this is Client<true>;
public sweepMessages(lifetime?: number): number;
public toJSON(): unknown;

Expand Down Expand Up @@ -2818,7 +2821,7 @@ export interface ClientEvents {
presenceUpdate: [oldPresence: Presence | null, newPresence: Presence];
rateLimit: [rateLimitData: RateLimitData];
invalidRequestWarning: [invalidRequestWarningData: InvalidRequestWarningData];
ready: [];
ready: [client: Client<true>];
invalidated: [];
roleCreate: [role: Role];
roleDelete: [role: Role];
Expand Down
23 changes: 23 additions & 0 deletions typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
ApplicationCommandResolvable,
CategoryChannel,
Client,
ClientApplication,
ClientUser,
Collection,
Constants,
DMChannel,
Expand Down Expand Up @@ -447,6 +449,27 @@ client.on('interaction', async interaction => {

client.login('absolutely-valid-token');

// Test client conditional types
client.on('ready', client => {
assertType<Client<true>>(client);
});

declare const loggedInClient: Client<true>;
assertType<ClientApplication>(loggedInClient.application);
assertType<Date>(loggedInClient.readyAt);
assertType<number>(loggedInClient.readyTimestamp);
assertType<string>(loggedInClient.token);
assertType<number>(loggedInClient.uptime);
assertType<ClientUser>(loggedInClient.user);

declare const loggedOutClient: Client<false>;
assertType<null>(loggedOutClient.application);
assertType<null>(loggedOutClient.readyAt);
assertType<null>(loggedOutClient.readyTimestamp);
assertType<string | null>(loggedOutClient.token);
assertType<null>(loggedOutClient.uptime);
assertType<null>(loggedOutClient.user);

// Test type transformation:
declare const assertType: <T>(value: T) => asserts value is T;
declare const serialize: <T>(value: T) => Serialized<T>;
Expand Down

0 comments on commit 4206e35

Please sign in to comment.