This repository has been archived by the owner on Nov 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
v3: fix create generics #118
Open
viztea
wants to merge
2
commits into
MenuDocs:v3
Choose a base branch
from
viztea:v3-melike2d
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +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< | ||
MO extends ManagerOptions = ManagerOptions, | ||
PO extends PlayerOptions = PlayerOptions, | ||
P extends Player<PO> = Player<PO> | ||
> extends EventEmitter { | ||
/** | ||
* The options provided to this manager. | ||
*/ | ||
readonly options: MO; | ||
export interface Manager<O extends ManagerOptions, P extends Player<any, any>> extends EventEmitter { | ||
/** | ||
* The options provided to this manager. | ||
*/ | ||
readonly options: O; | ||
|
||
/** | ||
* All the players that were created by this manager. | ||
*/ | ||
readonly players: Collection<string, P>; | ||
/** | ||
* All the players that were created by this manager. | ||
*/ | ||
readonly players: Collection<string, P>; | ||
|
||
/** | ||
* 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>): 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> = P extends Player<Manager<any, any>, infer O> ? O : never; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ManagerOptions, PlayerOptions, Player> | ||
>(options: ExtractOptions<M>, klass?: Class<M>): Promise<M> | ||
|
||
export async function create< | ||
M extends Manager<O, PO, P>, | ||
O extends ManagerOptions = ManagerOptions, | ||
PO extends PlayerOptions = PlayerOptions, | ||
P extends Player<PO> = Player<PO> | ||
>(options?: O, klass?: Class<M>): Promise<M> { | ||
* 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<M extends Manager<any, any>>(options: ExtractOptions<M>, klass?: Class<M>): M | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
|
||
export function create<M extends Manager<any, any>>(options?: ExtractOptions<M>, klass?: Class<M>): M { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
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> = P extends Manager<infer O, Player> ? O : never; | ||
type ExtractOptions<P> = P extends Manager<infer O, Player<any, any>> ? O : never; | ||
|
||
type Class<T> = { | ||
new(...args: any): T; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
export * from "./erela"; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest
target
is better word here thanklass