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!: use AsyncEventEmitter instead of EventEmitter #10710

Merged
merged 3 commits into from
Jan 23, 2025
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
1 change: 1 addition & 0 deletions packages/discord.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"@discordjs/util": "workspace:^",
"@discordjs/ws": "workspace:^",
"@sapphire/snowflake": "3.5.5",
"@vladfrangu/async_event_emitter": "^2.4.6",
"discord-api-types": "^0.37.114",
"fast-deep-equal": "3.1.3",
"lodash.snakecase": "4.1.1",
Expand Down
8 changes: 4 additions & 4 deletions packages/discord.js/src/client/BaseClient.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
'use strict';

const EventEmitter = require('node:events');
const { REST } = require('@discordjs/rest');
const { AsyncEventEmitter } = require('@vladfrangu/async_event_emitter');
const { Routes } = require('discord-api-types/v10');
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
const { Options } = require('../util/Options');
const { flatten } = require('../util/Util');

/**
* The base class for all clients.
* @extends {EventEmitter}
* @extends {AsyncEventEmitter}
*/
class BaseClient extends EventEmitter {
class BaseClient extends AsyncEventEmitter {
constructor(options = {}) {
super({ captureRejections: true });
super();

if (typeof options !== 'object' || options === null) {
throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true);
Expand Down
10 changes: 5 additions & 5 deletions packages/discord.js/src/sharding/Shard.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const EventEmitter = require('node:events');
const path = require('node:path');
const process = require('node:process');
const { setTimeout, clearTimeout } = require('node:timers');
const { setTimeout: sleep } = require('node:timers/promises');
const { SHARE_ENV } = require('node:worker_threads');
const { AsyncEventEmitter } = require('@vladfrangu/async_event_emitter');
const { DiscordjsError, ErrorCodes } = require('../errors');
const { ShardEvents } = require('../util/ShardEvents');
const { makeError, makePlainError } = require('../util/Util');
Expand All @@ -17,9 +17,9 @@ let Worker = null;
* A self-contained shard created by the {@link ShardingManager}. Each one has a {@link ChildProcess} that contains
* an instance of the bot and its {@link Client}. When its child process/worker exits for any reason, the shard will
* spawn a new one to replace it as necessary.
* @extends {EventEmitter}
* @extends {AsyncEventEmitter}
*/
class Shard extends EventEmitter {
class Shard extends AsyncEventEmitter {
constructor(manager, id) {
super();

Expand Down Expand Up @@ -445,7 +445,7 @@ class Shard extends EventEmitter {

/**
* Increments max listeners by one for a given emitter, if they are not zero.
* @param {EventEmitter|process} emitter The emitter that emits the events.
* @param {Worker|ChildProcess} emitter The emitter that emits the events.
* @private
*/
incrementMaxListeners(emitter) {
Expand All @@ -457,7 +457,7 @@ class Shard extends EventEmitter {

/**
* Decrements max listeners by one for a given emitter, if they are not zero.
* @param {EventEmitter|process} emitter The emitter that emits the events.
* @param {Worker|ChildProcess} emitter The emitter that emits the events.
* @private
*/
decrementMaxListeners(emitter) {
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/sharding/ShardClientUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class ShardClientUtil {

/**
* Increments max listeners by one for a given emitter, if they are not zero.
* @param {EventEmitter|process} emitter The emitter that emits the events.
* @param {Worker|ChildProcess} emitter The emitter that emits the events.
* @private
*/
incrementMaxListeners(emitter) {
Expand All @@ -254,7 +254,7 @@ class ShardClientUtil {

/**
* Decrements max listeners by one for a given emitter, if they are not zero.
* @param {EventEmitter|process} emitter The emitter that emits the events.
* @param {Worker|ChildProcess} emitter The emitter that emits the events.
* @private
*/
decrementMaxListeners(emitter) {
Expand Down
6 changes: 3 additions & 3 deletions packages/discord.js/src/sharding/ShardingManager.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

const EventEmitter = require('node:events');
const fs = require('node:fs');
const path = require('node:path');
const process = require('node:process');
const { setTimeout: sleep } = require('node:timers/promises');
const { Collection } = require('@discordjs/collection');
const { AsyncEventEmitter } = require('@vladfrangu/async_event_emitter');
const { Shard } = require('./Shard');
const { DiscordjsError, DiscordjsTypeError, DiscordjsRangeError, ErrorCodes } = require('../errors');
const { fetchRecommendedShardCount } = require('../util/Util');
Expand All @@ -17,9 +17,9 @@ const { fetchRecommendedShardCount } = require('../util/Util');
* process, and there are several useful methods that utilize it in order to simplify tasks that are normally difficult
* with sharding. It can spawn a specific number of shards or the amount that Discord suggests for the bot, and takes a
* path to your main bot script to launch for each one.
* @extends {EventEmitter}
* @extends {AsyncEventEmitter}
*/
class ShardingManager extends EventEmitter {
class ShardingManager extends AsyncEventEmitter {
/**
* The mode to spawn shards with for a {@link ShardingManager}. Can be either one of:
* * 'process' to use child processes
Expand Down
6 changes: 3 additions & 3 deletions packages/discord.js/src/structures/interfaces/Collector.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const EventEmitter = require('node:events');
const { setTimeout, clearTimeout } = require('node:timers');
const { Collection } = require('@discordjs/collection');
const { AsyncEventEmitter } = require('@vladfrangu/async_event_emitter');
const { DiscordjsTypeError, ErrorCodes } = require('../../errors');
const { flatten } = require('../../util/Util');

Expand All @@ -25,10 +25,10 @@ const { flatten } = require('../../util/Util');

/**
* Abstract class for defining a new Collector.
* @extends {EventEmitter}
* @extends {AsyncEventEmitter}
* @abstract
*/
class Collector extends EventEmitter {
class Collector extends AsyncEventEmitter {
constructor(client, options = {}) {
super();

Expand Down
Loading
Loading