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

feat(ShardingManager): add options typings #5583

Merged
merged 3 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 18 additions & 12 deletions src/sharding/ShardingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,30 @@ const Util = require('../util/Util');
*/
class ShardingManager extends EventEmitter {
/**
* The mode to spawn shards with for a {@link ShardingManager}: either "process" to use child processes, or
* The mode to spawn shards with for a {@link ShardingManager}. Either "process" to use child processes, or
* "worker" to use [Worker threads](https://nodejs.org/api/worker_threads.html).
* @typedef {Object} ShardingManagerMode
* @typedef {string} ShardingManagerMode
*/

/**
* The options to spawn shards with for a {@link ShardingManager},
* @typedef {Object} ShardingManagerOptions
* @property {string|number} [totalShards='auto'] Number of total shards of all shard managers or "auto"
* @property {string|number[]} [shardList='auto'] List of shards to spawn or "auto"
* @property {ShardingManagerMode} [mode='process'] Which mode to use for shards
* @property {boolean} [respawn=true] Whether shards should automatically respawn upon exiting
* @property {string[]} [shardArgs=[]] Arguments to pass to the shard script when spawning
* (only available when mode is set to 'process')
* @property {string} [execArgv=[]] Arguments to pass to the shard script executable when spawning
* (only available when mode is set to 'process')
* @property {string} [token] Token to use for automatical shard count and passing to shards
uhKevinMC marked this conversation as resolved.
Show resolved Hide resolved
*/

/**
* @param {string} file Path to your shard script file
* @param {Object} [options] Options for the sharding manager
* @param {string|number} [options.totalShards='auto'] Number of total shards of all shard managers or "auto"
* @param {string|number[]} [options.shardList='auto'] List of shards to spawn or "auto"
* @param {ShardingManagerMode} [options.mode='process'] Which mode to use for shards
* @param {boolean} [options.respawn=true] Whether shards should automatically respawn upon exiting
* @param {string[]} [options.shardArgs=[]] Arguments to pass to the shard script when spawning
* (only available when using the `process` mode)
* @param {string[]} [options.execArgv=[]] Arguments to pass to the shard script executable when spawning
* (only available when using the `process` mode)
* @param {string} [options.token] Token to use for automatic shard count and passing to shards
* @param {ShardingManagerOptions} [options] Options for the sharding manager
*/

uhKevinMC marked this conversation as resolved.
Show resolved Hide resolved
constructor(file, options = {}) {
super();
options = Util.mergeDefault(
Expand Down
39 changes: 21 additions & 18 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ declare module 'discord.js' {
public eval<T>(fn: (client: Client) => T): Promise<T[]>;
public fetchClientValue(prop: string): Promise<any>;
public kill(): void;
public respawn(options?: { delay?: number, timeout?: number }): Promise<ChildProcess>;
public respawn(options?: { delay?: number; timeout?: number }): Promise<ChildProcess>;
public send(message: any): Promise<Shard>;
public spawn(timeout?: number): Promise<ChildProcess>;

Expand Down Expand Up @@ -1411,26 +1411,15 @@ declare module 'discord.js' {
public broadcastEval<T>(fn: (client: Client) => T, shard: number): Promise<T>;
public fetchClientValues(prop: string): Promise<any[]>;
public fetchClientValues(prop: string, shard: number): Promise<any>;
public respawnAll(options?: { shardDelay?: number, respawnDelay?: number, timeout?: number }): Promise<void>;
public respawnAll(options?: { shardDelay?: number; respawnDelay?: number; timeout?: number }): Promise<void>;
public send(message: any): Promise<void>;

public static singleton(client: Client, mode: ShardingManagerMode): ShardClientUtil;
public static shardIDForGuildID(guildID: Snowflake, shardCount: number): number;
}

export class ShardingManager extends EventEmitter {
constructor(
file: string,
options?: {
totalShards?: number | 'auto';
shardList?: number[] | 'auto';
mode?: ShardingManagerMode;
respawn?: boolean;
shardArgs?: string[];
token?: string;
execArgv?: string[];
},
);
constructor(file: string, options?: ShardingManagerOptions);
private _performOnShards(method: string, args: any[]): Promise<any[]>;
private _performOnShards(method: string, args: any[], shard: number): Promise<any>;

Expand All @@ -1448,11 +1437,15 @@ declare module 'discord.js' {
public fetchClientValues(prop: string): Promise<any[]>;
public fetchClientValues(prop: string, shard: number): Promise<any>;
public respawnAll(options?: {
shardDelay?: number,
respawnDelay?: number,
timeout?: number,
shardDelay?: number;
respawnDelay?: number;
timeout?: number;
}): Promise<Collection<number, Shard>>;
public spawn(options?: {
amount?: number | 'auto';
delay?: number;
timeout?: number;
}): Promise<Collection<number, Shard>>;
public spawn(options?: { amount?: number | 'auto', delay?: number, timeout?: number }): Promise<Collection<number, Shard>>;

public on(event: 'shardCreate', listener: (shard: Shard) => void): this;

Expand Down Expand Up @@ -3261,6 +3254,16 @@ declare module 'discord.js' {

type ShardingManagerMode = 'process' | 'worker';

interface ShardingManagerOptions {
totalShards?: number | 'auto';
shardList?: number[] | 'auto';
mode?: ShardingManagerMode;
respawn?: boolean;
shardArgs?: string[];
token?: string;
execArgv?: string[];
}

type Snowflake = string;

interface SplitOptions {
Expand Down