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

refactor(utils): remove mergeDefault #9938

Merged
merged 2 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 deletions packages/discord.js/src/client/BaseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
const Options = require('../util/Options');
const { mergeDefault, flatten } = require('../util/Util');
const { flatten } = require('../util/Util');

/**
* The base class for all clients.
Expand All @@ -23,15 +23,21 @@ class BaseClient extends EventEmitter {
* The options the client was instantiated with
* @type {ClientOptions}
*/
this.options = mergeDefault(Options.createDefault(), {
const defaultOptions = Options.createDefault();
this.options = {
...defaultOptions,
...options,
ws: {
...defaultOptions.ws,
...options.ws,
},
rest: {
...options.rest,
userAgentAppendix: options.rest?.userAgentAppendix
? `${Options.userAgentAppendix} ${options.rest.userAgentAppendix}`
: undefined,
},
});
};

/**
* The REST manager of the client
Expand Down
42 changes: 20 additions & 22 deletions packages/discord.js/src/sharding/ShardingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { setTimeout: sleep } = require('node:timers/promises');
const { Collection } = require('@discordjs/collection');
const Shard = require('./Shard');
const { DiscordjsError, DiscordjsTypeError, DiscordjsRangeError, ErrorCodes } = require('../errors');
const { mergeDefault, fetchRecommendedShardCount } = require('../util/Util');
const { fetchRecommendedShardCount } = require('../util/Util');

/**
* This is a utility class that makes multi-process sharding of a bot an easy and painless experience.
Expand Down Expand Up @@ -47,20 +47,18 @@ class ShardingManager extends EventEmitter {
* @param {string} file Path to your shard script file
* @param {ShardingManagerOptions} [options] Options for the sharding manager
*/
constructor(file, options = {}) {
constructor(file, options) {
super();
options = mergeDefault(
{
totalShards: 'auto',
mode: 'process',
respawn: true,
silent: false,
shardArgs: [],
execArgv: [],
token: process.env.DISCORD_TOKEN,
},
options,
);
const _options = {
totalShards: 'auto',
mode: 'process',
respawn: true,
silent: false,
shardArgs: [],
execArgv: [],
token: process.env.DISCORD_TOKEN,
...options,
};

/**
* Path to the shard script file
Expand All @@ -76,7 +74,7 @@ class ShardingManager extends EventEmitter {
* List of shards this sharding manager spawns
* @type {string|number[]}
*/
this.shardList = options.shardList ?? 'auto';
this.shardList = _options.shardList ?? 'auto';
if (this.shardList !== 'auto') {
if (!Array.isArray(this.shardList)) {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'shardList', 'an array.');
Expand All @@ -98,7 +96,7 @@ class ShardingManager extends EventEmitter {
* Amount of shards that all sharding managers spawn in total
* @type {number}
*/
this.totalShards = options.totalShards || 'auto';
this.totalShards = _options.totalShards || 'auto';
if (this.totalShards !== 'auto') {
if (typeof this.totalShards !== 'number' || isNaN(this.totalShards)) {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'Amount of shards', 'a number.');
Expand All @@ -115,7 +113,7 @@ class ShardingManager extends EventEmitter {
* Mode for shards to spawn with
* @type {ShardingManagerMode}
*/
this.mode = options.mode;
this.mode = _options.mode;
if (this.mode !== 'process' && this.mode !== 'worker') {
throw new DiscordjsRangeError(ErrorCodes.ClientInvalidOption, 'Sharding mode', '"process" or "worker"');
}
Expand All @@ -124,31 +122,31 @@ class ShardingManager extends EventEmitter {
* Whether shards should automatically respawn upon exiting
* @type {boolean}
*/
this.respawn = options.respawn;
this.respawn = _options.respawn;

/**
* Whether to pass the silent flag to child process (only when {@link ShardingManager#mode} is `process`)
* @type {boolean}
*/
this.silent = options.silent;
this.silent = _options.silent;

/**
* An array of arguments to pass to shards (only when {@link ShardingManager#mode} is `process`)
* @type {string[]}
*/
this.shardArgs = options.shardArgs;
this.shardArgs = _options.shardArgs;

/**
* An array of arguments to pass to the executable (only when {@link ShardingManager#mode} is `process`)
* @type {string[]}
*/
this.execArgv = options.execArgv;
this.execArgv = _options.execArgv;

/**
* Token to use for obtaining the automatic shard count, and passing to shards
* @type {?string}
*/
this.token = options.token?.replace(/^Bot\s*/i, '') ?? null;
this.token = _options.token?.replace(/^Bot\s*/i, '') ?? null;

/**
* A collection of shards that this manager has spawned
Expand Down
21 changes: 0 additions & 21 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,6 @@ function resolvePartialEmoji(emoji) {
return { id, name, animated: Boolean(animated) };
}

/**
* Sets default properties on an object that aren't already specified.
* @param {Object} def Default properties
* @param {Object} given Object to assign defaults to
* @returns {Object}
* @private
*/
function mergeDefault(def, given) {
if (!given) return def;
for (const key in def) {
if (!Object.hasOwn(given, key) || given[key] === undefined) {
given[key] = def[key];
} else if (given[key] === Object(given[key])) {
given[key] = mergeDefault(def[key], given[key]);
}
}

return given;
}

/**
* Options used to make an error object.
* @typedef {Object} MakeErrorOptions
Expand Down Expand Up @@ -434,7 +414,6 @@ module.exports = {
fetchRecommendedShardCount,
parseEmoji,
resolvePartialEmoji,
mergeDefault,
makeError,
makePlainError,
getSortableGroupTypes,
Expand Down
1 change: 0 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3207,7 +3207,6 @@ export function fetchRecommendedShardCount(token: string, options?: FetchRecomme
export function flatten(obj: unknown, ...props: Record<string, boolean | string>[]): unknown;
export function makeError(obj: MakeErrorOptions): Error;
export function makePlainError(err: Error): MakeErrorOptions;
export function mergeDefault(def: unknown, given: unknown): unknown;
export function moveElementInArray(array: unknown[], element: unknown, newIndex: number, offset?: boolean): number;
export function parseEmoji(text: string): PartialEmoji | null;
export function resolveColor(color: ColorResolvable): number;
Expand Down