@@ -6,8 +6,9 @@ export class EventEmitter {
66
77 /**
88 * Subscribe to the event.
9- * @param name Event name.
10- * @param fn Callback.
9+ * @param {string } name Event name.
10+ * @param {(...args: any[]) => void } fn Callback.
11+ * @returns {void }
1112 */
1213 on ( name , fn ) {
1314 const callbacks = this . #events. get ( name ) ;
@@ -17,8 +18,9 @@ export class EventEmitter {
1718
1819 /**
1920 * Emit event.
20- * @param name Event name.
21- * @param args Arguments.
21+ * @param {string } name Event name.
22+ * @param {...any } args – Arguments to pass to each listener.
23+ * @returns {void }
2224 */
2325 emit ( name , ...args ) {
2426 const callbacks = this . #events. get ( name ) ;
@@ -31,8 +33,9 @@ export class EventEmitter {
3133 /**
3234 * Remove a specific callback from event
3335 * or all event subscriptions.
34- * @param name Event name.
35- * @param fn Callback.
36+ * @param {string } name Event name.
37+ * @param {(...args: any[]) => void } fn Callback.
38+ * @returns {void }
3639 */
3740 off ( name , fn ) {
3841 const callbacks = this . #events. get ( name ) ;
@@ -47,6 +50,20 @@ export class EventEmitter {
4750 }
4851 }
4952
53+ /**
54+ * Subscribe to an event that will be called only once.
55+ * @param {string } name Event name.
56+ * @param {(...args: any[]) => void } fn Callback.
57+ * @returns {void }
58+ */
59+ once ( name , fn ) {
60+ const wrapper = ( ...args ) => {
61+ this . off ( name , wrapper ) ;
62+ fn . apply ( this , args ) ;
63+ } ;
64+ this . on ( name , wrapper )
65+ }
66+
5067 /**
5168 * Remove all registered events and subscriptions.
5269 */
0 commit comments