//#region src/index.d.ts type DefaultEventMap = { [eventType: string]: TypedEvent; }; /** * Reserved event map containing special event types like '*' for catch-all listeners. */ type ReservedEventMap = { '*': TypedEvent; }; type IsReservedEvent = Type extends keyof ReservedEventMap ? true : false; interface TypedEvent extends Omit, 'type'> { type: EventType; } declare const kDefaultPrevented: unique symbol; declare const kPropagationStopped: unique symbol; declare const kImmediatePropagationStopped: unique symbol; declare class TypedEvent extends MessageEvent implements TypedEvent { #private; [kDefaultPrevented]: boolean; [kPropagationStopped]?: Emitter; [kImmediatePropagationStopped]?: boolean; constructor(...args: [DataType] extends [void] ? [type: EventType] : [type: EventType, init: { data: DataType; }]); get defaultPrevented(): boolean; preventDefault(): void; stopImmediatePropagation(): void; } /** * Brands a TypedEvent or its subclass while preserving its (narrower) type. */ type Brand = Loose extends true ? Event extends TypedEvent ? /** * @note Omit the `ReturnType` so emit methods can accept type events * where infering the return type is impossible. */ TypedEvent & { type: EventType; } : never : Event & { type: EventType; }; type InferEventMap> = Target extends Emitter ? MergedEventMap : never; /** * Extracts only user-defined events, excluding reserved event types. */ type UserEventMap = Omit; /** * Merges the user EventMap with the ReservedEventMap. * The '*' event type accepts a union of all user-defined events. */ type MergedEventMap = EventMap$1 & ReservedEventMap; /** * Creates a union of all events in the EventMap with their literal type strings. */ type AllEvents = { [K in keyof EventMap$1 & string]: Brand }[keyof EventMap$1 & string]; type TypedListenerOptions = { once?: boolean; signal?: AbortSignal; }; declare namespace Emitter { /** * Returns an appropriate `Event` type for the given event type. * * @example * const emitter = new Emitter<{ greeting: TypedEvent }>() * type GreetingEvent = Emitter.InferEventType * // TypedEvent */ type EventType, EventType extends keyof EventMap$1 & string, EventMap$1 extends DefaultEventMap = InferEventMap> = IsReservedEvent extends true ? AllEvents> : Brand; type EventDataType, EventType extends keyof EventMap$1 & string, EventMap$1 extends DefaultEventMap = InferEventMap> = EventMap$1[EventType] extends TypedEvent ? DataType : never; /** * Returns the listener type for the given event type. * * @example * const emitter = new Emitter<{ getTotalPrice: TypedEvent }>() * type Listener = Emitter.ListenerType * // (event: TypedEvent) => number */ type ListenerType, EventType extends keyof EventMap$1 & string, EventMap$1 extends DefaultEventMap = InferEventMap> = IsReservedEvent extends true ? (event: AllEvents>) => void : (event: Emitter.EventType) => Emitter.ListenerReturnType extends [void] ? void : Emitter.ListenerReturnType; /** * Returns the return type of the listener for the given event type. * * @example * const emitter = new Emitter<{ getTotalPrice: TypedEvent }>() * type ListenerReturnType = Emitter.InferListenerReturnType * // number */ type ListenerReturnType, EventType extends keyof EventMap$1 & string, EventMap$1 extends DefaultEventMap = InferEventMap> = IsReservedEvent extends true ? void : EventMap$1[EventType] extends TypedEvent ? ReturnType : never; } declare class Emitter { #private; constructor(); /** * Adds a listener for the given event type. */ on & string>(type: EventType, listener: Emitter.ListenerType>, options?: TypedListenerOptions): typeof this; /** * Adds a one-time listener for the given event type. */ once & string>(type: EventType, listener: Emitter.ListenerType>, options?: Omit): typeof this; /** * Prepends a listener for the given event type. */ earlyOn & string>(type: EventType, listener: Emitter.ListenerType>, options?: TypedListenerOptions): typeof this; /** * Prepends a one-time listener for the given event type. */ earlyOnce & string>(type: EventType, listener: Emitter.ListenerType>, options?: Omit): typeof this; /** * Emits the given typed event. * * @returns {boolean} Returns `true` if the event had any listeners, `false` otherwise. */ emit(event: Brand): boolean; /** * Emits the given typed event and returns a promise that resolves * when all the listeners for that event have settled. * * @returns {Promise>} A promise that resolves * with the return values of all listeners. */ emitAsPromise(event: Brand): Promise>>; /** * Emits the given event and returns a generator that yields * the result of each listener in the order of their registration. * This way, you stop exhausting the listeners once you get the expected value. */ emitAsGenerator(event: Brand): Generator>; /** * Removes a listener for the given event type. */ removeListener & string>(type: EventType, listener: Emitter.ListenerType>): void; /** * Removes all listeners for the given event type. * If no event type is provided, removes all existing listeners. */ removeAllListeners & string>(type?: EventType): void; /** * Returns the list of listeners for the given event type. * If no even type is provided, returns all listeners. */ listeners & string>(type?: EventType): Array>>; /** * Returns the number of listeners for the given event type. * If no even type is provided, returns the total number of listeners. */ listenerCount & string>(type?: EventType): number; } //#endregion export { DefaultEventMap, Emitter, ReservedEventMap, TypedEvent, TypedListenerOptions }; //# sourceMappingURL=index.d.mts.map