mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
refactor: use interfaces for AsyncEventEmitter event maps (#10044)
* refactor: use interfaces for AsyncEventEmitter event maps * refactor: apply suggestions from code review and add tests * refactor: better errors on missing dispatch types
This commit is contained in:
@@ -67,7 +67,7 @@
|
||||
"homepage": "https://discord.js.org",
|
||||
"dependencies": {
|
||||
"@msgpack/msgpack": "^3.0.0-beta2",
|
||||
"@vladfrangu/async_event_emitter": "^2.2.2",
|
||||
"@vladfrangu/async_event_emitter": "^2.2.4",
|
||||
"ioredis": "^5.3.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -68,7 +68,7 @@
|
||||
"@discordjs/util": "workspace:^",
|
||||
"@discordjs/ws": "workspace:^",
|
||||
"@sapphire/snowflake": "^3.5.1",
|
||||
"@vladfrangu/async_event_emitter": "^2.2.2",
|
||||
"@vladfrangu/async_event_emitter": "^2.2.4",
|
||||
"discord-api-types": "0.37.61"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -163,9 +163,7 @@ export interface MappedEvents {
|
||||
[GatewayDispatchEvents.WebhooksUpdate]: [WithIntrinsicProps<GatewayWebhooksUpdateDispatchData>];
|
||||
}
|
||||
|
||||
export type ManagerShardEventsMap = {
|
||||
[K in keyof MappedEvents]: MappedEvents[K];
|
||||
};
|
||||
export interface ManagerShardEventsMap extends MappedEvents {}
|
||||
|
||||
export interface ClientOptions {
|
||||
gateway: Gateway;
|
||||
@@ -179,7 +177,7 @@ export interface RequestGuildMembersResult {
|
||||
presences: NonNullable<GatewayGuildMembersChunkDispatchData['presences']>;
|
||||
}
|
||||
|
||||
export class Client extends AsyncEventEmitter<ManagerShardEventsMap> {
|
||||
export class Client extends AsyncEventEmitter<MappedEvents> {
|
||||
public readonly rest: REST;
|
||||
|
||||
public readonly gateway: Gateway;
|
||||
@@ -193,8 +191,12 @@ export class Client extends AsyncEventEmitter<ManagerShardEventsMap> {
|
||||
this.api = new API(rest);
|
||||
|
||||
this.gateway.on(WebSocketShardEvents.Dispatch, ({ data: dispatch, shardId }) => {
|
||||
// @ts-expect-error event props can't be resolved properly, but they are correct
|
||||
this.emit(dispatch.t, this.wrapIntrinsicProps(dispatch.d, shardId));
|
||||
this.emit(
|
||||
// TODO: move this expect-error down to the next line once entitlements get merged, so missing dispatch types result in errors
|
||||
// @ts-expect-error event props can't be resolved properly, but they are correct
|
||||
dispatch.t,
|
||||
this.wrapIntrinsicProps(dispatch.d, shardId),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -86,7 +86,7 @@
|
||||
"@discordjs/util": "workspace:^",
|
||||
"@sapphire/async-queue": "^1.5.0",
|
||||
"@sapphire/snowflake": "^3.5.1",
|
||||
"@vladfrangu/async_event_emitter": "^2.2.2",
|
||||
"@vladfrangu/async_event_emitter": "^2.2.4",
|
||||
"discord-api-types": "0.37.61",
|
||||
"magic-bytes.js": "^1.5.0",
|
||||
"tslib": "^2.6.2",
|
||||
|
||||
@@ -18,7 +18,7 @@ import { RequestMethod } from './utils/types.js';
|
||||
import type {
|
||||
RESTOptions,
|
||||
ResponseLike,
|
||||
RestEventsMap,
|
||||
RestEvents,
|
||||
HashData,
|
||||
InternalRequest,
|
||||
RouteLike,
|
||||
@@ -31,7 +31,7 @@ import { isBufferLike, parseResponse } from './utils/utils.js';
|
||||
/**
|
||||
* Represents the class that manages handlers for endpoints
|
||||
*/
|
||||
export class REST extends AsyncEventEmitter<RestEventsMap> {
|
||||
export class REST extends AsyncEventEmitter<RestEvents> {
|
||||
/**
|
||||
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} for all requests
|
||||
* performed by this manager.
|
||||
|
||||
@@ -14,9 +14,7 @@ export interface RestEvents {
|
||||
restDebug: [info: string];
|
||||
}
|
||||
|
||||
export type RestEventsMap = {
|
||||
[K in keyof RestEvents]: RestEvents[K];
|
||||
};
|
||||
export interface RestEventsMap extends RestEvents {}
|
||||
|
||||
/**
|
||||
* Options to be passed when creating the REST instance
|
||||
|
||||
15
packages/ws/__tests__/types/WebSocketManager.test-d.ts
Normal file
15
packages/ws/__tests__/types/WebSocketManager.test-d.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import type { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
||||
import { expectType, expectAssignable } from 'tsd';
|
||||
import type { ManagerShardEventsMap, WebSocketShardEventsMap, WebSocketManager } from '../../src/index.js';
|
||||
|
||||
declare const manager: WebSocketManager;
|
||||
declare const eventMap: ManagerShardEventsMap;
|
||||
|
||||
type AugmentedShardEventsMap = {
|
||||
[K in keyof WebSocketShardEventsMap]: [
|
||||
WebSocketShardEventsMap[K] extends [] ? { shardId: number } : WebSocketShardEventsMap[K][0] & { shardId: number },
|
||||
];
|
||||
};
|
||||
|
||||
expectType<AugmentedShardEventsMap>(eventMap);
|
||||
expectAssignable<AsyncEventEmitter<AugmentedShardEventsMap>>(manager);
|
||||
@@ -77,7 +77,7 @@
|
||||
"@discordjs/util": "workspace:^",
|
||||
"@sapphire/async-queue": "^1.5.0",
|
||||
"@types/ws": "^8.5.9",
|
||||
"@vladfrangu/async_event_emitter": "^2.2.2",
|
||||
"@vladfrangu/async_event_emitter": "^2.2.4",
|
||||
"discord-api-types": "0.37.61",
|
||||
"tslib": "^2.6.2",
|
||||
"ws": "^8.14.2"
|
||||
@@ -94,6 +94,7 @@
|
||||
"eslint-formatter-pretty": "^5.0.0",
|
||||
"mock-socket": "^9.3.1",
|
||||
"prettier": "^3.1.0",
|
||||
"tsd": "^0.29.0",
|
||||
"tsup": "^7.2.0",
|
||||
"turbo": "^1.10.17-canary.0",
|
||||
"typescript": "^5.2.2",
|
||||
|
||||
@@ -9,11 +9,13 @@ import {
|
||||
type RESTGetAPIGatewayBotResult,
|
||||
type GatewayIntentBits,
|
||||
type GatewaySendPayload,
|
||||
type GatewayDispatchPayload,
|
||||
type GatewayReadyDispatchData,
|
||||
} from 'discord-api-types/v10';
|
||||
import type { IShardingStrategy } from '../strategies/sharding/IShardingStrategy.js';
|
||||
import type { IIdentifyThrottler } from '../throttling/IIdentifyThrottler.js';
|
||||
import { DefaultWebSocketManagerOptions, type CompressionMethod, type Encoding } from '../utils/constants.js';
|
||||
import type { WebSocketShardDestroyOptions, WebSocketShardEventsMap } from './WebSocketShard.js';
|
||||
import type { WebSocketShardDestroyOptions, WebSocketShardEvents } from './WebSocketShard.js';
|
||||
|
||||
/**
|
||||
* Represents a range of shard ids
|
||||
@@ -178,13 +180,24 @@ export interface OptionalWebSocketManagerOptions {
|
||||
version: string;
|
||||
}
|
||||
|
||||
export type WebSocketManagerOptions = OptionalWebSocketManagerOptions & RequiredWebSocketManagerOptions;
|
||||
export interface WebSocketManagerOptions extends OptionalWebSocketManagerOptions, RequiredWebSocketManagerOptions {}
|
||||
|
||||
export type ManagerShardEventsMap = {
|
||||
[K in keyof WebSocketShardEventsMap]: [
|
||||
WebSocketShardEventsMap[K] extends [] ? { shardId: number } : WebSocketShardEventsMap[K][0] & { shardId: number },
|
||||
export interface CreateWebSocketManagerOptions
|
||||
extends Partial<OptionalWebSocketManagerOptions>,
|
||||
RequiredWebSocketManagerOptions {}
|
||||
|
||||
export interface ManagerShardEventsMap {
|
||||
[WebSocketShardEvents.Closed]: [{ code: number; shardId: number }];
|
||||
[WebSocketShardEvents.Debug]: [payload: { message: string; shardId: number }];
|
||||
[WebSocketShardEvents.Dispatch]: [payload: { data: GatewayDispatchPayload; shardId: number }];
|
||||
[WebSocketShardEvents.Error]: [payload: { error: Error; shardId: number }];
|
||||
[WebSocketShardEvents.Hello]: [{ shardId: number }];
|
||||
[WebSocketShardEvents.Ready]: [payload: { data: GatewayReadyDispatchData; shardId: number }];
|
||||
[WebSocketShardEvents.Resumed]: [{ shardId: number }];
|
||||
[WebSocketShardEvents.HeartbeatComplete]: [
|
||||
payload: { ackAt: number; heartbeatAt: number; latency: number; shardId: number },
|
||||
];
|
||||
};
|
||||
}
|
||||
|
||||
export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> {
|
||||
/**
|
||||
@@ -212,7 +225,7 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> {
|
||||
*/
|
||||
private readonly strategy: IShardingStrategy;
|
||||
|
||||
public constructor(options: Partial<OptionalWebSocketManagerOptions> & RequiredWebSocketManagerOptions) {
|
||||
public constructor(options: CreateWebSocketManagerOptions) {
|
||||
super();
|
||||
this.options = { ...DefaultWebSocketManagerOptions, ...options };
|
||||
this.strategy = this.options.buildStrategy(this);
|
||||
|
||||
@@ -52,8 +52,7 @@ export enum WebSocketShardDestroyRecovery {
|
||||
Resume,
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
||||
export type WebSocketShardEventsMap = {
|
||||
export interface WebSocketShardEventsMap {
|
||||
[WebSocketShardEvents.Closed]: [{ code: number }];
|
||||
[WebSocketShardEvents.Debug]: [payload: { message: string }];
|
||||
[WebSocketShardEvents.Dispatch]: [payload: { data: GatewayDispatchPayload }];
|
||||
@@ -62,7 +61,7 @@ export type WebSocketShardEventsMap = {
|
||||
[WebSocketShardEvents.Ready]: [payload: { data: GatewayReadyDispatchData }];
|
||||
[WebSocketShardEvents.Resumed]: [];
|
||||
[WebSocketShardEvents.HeartbeatComplete]: [payload: { ackAt: number; heartbeatAt: number; latency: number }];
|
||||
};
|
||||
}
|
||||
|
||||
export interface WebSocketShardDestroyOptions {
|
||||
code?: number;
|
||||
|
||||
Reference in New Issue
Block a user