Skip to content

Commit 3bdbd90

Browse files
authored
feat(Plugins)!: improve event system (#32)
1 parent f211709 commit 3bdbd90

6 files changed

Lines changed: 44 additions & 25 deletions

File tree

‎src/Main/Player.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,31 @@ import { isString } from "../Functions";
55
import { NodeManager } from "../Node";
66
import { VoiceManager } from "../Voice";
77
import { Playlist, Queue, QueueManager, Track } from "../Queue";
8+
import { PlayerPlugin } from "./index";
89
import type {
910
CreateQueueOptions,
1011
PlayerEventMap,
1112
PlayerOptions,
1213
PlayOptions,
13-
PlayerPlugin,
1414
PluginRecord,
1515
RepeatMode,
1616
SearchOptions,
1717
SearchResult,
1818
CreateNodeOptions,
1919
PlayerInstanceOptions,
2020
QueueContext,
21+
PluginEventMap,
22+
MergeUnionType,
2123
} from "../Typings";
2224

25+
type ConstrainEventMap<T> = {
26+
[K in keyof T]: T[K] extends any[] ? T[K] : never;
27+
};
28+
2329
export class Player<
2430
Context extends Record<string, unknown> = QueueContext,
25-
Plugins extends PlayerPlugin[] = PlayerPlugin[],
26-
> extends EventEmitter<Plugins[number]["eventMap"] & PlayerEventMap> {
31+
Plugins extends PlayerPlugin[] = [],
32+
> extends EventEmitter<ConstrainEventMap<PlayerEventMap & MergeUnionType<PluginEventMap<Plugins[number]>>>> {
2733
#initialized = false;
2834
#initPromise: Promise<void> | null = null;
2935

@@ -52,7 +58,10 @@ export class Player<
5258
this.plugins = {} as PluginRecord<Plugins>;
5359

5460
if (_options.plugins !== undefined) {
55-
for (const plugin of _options.plugins) (this.plugins as { [x: string]: PlayerPlugin })[plugin.name] = plugin;
61+
for (const plugin of _options.plugins) {
62+
if (!(plugin instanceof PlayerPlugin)) throw new Error("Invalid plugin(s)");
63+
(this.plugins as { [x: string]: PlayerPlugin })[plugin.name] = plugin;
64+
}
5665
delete _options.plugins;
5766
}
5867

‎src/Main/Plugin.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Player } from "./index";
2+
3+
export abstract class PlayerPlugin<EventMap extends Record<string, unknown[]> = {}> {
4+
declare protected _: EventMap;
5+
abstract readonly name: string;
6+
abstract init(player: Player): void;
7+
}

‎src/Main/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from "./Plugin";
12
export * from "./Player";

‎src/Plugins/LavaLyrics.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { HttpStatusCode } from "axios";
22
import { OPType } from "../Typings";
33
import { Routes } from "../Constants";
44
import { isString } from "../Functions";
5-
import type { BaseEventPayload, CommonPluginInfo, JsonObject, PlayerPlugin } from "../Typings";
5+
import { PlayerPlugin } from "../Main";
6+
import type { BaseEventPayload, CommonPluginInfo, JsonObject } from "../Typings";
67
import type { Node } from "../Node";
78
import type { Queue } from "../Queue";
89
import type { Player } from "../Main";
@@ -47,24 +48,22 @@ export namespace LavaLyrics {
4748

4849
export type Event = LyricsFoundEvent | LyricsNotFoundEvent | LyricsLineEvent;
4950

50-
export type EventMap = {
51-
lyricsFound: [queue: Queue, lyrics: Lyrics];
52-
lyricsNotFound: [queue: Queue];
53-
lyricsLine: [queue: Queue, line: LyricsLine, index: number, skipped: boolean];
54-
};
55-
5651
export interface PluginOptions {
5752
skipTrackSource?: boolean;
5853
}
5954

60-
export class Plugin implements PlayerPlugin {
61-
name = "lavalyrics" as const;
62-
eventMap!: EventMap;
55+
export class Plugin extends PlayerPlugin<{
56+
lyricsFound: [queue: Queue, lyrics: Lyrics];
57+
lyricsNotFound: [queue: Queue];
58+
lyricsLine: [queue: Queue, line: LyricsLine, index: number, skipped: boolean];
59+
}> {
60+
name = "lavalyrics";
6361

6462
#player!: Player;
6563
skipTrackSource: boolean;
6664

6765
constructor(options?: PluginOptions) {
66+
super();
6867
this.skipTrackSource = options?.skipTrackSource === true;
6968
}
7069

‎src/Typings/Main/Player.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import type { DefaultPlayerOptions } from "../../Constants";
66
import type { Node } from "../../Node";
77
import type { VoiceState } from "../../Voice";
88
import type { Playlist, Queue, Track } from "../../Queue";
9-
import type { Player } from "../../Main";
9+
import type { PlayerPlugin } from "../../Main";
1010

1111
type ExcludeLast<T extends any[]> = T extends [...infer Items, any] ? Items : never;
1212

13-
export interface PlayerEventMap extends Record<string & {}, any> {
13+
export interface PlayerEventMap {
1414
init: [];
1515

1616
nodeConnect: [node: Node, ...ExcludeLast<NodeEventMap["connect"]>];
@@ -36,15 +36,6 @@ export interface PlayerEventMap extends Record<string & {}, any> {
3636
trackFinish: [queue: Queue, track: Track, reason: TrackEndReason];
3737
}
3838

39-
/**
40-
* Represents the structure of a plugin
41-
*/
42-
export interface PlayerPlugin {
43-
readonly name: string;
44-
eventMap: Record<string & {}, any[]>;
45-
init(player: Player): void;
46-
}
47-
4839
/**
4940
* Constructs a record type mapping plugins by their names
5041
*/

‎src/Typings/Utility.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { PlayerPlugin } from "../Main";
2+
13
/**
24
* Represents an empty object
35
*/
@@ -30,6 +32,16 @@ export type NonNullableProp<T, P extends keyof T> = {
3032
[K in keyof T]: K extends P ? NonNullable<T[K]> : T[K];
3133
};
3234

35+
/**
36+
* Merges a union type into one
37+
*/
38+
export type MergeUnionType<U> = (U extends any ? (i: U) => void : never) extends (i: infer I) => void ? I : never;
39+
40+
/**
41+
* Extracts the event map of a plugin
42+
*/
43+
export type PluginEventMap<Plugin> = Plugin extends PlayerPlugin<infer EventMap> ? EventMap : {};
44+
3345
/**
3446
* Queue context, extend via module declaration
3547
*/

0 commit comments

Comments
 (0)