Skip to content

Commit

Permalink
Merge branch 'main' into ci/upload-docs-with-main
Browse files Browse the repository at this point in the history
  • Loading branch information
Qjuh committed Dec 14, 2023
2 parents c374ae7 + adfd9cd commit f1c49c6
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 66 deletions.
2 changes: 1 addition & 1 deletion packages/brokers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 8 additions & 6 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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),
);
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/rest/src/lib/REST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { RequestMethod } from './utils/types.js';
import type {
RESTOptions,
ResponseLike,
RestEventsMap,
RestEvents,
HashData,
InternalRequest,
RouteLike,
Expand All @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions packages/rest/src/lib/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions packages/ws/__tests__/types/WebSocketManager.test-d.ts
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 2 additions & 1 deletion packages/ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
27 changes: 20 additions & 7 deletions packages/ws/src/ws/WebSocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> {
/**
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }];
Expand All @@ -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;
Expand Down
Loading

0 comments on commit f1c49c6

Please sign in to comment.