From 44d4629287312b43f936772c3edab4c66cd04511 Mon Sep 17 00:00:00 2001 From: 2D Date: Fri, 18 Jun 2021 00:25:15 -0700 Subject: [PATCH 1/2] Update Manager.ts --- packages/erela.js-api/src/api/Manager.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/erela.js-api/src/api/Manager.ts b/packages/erela.js-api/src/api/Manager.ts index ef7fcf5..30b4b1e 100644 --- a/packages/erela.js-api/src/api/Manager.ts +++ b/packages/erela.js-api/src/api/Manager.ts @@ -15,15 +15,11 @@ export interface ManagerOptions { /** * The base Manager */ -export interface Manager< - MO extends ManagerOptions = ManagerOptions, - PO extends PlayerOptions = PlayerOptions, - P extends Player = Player - > extends EventEmitter { +export interface Manager extends EventEmitter { /** * The options provided to this manager. */ - readonly options: MO; + readonly options: O; /** * All the players that were created by this manager. From 32b8d52414f05c61be2c017defefb85e8f1d19ed Mon Sep 17 00:00:00 2001 From: 2D Date: Fri, 18 Jun 2021 00:47:20 -0700 Subject: [PATCH 2/2] Fix generics --- packages/erela.js-api/src/api/Manager.ts | 93 ++++++++++++------------ packages/erela.js-api/src/api/Player.ts | 29 ++++---- packages/erela.js-api/src/erela.ts | 41 +++++------ packages/erela.js-api/src/index.ts | 8 +- 4 files changed, 83 insertions(+), 88 deletions(-) diff --git a/packages/erela.js-api/src/api/Manager.ts b/packages/erela.js-api/src/api/Manager.ts index 30b4b1e..94d7f3b 100644 --- a/packages/erela.js-api/src/api/Manager.ts +++ b/packages/erela.js-api/src/api/Manager.ts @@ -1,65 +1,68 @@ import Collection from "@discordjs/collection"; import EventEmitter from "events"; -import type { Player, PlayerOptions } from "./Player"; +import type { Player } from "./Player"; /** - * The base options to provide for a audio provider. + * The base options to provide for a audio provider. */ export interface ManagerOptions { - /** - * A list of plugins to use with the audio provider, these may be specific for an audio provider. - */ - readonly plugins?: Plugin[] + /** + * A list of plugins to use with the audio provider, these may be specific for an audio provider. + */ + readonly plugins?: Plugin[] } /** * The base Manager */ -export interface Manager extends EventEmitter { - /** - * The options provided to this manager. - */ - readonly options: O; +export interface Manager> extends EventEmitter { + /** + * The options provided to this manager. + */ + readonly options: O; - /** - * All the players that were created by this manager. - */ - readonly players: Collection; + /** + * All the players that were created by this manager. + */ + readonly players: Collection; - /** - * Adds a plugin - * @param plugin Plugin - */ - use(plugin: Plugin): void; + /** + * Adds a plugin + * @param plugin Plugin + */ + use(plugin: Plugin): void; - /** - * Adds an array of plugins - * @param plugin Plugin - */ - use(plugins: Plugin[]): void; + /** + * Adds an array of plugins + * @param plugins The plugins to use in this manager + */ + use(plugins: Plugin[]): void; - /** - * Creates a Player with the provided Guild ID, `textChannel` and `voiceChannel` must be bound later. - */ - create(guild: string): P; + /** + * Creates a Player with the provided Guild ID, `textChannel` and `voiceChannel` must be bound later. + */ + create(guild: string): P; - // /** - // * Creates a Player with the provided Guild and Voice channel ID, `textChannel` must be bound later. - // */ - // create(guild: string, channel: string): P; + // /** + // * Creates a Player with the provided Guild and Voice channel ID, `textChannel` must be bound later. + // */ + // create(guild: string, channel: string): P; - /** - * Creates a Player with the provided player options. - */ - create(options: PlayerOptions): P; + /** + * Creates a Player with the provided player options. + */ + create(options: ExtractPlayerOptions

): P; - /** - * Destroys a Player using the Guild ID. - */ - destroy(guild: string): void; + /** + * Destroys a Player using the Guild ID. + */ + destroy(guild: string): void; - /** - * Destroys a Player using the Player instance. - */ - destroy(player: P): void; + /** + * Destroys a Player using the Player instance. + */ + destroy(player: P): void; } + +type ExtractPlayerOptions

= P extends Player, infer O> ? O : never; + diff --git a/packages/erela.js-api/src/api/Player.ts b/packages/erela.js-api/src/api/Player.ts index 23f5f6a..d478080 100644 --- a/packages/erela.js-api/src/api/Player.ts +++ b/packages/erela.js-api/src/api/Player.ts @@ -1,5 +1,5 @@ import { State } from "../impl/State"; -import { Manager, ManagerOptions } from "./Manager"; +import { Manager } from "./Manager"; /** * The base PlayerOptions. @@ -9,7 +9,7 @@ export interface PlayerOptions { * The guild the Player belongs to. */ readonly guild: string; - + /** * The text channel the Player belongs to. */ @@ -39,19 +39,16 @@ export interface PlayerOptions { /** * The base Player. */ -export interface Player< - PO extends PlayerOptions = PlayerOptions, - MO extends ManagerOptions = ManagerOptions -> { +export interface Player, O extends PlayerOptions> { /** * The options for this Player. */ - readonly options: PO; + readonly options: O; /** * The manager */ - readonly manager: Manager + readonly manager: M; /** * The guild this Player is bound to. @@ -59,12 +56,12 @@ export interface Player< readonly guild: string; /** - * The guild text channel this Player is bound to. + * The guild text channel this Player is bound to. */ textChannel?: string | null; /** - * The guild voice channel this Player is bound to and will join. + * The guild voice channel this Player is bound to and will join. */ voiceChannel?: string | null; @@ -72,22 +69,22 @@ export interface Player< * The time position in the track the player is currently at. */ position?: number | null; - + /** * Whether is the player is currently playing a track. */ playing?: boolean | null; - + /** * Whether is the player is currently paused. */ paused?: boolean | null; - + /** * The volume the player is playing at. */ volume?: number | null; - + /** * The state the player is in. */ @@ -98,7 +95,7 @@ export interface Player< * @param channel string */ connect(channel?: string): Promise; - + /** * Disconnects from the voice channel, this will keep the player alive for use later. */ @@ -108,4 +105,4 @@ export interface Player< * Destroys the player and disconnects it from the voice channel. */ destroy(): Promise; -} \ No newline at end of file +} diff --git a/packages/erela.js-api/src/erela.ts b/packages/erela.js-api/src/erela.ts index 7935f5f..351cd82 100644 --- a/packages/erela.js-api/src/erela.ts +++ b/packages/erela.js-api/src/erela.ts @@ -1,34 +1,27 @@ -import type { Player, PlayerOptions } from "./api/Player"; -import type { Manager, ManagerOptions } from "./api/Manager"; +import type { Player } from "./api/Player"; +import type { Manager } from "./api/Manager"; import { Detector } from "./impl/detection/Detector"; export namespace Erela { const plugins: Plugin[] = []; /** - * Adds a plugin that will be used when a Manager is created. - * @param plugin Plugin - */ + * Adds a plugin that will be used when a Manager is created. + * @param plugin Plugin + */ export function use(plugin: Plugin): void { plugins.push(plugin); } /** - * Creates a new player manager with the supplied options. - * - * @param options The options to supply the player manager. - * @param klass The provider class to instantiate. - */ - export async function create< - M extends Manager - >(options: ExtractOptions, klass?: Class): Promise - - export async function create< - M extends Manager, - O extends ManagerOptions = ManagerOptions, - PO extends PlayerOptions = PlayerOptions, - P extends Player = Player - >(options?: O, klass?: Class): Promise { + * Creates a new player manager with the supplied options. + * + * @param options The options to supply the player manager. + * @param klass The provider class to instantiate. + */ + export function create>(options: ExtractOptions, klass?: Class): M + + export function create>(options?: ExtractOptions, klass?: Class): M { if (!klass) { const providers = Detector.findProviders(); if (!providers.length) { @@ -46,14 +39,16 @@ export namespace Erela { } const manager = new klass!!(options); - if (plugins.length) manager.use(plugins); + if (plugins.length) { + manager.use(plugins); + } return manager; } - type ExtractOptions

= P extends Manager ? O : never; + type ExtractOptions

= P extends Manager> ? O : never; type Class = { new(...args: any): T; } -} \ No newline at end of file +} diff --git a/packages/erela.js-api/src/index.ts b/packages/erela.js-api/src/index.ts index a94fb38..c2ff4b5 100644 --- a/packages/erela.js-api/src/index.ts +++ b/packages/erela.js-api/src/index.ts @@ -1,6 +1,6 @@ -export * from "./api/Manager" -export * from "./api/Player" +export * from "./api/Manager"; +export * from "./api/Player"; -export * from "./impl/State" +export * from "./impl/State"; -export * from "./erela" \ No newline at end of file +export * from "./erela";