Skip to content

Commit 5b0aa92

Browse files
authored
refactor(utils): remove mergeDefault (#9938)
* refactor(utils): remove `mergeDefault` `BaseClient` and `ShardingManager` not longer mutate options * refactor(ShardingManager): avoid reassigning method argument
1 parent d28814d commit 5b0aa92

File tree

4 files changed

+29
-47
lines changed

4 files changed

+29
-47
lines changed

packages/discord.js/src/client/BaseClient.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { REST } = require('@discordjs/rest');
55
const { Routes } = require('discord-api-types/v10');
66
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
77
const Options = require('../util/Options');
8-
const { mergeDefault, flatten } = require('../util/Util');
8+
const { flatten } = require('../util/Util');
99

1010
/**
1111
* The base class for all clients.
@@ -23,15 +23,21 @@ class BaseClient extends EventEmitter {
2323
* The options the client was instantiated with
2424
* @type {ClientOptions}
2525
*/
26-
this.options = mergeDefault(Options.createDefault(), {
26+
const defaultOptions = Options.createDefault();
27+
this.options = {
28+
...defaultOptions,
2729
...options,
30+
ws: {
31+
...defaultOptions.ws,
32+
...options.ws,
33+
},
2834
rest: {
2935
...options.rest,
3036
userAgentAppendix: options.rest?.userAgentAppendix
3137
? `${Options.userAgentAppendix} ${options.rest.userAgentAppendix}`
3238
: undefined,
3339
},
34-
});
40+
};
3541

3642
/**
3743
* The REST manager of the client

packages/discord.js/src/sharding/ShardingManager.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { setTimeout: sleep } = require('node:timers/promises');
88
const { Collection } = require('@discordjs/collection');
99
const Shard = require('./Shard');
1010
const { DiscordjsError, DiscordjsTypeError, DiscordjsRangeError, ErrorCodes } = require('../errors');
11-
const { mergeDefault, fetchRecommendedShardCount } = require('../util/Util');
11+
const { fetchRecommendedShardCount } = require('../util/Util');
1212

1313
/**
1414
* This is a utility class that makes multi-process sharding of a bot an easy and painless experience.
@@ -47,20 +47,18 @@ class ShardingManager extends EventEmitter {
4747
* @param {string} file Path to your shard script file
4848
* @param {ShardingManagerOptions} [options] Options for the sharding manager
4949
*/
50-
constructor(file, options = {}) {
50+
constructor(file, options) {
5151
super();
52-
options = mergeDefault(
53-
{
54-
totalShards: 'auto',
55-
mode: 'process',
56-
respawn: true,
57-
silent: false,
58-
shardArgs: [],
59-
execArgv: [],
60-
token: process.env.DISCORD_TOKEN,
61-
},
62-
options,
63-
);
52+
const _options = {
53+
totalShards: 'auto',
54+
mode: 'process',
55+
respawn: true,
56+
silent: false,
57+
shardArgs: [],
58+
execArgv: [],
59+
token: process.env.DISCORD_TOKEN,
60+
...options,
61+
};
6462

6563
/**
6664
* Path to the shard script file
@@ -76,7 +74,7 @@ class ShardingManager extends EventEmitter {
7674
* List of shards this sharding manager spawns
7775
* @type {string|number[]}
7876
*/
79-
this.shardList = options.shardList ?? 'auto';
77+
this.shardList = _options.shardList ?? 'auto';
8078
if (this.shardList !== 'auto') {
8179
if (!Array.isArray(this.shardList)) {
8280
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'shardList', 'an array.');
@@ -98,7 +96,7 @@ class ShardingManager extends EventEmitter {
9896
* Amount of shards that all sharding managers spawn in total
9997
* @type {number}
10098
*/
101-
this.totalShards = options.totalShards || 'auto';
99+
this.totalShards = _options.totalShards || 'auto';
102100
if (this.totalShards !== 'auto') {
103101
if (typeof this.totalShards !== 'number' || isNaN(this.totalShards)) {
104102
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'Amount of shards', 'a number.');
@@ -115,7 +113,7 @@ class ShardingManager extends EventEmitter {
115113
* Mode for shards to spawn with
116114
* @type {ShardingManagerMode}
117115
*/
118-
this.mode = options.mode;
116+
this.mode = _options.mode;
119117
if (this.mode !== 'process' && this.mode !== 'worker') {
120118
throw new DiscordjsRangeError(ErrorCodes.ClientInvalidOption, 'Sharding mode', '"process" or "worker"');
121119
}
@@ -124,31 +122,31 @@ class ShardingManager extends EventEmitter {
124122
* Whether shards should automatically respawn upon exiting
125123
* @type {boolean}
126124
*/
127-
this.respawn = options.respawn;
125+
this.respawn = _options.respawn;
128126

129127
/**
130128
* Whether to pass the silent flag to child process (only when {@link ShardingManager#mode} is `process`)
131129
* @type {boolean}
132130
*/
133-
this.silent = options.silent;
131+
this.silent = _options.silent;
134132

135133
/**
136134
* An array of arguments to pass to shards (only when {@link ShardingManager#mode} is `process`)
137135
* @type {string[]}
138136
*/
139-
this.shardArgs = options.shardArgs;
137+
this.shardArgs = _options.shardArgs;
140138

141139
/**
142140
* An array of arguments to pass to the executable (only when {@link ShardingManager#mode} is `process`)
143141
* @type {string[]}
144142
*/
145-
this.execArgv = options.execArgv;
143+
this.execArgv = _options.execArgv;
146144

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

153151
/**
154152
* A collection of shards that this manager has spawned

packages/discord.js/src/util/Util.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,6 @@ function resolvePartialEmoji(emoji) {
122122
return { id, name, animated: Boolean(animated) };
123123
}
124124

125-
/**
126-
* Sets default properties on an object that aren't already specified.
127-
* @param {Object} def Default properties
128-
* @param {Object} given Object to assign defaults to
129-
* @returns {Object}
130-
* @private
131-
*/
132-
function mergeDefault(def, given) {
133-
if (!given) return def;
134-
for (const key in def) {
135-
if (!Object.hasOwn(given, key) || given[key] === undefined) {
136-
given[key] = def[key];
137-
} else if (given[key] === Object(given[key])) {
138-
given[key] = mergeDefault(def[key], given[key]);
139-
}
140-
}
141-
142-
return given;
143-
}
144-
145125
/**
146126
* Options used to make an error object.
147127
* @typedef {Object} MakeErrorOptions
@@ -434,7 +414,6 @@ module.exports = {
434414
fetchRecommendedShardCount,
435415
parseEmoji,
436416
resolvePartialEmoji,
437-
mergeDefault,
438417
makeError,
439418
makePlainError,
440419
getSortableGroupTypes,

packages/discord.js/typings/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3207,7 +3207,6 @@ export function fetchRecommendedShardCount(token: string, options?: FetchRecomme
32073207
export function flatten(obj: unknown, ...props: Record<string, boolean | string>[]): unknown;
32083208
export function makeError(obj: MakeErrorOptions): Error;
32093209
export function makePlainError(err: Error): MakeErrorOptions;
3210-
export function mergeDefault(def: unknown, given: unknown): unknown;
32113210
export function moveElementInArray(array: unknown[], element: unknown, newIndex: number, offset?: boolean): number;
32123211
export function parseEmoji(text: string): PartialEmoji | null;
32133212
export function resolveColor(color: ColorResolvable): number;

0 commit comments

Comments
 (0)