//#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 ? WithReservedEvents : never; /** * Extracts only user-defined events, excluding reserved event types. */ type UserEventMap = Omit; /** * Decorates the given `EventMap` with the reserved emitter events (e.g. `*`). */ type WithReservedEvents = 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; }; interface HookListenerOptions extends TypedListenerOptions { persist?: boolean; } type EmitterHookMap = { newListener: (type: keyof WithReservedEvents & string, listener: { [K in keyof WithReservedEvents & string]: Emitter.Listener, K, WithReservedEvents> }[keyof WithReservedEvents & string], options: HookListenerOptions | undefined) => void; removeListener: (type: keyof WithReservedEvents & string, listener: { [K in keyof WithReservedEvents & string]: Emitter.Listener, K, WithReservedEvents> }[keyof WithReservedEvents & string], options: HookListenerOptions | undefined) => void; beforeEmit: (event: EventMap$1[keyof EventMap$1 & string]) => boolean | void; }; declare namespace Emitter { /** * Returns a union of all event types, both public and reserved, for the given emitter. * * @example * const emitter = new Emitter<{ greeting: TypedEvent, handshake: TypedEvent }>() * type AllEventTypes = Emitter.AllEventTypes * // "*" | "greeting" | "handshake" */ type AllEventTypes, EventMap$1 extends DefaultEventMap = InferEventMap> = string extends keyof EventMap$1 ? never : keyof EventMap$1 & string; /** * Returns a union of all public event types for the given emitter. * * @example * const emitter = new Emitter<{ greeting: TypedEvent, handshake: TypedEvent }>() * type EventTypes = Emitter.EventTypes * // "greeting" | "handshake" */ type PublicEventTypes, EventMap$1 extends DefaultEventMap = InferEventMap, UserEvents extends UserEventMap = UserEventMap> = string extends keyof UserEvents ? never : keyof UserEvents & string; /** * Returns a union of all public event type for the given emitter. * * @example * const emitter = new Emitter<{ greeting: GreetingEvent, handshake: HandshakeEvent }>() * type Events = Emitter.Events * // GreetingEvent | HandshakeEvent */ type Events, EventMap$1 extends DefaultEventMap = InferEventMap> = string extends keyof UserEventMap ? never : UserEventMap[keyof UserEventMap]; /** * Returns an appropriate `Event` type for the given event type. * * @example * const emitter = new Emitter<{ greeting: TypedEvent }>() * type GreetingEvent = Emitter.Event * // TypedEvent */ type Event, EventType extends keyof EventMap$1 & string, EventMap$1 extends DefaultEventMap = InferEventMap> = IsReservedEvent extends true ? AllEvents> : Brand; /** * Returns an appropriate event data type for the given event type. * * @example * const emitter = new Emitter<{ greeting: TypedEvent<'hello'> }>() * type GreetingData = Emitter.EventData * // "hello" */ type EventData, 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 Listener, EventType extends keyof EventMap$1 & string, EventMap$1 extends DefaultEventMap = InferEventMap> = IsReservedEvent extends true ? (event: Emitter.Event) => void : (event: Emitter.Event) => 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.ListenerReturnType * // 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 namespace EventMap { /** * Returns a union of all public event types from the given event map. * * @example * type MyEventMap = { greeting: TypedEvent, handshake: TypedEvent } * type EventTypes = EventMap.EventTypes * // "greeting" | "handshake" */ type EventTypes = Emitter.PublicEventTypes>; /** * Returns a union of all public event type from the given event map. * * @example * type MyEventMap = { greeting: GreetingEvent, handshake: HandshakeEvent } * type Events = EventMap.Events * // GreetingEvent | HandshakeEvent */ type Events = Emitter.Events>; /** * Returns an appropriate `Event` type for the given event type. * * @example * type MyEventMap = { greeting: TypedEvent } * type GreetingEvent = EventMap.Event * // TypedEvent */ type Event & string> = Emitter.Event, Type, WithReservedEvents>; /** * Returns an appropriate event data type for the given event type. * * @example * type MyEventMap = { greeting: TypedEvent<'hello'> } * type GreetingData = EventMap.EventData * // "hello" */ type EventData & string> = Emitter.EventData, Type, WithReservedEvents>; /** * Returns the listener type for the given event type. * * @example * type MyEventMap = { getTotalPrice: TypedEvent }> * type Listener = EventMap.Listener * // (event: TypedEvent) => number */ type Listener & string> = Emitter.Listener, Type, WithReservedEvents>; /** * Returns the return type of the listener for the given event type. * * @example * type MyEventMap = { getTotalPrice: TypedEvent } * type ListenerReturnType = EventMap.ListenerReturnType * // number */ type ListenerReturnType & string> = Emitter.ListenerReturnType, Type, WithReservedEvents>; } declare class Emitter { #private; readonly hooks: { on>(type: HookType, callback: EmitterHookMap[HookType], options?: HookListenerOptions): void; removeListener>(type: HookType, callback: EmitterHookMap[HookType], options?: HookListenerOptions): void; }; constructor(); /** * Adds a listener for the given event type. */ on & string>(type: EventType, listener: Emitter.Listener>, options?: TypedListenerOptions): typeof this; /** * Adds a one-time listener for the given event type. */ once & string>(type: EventType, listener: Emitter.Listener>, options?: Omit): typeof this; /** * Prepends a listener for the given event type. */ earlyOn & string>(type: EventType, listener: Emitter.Listener>, options?: TypedListenerOptions): typeof this; /** * Prepends a one-time listener for the given event type. */ earlyOnce & string>(type: EventType, listener: Emitter.Listener>, 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.Listener>): 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, EmitterHookMap, EventMap, HookListenerOptions, ReservedEventMap, TypedEvent, TypedListenerOptions, WithReservedEvents }; //# sourceMappingURL=index.d.mts.map