import { AsyncTeardownManager, type Subscription } from "../teardown-manager.js"; /** * Extracts the keys of events from an object. * @template T * @typedef {Object} EventKeys * @property {keyof T} key - The key of the event. */ export type EventKeys = { [key in keyof T]: T[key] extends IEvent ? key : never; }[keyof T]; /** * Options for attaching events. * @typedef {Object} AttachEventOptions * @property {boolean} once - Whether the event should be triggered only once. */ export interface AttachEventOptions { once: boolean; } /** * Represents a listener function for events. * @template T * @template TReturnType * @typedef {function(...T): TReturnType} Listener */ export type Listener = (...args: T) => TReturnType; /** * Represents a listener function for a specific event type. * @template T * @typedef {function(...TArgs): TReturnType} EventListener */ export type EventListener = T extends IEventSink ? (...args: TArgs) => TReturnType : never; /** * Extracts the argument types of an event. * @template TEvent * @typedef {TEvent extends IEventSink ? X : never} EventArgs */ export type EventArgs = TEvent extends IEventSink ? X : never; /** * Extracts the return type of an event. * @template TEvent * @typedef {TEvent extends IEventSink ? Y : never} EventReturnType */ export type EventReturnType = TEvent extends IEventSink ? Y : never; /** * Extracts the options type of an event. * @template TEvent * @typedef {TEvent extends IEventSink ? Z : never} EventOptions */ export type EventOptions = TEvent extends IEventSink ? Z : never; /** * Interface for an event sink. * @template T * @template TReturnType * @template TOptions * @interface IEventSink */ export interface IEventSink { maxListeners: number; hasListeners: boolean; addListener(listener: Listener, options?: TOptions): Subscription; removeListener(listener?: Listener): boolean; [Symbol.dispose](): void; } /** * Interface for an event. * @template T * @template TReturnType * @template TOptions * @interface IEvent * @extends {IEventSink} */ export type IEvent = IEventSink & { emit(...args: T): TReturnType; pipe(event: IEvent): Subscription; }; /** * Event class to manage listeners and emit events. * @template T * @template TReturnType * @template TOptions * @extends {TeardownManager} * @implements {IEvent} */ export declare class Event extends AsyncTeardownManager implements IEvent { maxListeners: number; protected readonly combineReturnTypes?: (args: TReturnType[]) => TReturnType; /** * Combines named events into a single event. * @template T * @param {T} obj - The object containing named events. * @returns {IEvent<[{ [K in keyof T]: T[K] extends IEventSink ? X : T[K] }], void>} - The combined event. */ static combineNamed; }>(obj: T): IEvent<[{ [K in keyof T]: T[K] extends IEventSink ? X : T[K]; }], void>; /** * Combines multiple events into a single event. * @template T * @param {...T} events - The events to combine. * @returns {IEventSink} - The combined event. */ static combine(...events: { [K in keyof T]?: T[K] | IEventSink; }): IEventSink; /** * Creates an instance of Event. * @param {number} [maxListeners=Event.maxListeners] - The maximum number of listeners. * @param {(args: TReturnType[]) => TReturnType} [combineReturnTypes] - Function to combine return types. */ constructor(maxListeners?: number, combineReturnTypes?: (args: TReturnType[]) => TReturnType); /** * Clones the event. * @returns {Event} - The cloned event. */ clone(): Event; static maxListeners: number; protected readonly listeners: Listener[]; /** * Checks if the event has listeners. * @returns {boolean} - True if the event has listeners, false otherwise. */ get hasListeners(): boolean; /** * Adds a listener to the event. * @param {Listener} listener - The listener to add. * @param {TOptions} [options] - The event options. * @returns {Subscription} - The subscription. */ addListener(listener: (...args: T) => TReturnType, options?: TOptions): Subscription; /** * Removes a listener from the event. * @param {Listener} listener - The listener to remove. * @returns {boolean} - True if the listener was removed, false otherwise. */ removeListener(listener?: (...args: T) => TReturnType): boolean; /** * Emits the event. * @param {...T} args - The arguments to pass to the listeners. * @returns {TReturnType} - The return value of the listeners. */ emit(...args: T): TReturnType; /** * Pipes the event to another event or emitter. * @param {IEvent} event - The event to pipe to. * @returns {Subscription} - The subscription. */ pipe(event: IEvent): Subscription; /** * Pipes the event to another event or emitter. * @param {IEvent} event - The event to pipe to. * @returns {Subscription} - The subscription. */ pipe(event: Listener): IEventSink; /** * Disposes the event. */ [Symbol.dispose](): void; } /** * PipeEvent class to map and pipe events. * @template T * @template U * @template TReturnType * @template TOptions * @extends {Event} */ export declare class PipeEvent extends Event { protected readonly source: IEventSink; private readonly map; protected subscription: ReturnType['addListener']>; /** * Creates an instance of PipeEvent. * @param {IEventSink} source - The source event sink. * @param {(...args: T) => U} map - Function to map arguments. * @param {(results: TReturnType[]) => TReturnType} combineResults - Function to combine return types. */ constructor(source: IEventSink, map: (...args: T) => U, combineResults: (results: TReturnType[]) => TReturnType); /** * Subscribes to the source event if required. */ protected subscribeToSourceIfRequired(): void; /** * Adds a listener to the pipe event. * @param {Listener} listener - The listener to add. * @param {TOptions} [options] - The event options. * @returns {Subscription} - The subscription. */ addListener(listener: (...args: U) => TReturnType, options?: TOptions): () => boolean; /** * Removes a listener from the pipe event. * @param {Listener} listener - The listener to remove. * @returns {boolean} - True if the listener was removed, false otherwise. */ removeListener(listener: (...args: U) => TReturnType): boolean; } /** * ReplayEvent class to manage events with a buffer. * @template T * @template TReturnType * @extends {Event} */ export declare class ReplayEvent extends Event { private readonly bufferLength; private readonly buffer; /** * Creates an instance of ReplayEvent. * @param {number} bufferLength - The length of the buffer. * @param {number} maxListeners - The maximum number of listeners. * @param {(args: TReturnType[]) => TReturnType} [combineReturnTypes] - Function to combine return types. */ constructor(bufferLength: number, maxListeners?: number, combineReturnTypes?: (args: TReturnType[]) => TReturnType); /** * Emits the event and stores the arguments in the buffer. * @param {...T} args - The arguments to pass to the listeners. * @returns {TReturnType} - The return value of the listeners. */ emit(...args: T): TReturnType; /** * Adds a listener to the event and replays the buffered events. * @param {Listener} listener - The listener to add. * @param {{ once?: boolean }} [options] - The event options. * @returns {Subscription} - The subscription. */ addListener(listener: (...args: T) => TReturnType, options?: { once?: boolean; }): () => boolean; }