@@ -43,12 +43,24 @@ import { Events } from '../types/Events';
43
43
* ```
44
44
*/
45
45
export abstract class Listener < E extends keyof ClientEvents | symbol = '' > extends Piece {
46
+ /**
47
+ * The emitter, if any.
48
+ * @since 2.0.0
49
+ */
46
50
public readonly emitter : EventEmitter | null ;
51
+
52
+ /**
53
+ * The name of the event the listener listens to.
54
+ * @since 2.0.0
55
+ */
47
56
public readonly event : string ;
48
- public readonly once : boolean ;
49
57
50
- // eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
51
- #listener: ( ( ...args : any [ ] ) => void ) | null ;
58
+ /**
59
+ * Whether or not the listener will be unloaded after the first run.
60
+ * @since 2.0.0
61
+ */
62
+ public readonly once : boolean ;
63
+ private _listener : ( ( ...args : any [ ] ) => void ) | null ;
52
64
53
65
public constructor ( context : PieceContext , options : EventOptions = { } ) {
54
66
super ( context , options ) ;
@@ -61,22 +73,46 @@ export abstract class Listener<E extends keyof ClientEvents | symbol = ''> exten
61
73
this . event = options . event ?? this . name ;
62
74
this . once = options . once ?? false ;
63
75
64
- this . #listener = this . emitter && this . event ? ( this . once ? this . _runOnce . bind ( this ) : this . _run . bind ( this ) ) : null ;
76
+ this . _listener = this . emitter && this . event ? ( this . once ? this . _runOnce . bind ( this ) : this . _run . bind ( this ) ) : null ;
77
+
78
+ // If there's no emitter or no listener, disable:
79
+ if ( this . emitter === null || this . _listener === null ) this . enabled = false ;
65
80
}
66
81
67
82
public abstract run ( ...args : E extends keyof ClientEvents ? ClientEvents [ E ] : unknown [ ] ) : unknown ;
68
83
69
84
public onLoad ( ) {
70
- if ( this . #listener) this . emitter ! [ this . once ? 'once' : 'on' ] ( this . event , this . #listener) ;
85
+ if ( this . _listener ) {
86
+ const emitter = this . emitter ! ;
87
+
88
+ // Increment the maximum amount of listeners by one:
89
+ const maxListeners = emitter . getMaxListeners ( ) ;
90
+ if ( maxListeners !== 0 ) emitter . setMaxListeners ( maxListeners + 1 ) ;
91
+
92
+ emitter [ this . once ? 'once' : 'on' ] ( this . event , this . _listener ) ;
93
+ }
94
+ return super . onLoad ( ) ;
71
95
}
72
96
73
97
public onUnload ( ) {
74
- if ( ! this . once && this . #listener) this . emitter ! . off ( this . event , this . #listener) ;
98
+ if ( ! this . once && this . _listener ) {
99
+ const emitter = this . emitter ! ;
100
+
101
+ // Increment the maximum amount of listeners by one:
102
+ const maxListeners = emitter . getMaxListeners ( ) ;
103
+ if ( maxListeners !== 0 ) emitter . setMaxListeners ( maxListeners - 1 ) ;
104
+
105
+ emitter . off ( this . event , this . _listener ) ;
106
+ this . _listener = null ;
107
+ }
108
+
109
+ return super . onUnload ( ) ;
75
110
}
76
111
77
112
public toJSON ( ) : Record < PropertyKey , unknown > {
78
113
return {
79
114
...super . toJSON ( ) ,
115
+ once : this . once ,
80
116
event : this . event
81
117
} ;
82
118
}
@@ -91,7 +127,7 @@ export abstract class Listener<E extends keyof ClientEvents | symbol = ''> exten
91
127
92
128
private async _runOnce ( ...args : unknown [ ] ) {
93
129
await this . _run ( ...args ) ;
94
- await this . store . unload ( this ) ;
130
+ await this . unload ( ) ;
95
131
}
96
132
}
97
133
0 commit comments