Skip to content

Commit

Permalink
fix(Sharding): strict type context and return (#5933)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Jun 29, 2021
1 parent 87e8cdd commit 1925d01
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 12 deletions.
40 changes: 29 additions & 11 deletions typings/index.d.ts
Expand Up @@ -150,7 +150,7 @@ declare enum WebhookTypes {
'Channel Follower' = 2,
}

type Awaited<T> = T | Promise<T>;
type Awaited<T> = T | PromiseLike<T>;

declare module '@discordjs/voice' {
import { GatewayVoiceServerUpdateDispatchData, GatewayVoiceStateUpdateDispatchData } from 'discord-api-types/v8';
Expand Down Expand Up @@ -1793,13 +1793,16 @@ declare module 'discord.js' {
public readonly ids: number[];
public mode: ShardingManagerMode;
public parentPort: any | null;
public broadcastEval<T>(fn: (client: Client) => T): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<T>;
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, options: { context: P }): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => Awaited<T>): Promise<Serialized<T>[]>;
public broadcastEval<T>(fn: (client: Client) => Awaited<T>, options: { shard: number }): Promise<Serialized<T>>;
public broadcastEval<T, P>(
fn: (client: Client, context: P) => T,
fn: (client: Client, context: Serialized<P>) => Awaited<T>,
options: { context: P },
): Promise<Serialized<T>[]>;
public broadcastEval<T, P>(
fn: (client: Client, context: Serialized<P>) => Awaited<T>,
options: { context: P; shard: number },
): Promise<T>;
): Promise<Serialized<T>>;
public fetchClientValues(prop: string): Promise<any[]>;
public fetchClientValues(prop: string, shard: number): Promise<any>;
public respawnAll(options?: MultipleShardRespawnOptions): Promise<void>;
Expand All @@ -1822,13 +1825,16 @@ declare module 'discord.js' {
public totalShards: number | 'auto';
public shardList: number[] | 'auto';
public broadcast(message: any): Promise<Shard[]>;
public broadcastEval<T>(fn: (client: Client) => T): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => T, options: { shard: number }): Promise<T>;
public broadcastEval<T, P>(fn: (client: Client, context: P) => T, options: { context: P }): Promise<T[]>;
public broadcastEval<T>(fn: (client: Client) => Awaited<T>): Promise<Serialized<T>[]>;
public broadcastEval<T>(fn: (client: Client) => Awaited<T>, options: { shard: number }): Promise<Serialized<T>>;
public broadcastEval<T, P>(
fn: (client: Client, context: Serialized<P>) => Awaited<T>,
options: { context: P },
): Promise<Serialized<T>[]>;
public broadcastEval<T, P>(
fn: (client: Client, context: P) => T,
fn: (client: Client, context: Serialized<P>) => Awaited<T>,
options: { context: P; shard: number },
): Promise<T>;
): Promise<Serialized<T>>;
public createShard(id: number): Shard;
public fetchClientValues(prop: string): Promise<any[]>;
public fetchClientValues(prop: string, shard: number): Promise<any>;
Expand Down Expand Up @@ -4408,5 +4414,17 @@ declare module 'discord.js' {
| 'STAGE_INSTANCE_UPDATE'
| 'STAGE_INSTANCE_DELETE';

type Serialized<T> = T extends symbol | bigint | (() => unknown)
? never
: T extends number | string | boolean | undefined
? T
: T extends { toJSON(): infer R }
? R
: T extends ReadonlyArray<infer V>
? Serialized<V>[]
: T extends ReadonlyMap<unknown, unknown> | ReadonlySet<unknown>
? {}
: { [K in keyof T]: Serialized<T[K]> };

//#endregion
}
52 changes: 51 additions & 1 deletion typings/index.ts
@@ -1,6 +1,17 @@
/// <reference path="index.d.ts" />

import { Client, Intents, Message, MessageAttachment, MessageEmbed } from 'discord.js';
import {
Client,
Collection,
Intents,
Message,
MessageAttachment,
MessageEmbed,
Permissions,
Serialized,
ShardClientUtil,
ShardingManager,
} from 'discord.js';

const client: Client = new Client({
intents: Intents.NON_PRIVILEGED,
Expand Down Expand Up @@ -48,3 +59,42 @@ client.on('message', ({ channel }) => {
});

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

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

assertType<undefined>(serialize(undefined));
assertType<null>(serialize(null));
assertType<number[]>(serialize([1, 2, 3]));
assertType<{}>(serialize(new Set([1, 2, 3])));
assertType<{}>(
serialize(
new Map([
[1, '2'],
[2, '4'],
]),
),
);
assertType<string>(serialize(new Permissions(Permissions.FLAGS.ATTACH_FILES)));
assertType<number>(serialize(new Intents(Intents.FLAGS.GUILDS)));
assertType<unknown>(
serialize(
new Collection([
[1, '2'],
[2, '4'],
]),
),
);
assertType<never>(serialize(Symbol('a')));
assertType<never>(serialize(() => {}));
assertType<never>(serialize(BigInt(42)));

// Test type return of broadcastEval:
declare const shardClientUtil: ShardClientUtil;
declare const shardingManager: ShardingManager;

assertType<Promise<number[]>>(shardingManager.broadcastEval(() => 1));
assertType<Promise<number[]>>(shardClientUtil.broadcastEval(() => 1));
assertType<Promise<number[]>>(shardingManager.broadcastEval(async () => 1));
assertType<Promise<number[]>>(shardClientUtil.broadcastEval(async () => 1));

0 comments on commit 1925d01

Please sign in to comment.