@@ -68,33 +68,6 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
6868 this . _subscribers = new Map ( ) ;
6969 }
7070
71- /**
72- * Unsubscribes all the subscribers from all the events.
73- *
74- * ---
75- *
76- * @example
77- * ```ts
78- * publisher.subscribe("player:spawn", (evt) => { [...] });
79- * publisher.subscribe("player:move", (coords) => { [...] });
80- * publisher.subscribe("player:move", () => { [...] });
81- * publisher.subscribe("player:move", ({ x, y }) => { [...] });
82- * publisher.subscribe("player:death", () => { [...] });
83- *
84- * // All these subscribers are working fine...
85- *
86- * publisher.clear();
87- *
88- * // ... but now they're all gone!
89- * ```
90- */
91- public clear ( ) : void
92- {
93- this . publish ( "__internals__:clear" ) ;
94-
95- this . _subscribers . clear ( ) ;
96- }
97-
9871 /**
9972 * Creates a new scoped instance of the {@link Publisher} class,
10073 * which can be used to publish and subscribe events within a specific context.
@@ -110,8 +83,8 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
11083 * const publisher = new Publisher();
11184 * const context = publisher.createScope();
11285 *
113- * publisher.subscribe("player:death", () => console.log(` Player has died.` ));
114- * context.subscribe("player:spawn", () => console.log(` Player has spawned.` ));
86+ * publisher.subscribe("player:death", () => console.log(" Player has died." ));
87+ * context.subscribe("player:spawn", () => console.log(" Player has spawned." ));
11588 *
11689 * publisher.publish("player:spawn"); // "Player has spawned."
11790 * context.publish("player:death"); // * no output *
@@ -239,7 +212,7 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
239212 *
240213 * @returns A function that can be used to unsubscribe the subscriber from the event.
241214 */
242- public subscribe < K extends keyof T > ( event : K & string , subscriber : T [ K ] ) : ( ) => void ;
215+ public subscribe < K extends keyof T > ( event : K & string , subscriber : T [ K ] ) : Callback ;
243216
244217 /**
245218 * Subscribes to the wildcard event to listen to all published events.
@@ -253,21 +226,21 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
253226 * ```ts
254227 * publisher.subscribe("*", (type, ...args) =>
255228 * {
256- * console.log(`Event \` ${type}\` was fired with args:`, args);
229+ * console.log(`Event " ${type}" was fired with args:`, args);
257230 * });
258231 * ```
259232 *
260233 * ---
261234 *
262- * @template K The key of the wildcard events map (always "*" ).
235+ * @template K The key of the wildcard events map (always `*` ).
263236 *
264- * @param event The wildcard event name ("*" ).
237+ * @param event The wildcard event name (`*` ).
265238 * @param subscriber The subscriber to execute for all published events.
266239 *
267240 * @returns A function that can be used to unsubscribe the subscriber from the wildcard event.
268241 */
269- public subscribe < K extends keyof S > ( event : K & string , subscriber : S [ K ] ) : ( ) => void ;
270- public subscribe ( event : string , subscriber : Callback < unknown [ ] , unknown > ) : ( ) => void
242+ public subscribe < K extends keyof S > ( event : K & string , subscriber : S [ K ] ) : Callback ;
243+ public subscribe ( event : string , subscriber : Callback < unknown [ ] , unknown > ) : Callback
271244 {
272245 const subscribers = this . _subscribers . get ( event ) ?? [ ] ;
273246 subscribers . push ( subscriber ) ;
@@ -326,9 +299,9 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
326299 *
327300 * ---
328301 *
329- * @template K The key of the wildcard events map (always "*" ).
302+ * @template K The key of the wildcard events map (always `*` ).
330303 *
331- * @param event The wildcard event name ("*" ).
304+ * @param event The wildcard event name (`*` ).
332305 * @param subscriber The wildcard subscriber to remove.
333306 */
334307 public unsubscribe < K extends keyof S > ( event : K & string , subscriber : S [ K ] ) : void ;
@@ -352,5 +325,94 @@ export default class Publisher<T extends CallbackMap<T> = CallbackMap>
352325 if ( subscribers . length === 0 ) { this . _subscribers . delete ( event ) ; }
353326 }
354327
328+ /**
329+ * Unsubscribes all subscribers from a specific event and removes
330+ * them from being executed when the event is published.
331+ *
332+ * ---
333+ *
334+ * @example
335+ * ```ts
336+ * publisher.subscribe("player:spawn", (evt) => { [...] });
337+ * publisher.subscribe("player:move", (coords) => { [...] });
338+ * publisher.subscribe("player:move", () => { [...] });
339+ * publisher.subscribe("player:move", ({ x, y }) => { [...] });
340+ * publisher.subscribe("player:death", () => { [...] });
341+ *
342+ * // All these subscribers are working fine...
343+ *
344+ * publisher.unsubscribeAll("player:move");
345+ *
346+ * // ... but now "player:move" subscribers are gone!
347+ * ```
348+ *
349+ * ---
350+ *
351+ * @template K The key of the map containing the event to clear.
352+ *
353+ * @param event The name of the event to unsubscribe all subscribers from.
354+ */
355+ public unsubscribeAll < K extends keyof T > ( event : K & string ) : void ;
356+
357+ /**
358+ * Unsubscribes all subscribers from the wildcard event and removes
359+ * them from being executed for all published events.
360+ *
361+ * ---
362+ *
363+ * @example
364+ * ```ts
365+ * publisher.subscribe("player:spawn", (evt) => { [...] });
366+ * publisher.subscribe("*", (type, ...args) => { [...] });
367+ * publisher.subscribe("*", (type, arg1, arg2, arg3) => { [...] });
368+ * publisher.subscribe("*", (_, arg, ...rest) => { [...] });
369+ * publisher.subscribe("player:death", () => { [...] });
370+ *
371+ * // All these subscribers are working fine...
372+ *
373+ * publisher.unsubscribeAll("*");
374+ *
375+ * // ... but now wildcard subscribers are gone!
376+ * ```
377+ *
378+ * ---
379+ *
380+ * @template K The key of the wildcard events map (`*`).
381+ *
382+ * @param event The wildcard event name (`*`).
383+ */
384+ public unsubscribeAll < K extends keyof S > ( event : K & string ) : void ;
385+ public unsubscribeAll ( event : string ) : void
386+ {
387+ this . _subscribers . delete ( event ) ;
388+ }
389+
390+ /**
391+ * Unsubscribes all the subscribers from all the events.
392+ *
393+ * ---
394+ *
395+ * @example
396+ * ```ts
397+ * publisher.subscribe("player:spawn", (evt) => { [...] });
398+ * publisher.subscribe("player:move", (coords) => { [...] });
399+ * publisher.subscribe("*", () => { [...] });
400+ * publisher.subscribe("player:move", ({ x, y }) => { [...] });
401+ * publisher.subscribe("player:death", () => { [...] });
402+ *
403+ * // All these subscribers are working fine...
404+ *
405+ * publisher.clear();
406+ *
407+ * // ... but now they're all gone!
408+ * ```
409+ */
410+ public clear ( ) : void
411+ {
412+ this . publish ( "__internals__:clear" ) ;
413+
414+ this . _subscribers . clear ( ) ;
415+ }
416+
355417 public readonly [ Symbol . toStringTag ] : string = "Publisher" ;
356418}
0 commit comments