Skip to content

Commit

Permalink
refactor: change to strict-event-emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
thetayloredman committed Feb 21, 2023
1 parent c6cc834 commit 9e063f0
Show file tree
Hide file tree
Showing 4 changed files with 844 additions and 1,010 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,8 @@
"prettier --write"
]
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"dependencies": {
"strict-event-emitter": "^0.4.6"
}
}
171 changes: 3 additions & 168 deletions src/lib/SafeEventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,172 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { EventEmitter } from "node:events";
import { Emitter } from "strict-event-emitter";

// FIXME: This file has a whole lot of any. Can someone find a good way to fix this?
/* eslint-disable @typescript-eslint/no-explicit-any */

/**
* Represents a type-safe EventEmitter. This is a wrapper around EventEmitter & should be
* completely type-compatible with it.
* @typeParam Events The types of events that can be emitted
*/
export default class SafeEventEmitter<Events extends Record<string, any[]>> implements EventEmitter {
/** The underlying EventEmitter */
private emitter: EventEmitter;

public constructor() {
this.emitter = new EventEmitter();
}

/**
* Add a new listener. Works like EventEmitter#on().
* @param event The event name to latch onto
* @param listener The listener to call when the event is emitted
* @returns This SafeEventEmitter
*/
public on<Name extends keyof Events & string>(event: Name, listener: (...args: Events[Name]) => void): this {
this.emitter.on(event, listener as any);
return this;
}

/**
* Add a new listener which can only fire once. Works like EventEmitter#once().
* @param event The event name to latch onto
* @param listener The listener to call when the event is emitted
* @returns This SafeEventEmitter
*/
public once<Name extends keyof Events & string>(event: Name, listener: (...args: Events[Name]) => void): this {
this.emitter.once(event, listener as any);
return this;
}

/**
* Remove a listener. Works like EventEmitter#removeListener().
* @param eventName The event name to remove the listener from
* @param listener The listener to remove
* @returns This SafeEventEmitter
*/
public removeListener<Name extends keyof Events & string>(eventName: Name, listener: (...args: Events[Name]) => void): this {
this.emitter.removeListener(eventName, listener as any);
return this;
}

/**
* Remove all listeners. Works like EventEmitter#removeAllListeners().
* @param eventName The event name to remove all listeners from
* @returns This SafeEventEmitter
* @remarks If eventName is not provided, all listeners will be removed.
*/
public removeAllListeners<Name extends keyof Events & string>(eventName?: Name): this {
this.emitter.removeAllListeners(eventName as any);
return this;
}

/**
* Add a new listener. Works like EventEmitter#addListener().
* @param eventName The event name to latch onto
* @param listener The listener to call when the event is emitted
* @returns This SafeEventEmitter
*/
public addListener<Name extends keyof Events & string>(eventName: Name, listener: (...args: Events[Name]) => void): this {
this.emitter.addListener(eventName, listener as any);
return this;
}

/**
* Set the maximum number of listeners before a warning is emitted. Works like EventEmitter#setMaxListeners().
* @param n The new maximum number of listeners
* @returns This SafeEventEmitter
*/
public setMaxListeners(n: number): this {
this.emitter.setMaxListeners(n);
return this;
}

/**
* Get the maximum number of listeners before a warning is emitted. Works like EventEmitter#getMaxListeners().
* @returns The maximum number of listeners
*/
public getMaxListeners(): number {
return this.emitter.getMaxListeners();
}

/**
* Get the listeners for an event. Works like EventEmitter#listeners().
* @param event The event name to get the listeners for
* @returns The listeners for the event
*/
public listeners<Name extends keyof Events & string>(event: Name): Array<(...args: Events[Name]) => void> {
return this.emitter.listeners(event as any) as any;
}

/**
* Get the listeners for an event, including wrappers. Works like EventEmitter#rawListeners().
* @param event The event name to get the listeners for
* @returns The listeners for the event
*/
public rawListeners<Name extends keyof Events & string>(event: Name): Array<(...args: Events[Name]) => void> {
return this.emitter.rawListeners(event as any) as any;
}

/**
* Get the number of listeners for an event. Works like EventEmitter#listenerCount().
* @param event The event name to get the listener count for
* @returns The number of listeners for the event
*/
public listenerCount<Name extends keyof Events & string>(event: Name): number {
return this.emitter.listenerCount(event as any);
}

/**
* Prepend a new listener. Works like EventEmitter#prependListener().
* @param event The event name to latch onto
* @param listener The listener to call when the event is emitted
* @returns This SafeEventEmitter
*/
public prependListener<Name extends keyof Events & string>(event: Name, listener: (...args: Events[Name]) => void): this {
this.emitter.prependListener(event, listener as any);
return this;
}

/**
* Prepend a new listener which can only fire once. Works like EventEmitter#prependOnceListener().
* @param event The event name to latch onto
* @param listener The listener to call when the event is emitted
* @returns This SafeEventEmitter
*/
public prependOnceListener<Name extends keyof Events & string>(event: Name, listener: (...args: Events[Name]) => void): this {
this.emitter.prependOnceListener(event, listener as any);
return this;
}

/**
* Get all listened-to events. Works like EventEmitter#eventNames().
* @returns An array of event names
*/
public eventNames(): (string | symbol)[] {
return this.emitter.eventNames();
}

/**
* Remove a listener. Works like EventEmitter#off().
* @param event The event name to remove the listener from
* @param listener The listener to remove
* @returns This SafeEventEmitter
*/
public off<Name extends keyof Events & string>(event: Name, listener: (...args: Events[Name]) => void): this {
this.emitter.removeListener(event, listener as any);
return this;
}

/**
* Emit an event. Works like EventEmitter#emit().
* @param event The event name to emit
* @param args The arguments to pass to the listeners
* @returns Whether the event had listeners
*/
public emit<Name extends keyof Events & string>(event: Name, ...args: Events[Name]): boolean {
return this.emitter.emit(event, ...args);
}
}
/** @deprecated use strict-event-emitter/Emitter */
export default Emitter;
9 changes: 5 additions & 4 deletions src/stream/providers/ManualIOProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import SafeEventEmitter from "../../lib/SafeEventEmitter";
import { Emitter } from "strict-event-emitter";

import type IOProvider from "./IOProvider";
import type { IOProviderEventArguments } from "./IOProvider";
import { IOProviderEvents } from "./IOProvider";
Expand All @@ -27,17 +28,17 @@ import { IOProviderEvents } from "./IOProvider";
* **You probably do not want to use this.** This should only be used in unit tests for SD2. We will not
* support use-cases that involve this provider.
*/
export default class ManualIOProvider extends SafeEventEmitter<IOProviderEventArguments> implements IOProvider {
export default class ManualIOProvider extends Emitter<IOProviderEventArguments> implements IOProvider {
/** Functions for reading/writing */
public manual: SafeEventEmitter<{ data: [Buffer]; close: [] }> & {
public manual: Emitter<{ data: [Buffer]; close: [] }> & {
write: (data: Buffer) => void;
close: () => void;
error: (error: Error) => void;
};

public constructor() {
super();
this.manual = Object.assign(new SafeEventEmitter(), {
this.manual = Object.assign(new Emitter<{ data: [Buffer]; close: [] }>(), {
write: (data: Buffer): void => {
this.emit(IOProviderEvents.Data, data);
},
Expand Down
Loading

0 comments on commit 9e063f0

Please sign in to comment.