import { EventEmitter } from "eventemitter3"; import logger from "./logger"; export interface EventsWithError { error: (error: PeerError<`${ErrorType}`>) => void; } export class EventEmitterWithError< ErrorType extends string, Events extends EventsWithError, > extends EventEmitter { /** * Emits a typed error message. * * @internal */ emitError(type: ErrorType, err: string | Error): void { logger.error("Error:", err); // @ts-ignore this.emit("error", new PeerError<`${ErrorType}`>(`${type}`, err)); } } /** * A PeerError is emitted whenever an error occurs. * It always has a `.type`, which can be used to identify the error. */ export class PeerError extends Error { /** * @internal */ constructor(type: T, err: Error | string) { if (typeof err === "string") { super(err); } else { super(); Object.assign(this, err); } this.type = type; } public type: T; }