feat(docgen): proper event parsing for typescript

This commit is contained in:
iCrawl
2022-06-10 16:22:11 +02:00
parent 0415300243
commit d4b41dd081
15 changed files with 113 additions and 140 deletions

View File

@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/prefer-ts-expect-error */
import { EventEmitter } from 'node:events';
import type { GatewayVoiceServerUpdateDispatchData, GatewayVoiceStateUpdateDispatchData } from 'discord-api-types/v10';
import { TypedEmitter } from 'tiny-typed-emitter';
import type { CreateVoiceConnectionOptions } from '.';
import {
getVoiceConnection,
@@ -15,7 +14,7 @@ import type { VoiceWebSocket, VoiceUDPSocket } from './networking';
import { Networking, NetworkingState, NetworkingStatusCode } from './networking/Networking';
import { VoiceReceiver } from './receive';
import type { DiscordGatewayAdapterImplementerMethods } from './util/adapter';
import { Awaited, noop } from './util/util';
import { noop } from './util/util';
/**
* The various status codes a voice connection can hold at any one time.
@@ -162,21 +161,10 @@ export type VoiceConnectionState =
| VoiceConnectionReadyState
| VoiceConnectionDestroyedState;
export type VoiceConnectionEvents = {
error: (error: Error) => Awaited<void>;
debug: (message: string) => Awaited<void>;
stateChange: (oldState: VoiceConnectionState, newState: VoiceConnectionState) => Awaited<void>;
} & {
[status in VoiceConnectionStatus]: (
oldState: VoiceConnectionState,
newState: VoiceConnectionState & { status: status },
) => Awaited<void>;
};
/**
* A connection to the voice server of a Guild, can be used to play audio in voice channels.
*/
export class VoiceConnection extends TypedEmitter<VoiceConnectionEvents> {
export class VoiceConnection extends EventEmitter {
/**
* The number of consecutive rejoin attempts. Initially 0, and increments for each rejoin.
* When a connection is successfully established, it resets to 0.
@@ -673,7 +661,6 @@ export class VoiceConnection extends TypedEmitter<VoiceConnectionEvents> {
*
* @param subscription - The removed subscription
*/
// @ts-ignore
private onSubscriptionRemoved(subscription: PlayerSubscription) {
if (this.state.status !== VoiceConnectionStatus.Destroyed && this.state.subscription === subscription) {
this.state = {

View File

@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/prefer-ts-expect-error */
import { TypedEmitter } from 'tiny-typed-emitter';
import EventEmitter from 'node:events';
import { AudioPlayerError } from './AudioPlayerError';
import type { AudioResource } from './AudioResource';
import { PlayerSubscription } from './PlayerSubscription';
import { addAudioPlayer, deleteAudioPlayer } from '../DataStore';
import { VoiceConnection, VoiceConnectionStatus } from '../VoiceConnection';
import { Awaited, noop } from '../util/util';
import { noop } from '../util/util';
// The Opus "silent" frame
export const SILENCE_FRAME = Buffer.from([0xf8, 0xff, 0xfe]);
@@ -151,18 +151,14 @@ export type AudioPlayerState =
| AudioPlayerPlayingState
| AudioPlayerPausedState;
export type AudioPlayerEvents = {
error: (error: AudioPlayerError) => Awaited<void>;
debug: (message: string) => Awaited<void>;
stateChange: (oldState: AudioPlayerState, newState: AudioPlayerState) => Awaited<void>;
subscribe: (subscription: PlayerSubscription) => Awaited<void>;
unsubscribe: (subscription: PlayerSubscription) => Awaited<void>;
} & {
[status in AudioPlayerStatus]: (
oldState: AudioPlayerState,
newState: AudioPlayerState & { status: status },
) => Awaited<void>;
};
export interface AudioPlayer extends EventEmitter {
/**
* Emitted when there is an error emitted from the audio resource played by the audio player
*
* @event
*/
on: (event: 'error', listener: (error: AudioPlayerError) => void) => this;
}
/**
* Stringifies an AudioPlayerState instance.
@@ -187,7 +183,7 @@ function stringifyState(state: AudioPlayerState) {
* The AudioPlayer drives the timing of playback, and therefore is unaffected by voice connections
* becoming unavailable. Its behavior in these scenarios can be configured.
*/
export class AudioPlayer extends TypedEmitter<AudioPlayerEvents> {
export class AudioPlayer extends EventEmitter {
/**
* The state that the AudioPlayer is in.
*/
@@ -372,12 +368,6 @@ export class AudioPlayer extends TypedEmitter<AudioPlayerEvents> {
// state if the resource is still being used.
const onStreamError = (error: Error) => {
if (this.state.status !== AudioPlayerStatus.Idle) {
/**
* Emitted when there is an error emitted from the audio resource played by the audio player
*
* @event AudioPlayer#error
* @type {AudioPlayerError}
*/
this.emit('error', new AudioPlayerError(error, this.state.resource));
}

View File

@@ -9,7 +9,6 @@ export {
AudioPlayerPausedState,
AudioPlayerPlayingState,
CreateAudioPlayerOptions,
AudioPlayerEvents,
} from './AudioPlayer';
export { AudioPlayerError } from './AudioPlayerError';

View File

@@ -16,7 +16,6 @@ export {
VoiceConnectionDisconnectReason,
VoiceConnectionReadyState,
VoiceConnectionSignallingState,
VoiceConnectionEvents,
} from './VoiceConnection';
export { JoinConfig, getVoiceConnection, getVoiceConnections, getGroups } from './DataStore';

View File

@@ -1,10 +1,11 @@
/* eslint-disable @typescript-eslint/method-signature-style */
import { EventEmitter } from 'node:events';
import { VoiceOpcodes } from 'discord-api-types/voice/v4';
import { TypedEmitter } from 'tiny-typed-emitter';
import type { CloseEvent } from 'ws';
import { VoiceUDPSocket } from './VoiceUDPSocket';
import { VoiceWebSocket } from './VoiceWebSocket';
import * as secretbox from '../util/Secretbox';
import { Awaited, noop } from '../util/util';
import { noop } from '../util/util';
// The number of audio channels required by Discord
const CHANNELS = 2;
@@ -150,11 +151,16 @@ export interface ConnectionData {
*/
const nonce = Buffer.alloc(24);
export interface NetworkingEvents {
debug: (message: string) => Awaited<void>;
error: (error: Error) => Awaited<void>;
stateChange: (oldState: NetworkingState, newState: NetworkingState) => Awaited<void>;
close: (code: number) => Awaited<void>;
export interface Networking extends EventEmitter {
/**
* Debug event for Networking.
*
* @event
*/
on(event: 'debug', listener: (message: string) => void): this;
on(event: 'error', listener: (error: Error) => void): this;
on(event: 'stateChange', listener: (oldState: NetworkingState, newState: NetworkingState) => void): this;
on(event: 'close', listener: (code: number) => void): this;
}
/**
@@ -195,7 +201,7 @@ function randomNBit(n: number) {
/**
* Manages the networking required to maintain a voice connection and dispatch audio packets
*/
export class Networking extends TypedEmitter<NetworkingEvents> {
export class Networking extends EventEmitter {
private _state: NetworkingState;
/**
@@ -274,12 +280,6 @@ export class Networking extends TypedEmitter<NetworkingEvents> {
this._state = newState;
this.emit('stateChange', oldState, newState);
/**
* Debug event for Networking.
*
* @event Networking#debug
* @type {string}
*/
this.debug?.(`state change:\nfrom ${stringifyState(oldState)}\nto ${stringifyState(newState)}`);
}

View File

@@ -1,7 +1,6 @@
import { createSocket, Socket } from 'node:dgram';
import { EventEmitter } from 'node:events';
import { isIPv4 } from 'node:net';
import { TypedEmitter } from 'tiny-typed-emitter';
import type { Awaited } from '../util/util';
/**
* Stores an IP address and port. Used to store socket details for the local client as well as
@@ -17,13 +16,6 @@ interface KeepAlive {
timestamp: number;
}
export interface VoiceUDPSocketEvents {
error: (error: Error) => Awaited<void>;
close: () => Awaited<void>;
debug: (message: string) => Awaited<void>;
message: (message: Buffer) => Awaited<void>;
}
/**
* Parses the response from Discord to aid with local IP discovery.
*
@@ -61,7 +53,7 @@ const MAX_COUNTER_VALUE = 2 ** 32 - 1;
/**
* Manages the UDP networking for a voice connection.
*/
export class VoiceUDPSocket extends TypedEmitter<VoiceUDPSocketEvents> {
export class VoiceUDPSocket extends EventEmitter {
/**
* The underlying network Socket for the VoiceUDPSocket.
*/

View File

@@ -1,28 +1,31 @@
/* eslint-disable @typescript-eslint/method-signature-style */
import { EventEmitter } from 'node:events';
import { VoiceOpcodes } from 'discord-api-types/voice/v4';
import { TypedEmitter } from 'tiny-typed-emitter';
import WebSocket, { MessageEvent } from 'ws';
import type { Awaited } from '../util/util';
/**
* Debug event for VoiceWebSocket.
*
* @event VoiceWebSocket#debug
* @type {string}
*/
export interface VoiceWebSocketEvents {
error: (error: Error) => Awaited<void>;
open: (event: WebSocket.Event) => Awaited<void>;
close: (event: WebSocket.CloseEvent) => Awaited<void>;
debug: (message: string) => Awaited<void>;
packet: (packet: any) => Awaited<void>;
export interface VoiceWebSocket extends EventEmitter {
on(event: 'error', listener: (error: Error) => void): this;
on(event: 'open', listener: (event: WebSocket.Event) => void): this;
on(event: 'close', listener: (event: WebSocket.CloseEvent) => void): this;
/**
* Debug event for VoiceWebSocket.
*
* @event
*/
on(event: 'debug', listener: (message: string) => void): this;
/**
* Packet event.
*
* @event
*/
on(event: 'packet', listener: (packet: any) => void): this;
}
/**
* An extension of the WebSocket class to provide helper functionality when interacting
* with the Discord Voice gateway.
*/
export class VoiceWebSocket extends TypedEmitter<VoiceWebSocketEvents> {
export class VoiceWebSocket extends EventEmitter {
/**
* The current heartbeat interval, if any.
*/
@@ -122,12 +125,6 @@ export class VoiceWebSocket extends TypedEmitter<VoiceWebSocketEvents> {
this.ping = this.lastHeartbeatAck - this.lastHeartbeatSend;
}
/**
* Packet event.
*
* @event VoiceWebSocket#packet
* @type {any}
*/
this.emit('packet', packet);
}

View File

@@ -1,5 +1,4 @@
import { TypedEmitter } from 'tiny-typed-emitter';
import type { Awaited } from '../util/util';
import { EventEmitter } from 'node:events';
/**
* The known data for a user in a Discord voice connection.
@@ -22,19 +21,10 @@ export interface VoiceUserData {
userId: string;
}
/**
* The events that an SSRCMap may emit.
*/
export interface SSRCMapEvents {
create: (newData: VoiceUserData) => Awaited<void>;
update: (oldData: VoiceUserData | undefined, newData: VoiceUserData) => Awaited<void>;
delete: (deletedData: VoiceUserData) => Awaited<void>;
}
/**
* Maps audio SSRCs to data of users in voice connections.
*/
export class SSRCMap extends TypedEmitter<SSRCMapEvents> {
export class SSRCMap extends EventEmitter {
/**
* The underlying map.
*/

View File

@@ -1,25 +1,9 @@
import { TypedEmitter } from 'tiny-typed-emitter';
import type { Awaited } from '../util/util';
/**
* The events that a SpeakingMap can emit.
*/
export interface SpeakingMapEvents {
/**
* Emitted when a user starts speaking.
*/
start: (userId: string) => Awaited<void>;
/**
* Emitted when a user stops speaking.
*/
end: (userId: string) => Awaited<void>;
}
import { EventEmitter } from 'node:events';
/**
* Tracks the speaking states of users in a voice channel.
*/
export class SpeakingMap extends TypedEmitter<SpeakingMapEvents> {
export class SpeakingMap extends EventEmitter {
/**
* The delay after a packet is received from a user until they're marked as not speaking anymore.
*/

View File

@@ -1,4 +1,2 @@
// eslint-disable-next-line @typescript-eslint/no-empty-function
export const noop = () => {};
export type Awaited<T> = T | Promise<T>;