export type EventTypeWithData = K extends keyof ET ? ET[K] extends undefined ? never : K extends string ? K : never : never; export type EventTypeWithoutData = K extends keyof ET ? ET[K] extends undefined ? K extends string ? K : never : never : never; export type EventType = K extends string ? K : never; export type EventDataType, K extends keyof ET> = ET[K] extends undefined ? undefined : ET[K]; /** * A class that provides an event hub for managing custom events. * * @template Events - The type of events that the EventHub will handle. This type should be an object with event names as keys and event data types as values. This make all events strongly typed. */ export declare class EventHub { /** * The event emitter used to dispatch and listen for events. * * @private */ private _emitter; /** * Creates an instance of the EventHub. * @param emitter - An optional EventTarget to use as the event emitter. If not provided, a dedicated new EventTarget will be created. i.e., it can share the same emitter with other EventHubs (e.g., for a global event bus, or separately built apps via window object). */ constructor(emitter?: EventTarget); private _assigned; private _allAssigned; private _issueSignal; private _listen; /** * Emits an event without data. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to emit. */ emitEvent(event: EventTypeWithoutData): void; /** * Emits an event with data. * * @template ET - The type of events. * @template K - The key of the event. * @param data - data to include with the event. * @param event - The event to emit. */ emitEvent(event: EventTypeWithData, data: ET[K]): void; /** * Registers an event listener for a specific event. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to listen for. * @param callback - The callback to execute when the event is triggered. * @param options - Native `EventTarget` listener options. A supplied signal removes only this registration when aborted. * @returns An idempotent function that removes only this registration. */ on(event: EventTypeWithoutData, callback: (e: Event) => void | Promise, options?: AddEventListenerOptions): () => void; /** * Registers an event listener for a specific event. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to listen for. * @param callback - The callback to execute when the event is triggered. * @param options - Native `EventTarget` listener options. A supplied signal removes only this registration when aborted. * @returns An idempotent function that removes only this registration. */ on(event: EventTypeWithData, callback: (e: Event, data: ET[K]) => void | Promise, options?: AddEventListenerOptions): () => void; /** * Removes current event registrations in bulk. * * Prefer the disposer returned by `on`, `onEvent`, `once`, or `onceEvent` when removing one registration. * * @param event - The event whose registrations should be removed. * @param callback - The callback whose registrations should be removed. Omit it to remove every registration for the event. */ off(event: EventType, callback?: CallableFunction): void; /** * Removes all event listeners. */ offAll(): void; /** * Registers an event listener for a specific event, with a callback that only receives the event data. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to listen for. * @param callback - The callback to execute when the event is triggered. * @param options - Native `EventTarget` listener options. A supplied signal removes only this registration when aborted. * @returns An idempotent function that removes only this registration. */ onEvent(event: EventTypeWithoutData, callback: () => any, options?: AddEventListenerOptions): () => void; /** * Registers an event listener for a specific event, with a callback that only receives the event data. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to listen for. * @param callback - The callback to execute when the event is triggered. * @param options - Native `EventTarget` listener options. A supplied signal removes only this registration when aborted. * @returns An idempotent function that removes only this registration. */ onEvent(event: EventTypeWithData, callback: (data: ET[K]) => any, options?: AddEventListenerOptions): () => void; /** * Registers a one-time event listener for a specific event. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to listen for. * @param callback - The callback to execute when the event is triggered. * @param options - Native `EventTarget` listener options. The `once` option is always treated as `true`. * @returns An idempotent function that removes only this registration. */ once(event: EventTypeWithoutData, callback: (e: Event) => void, options?: AddEventListenerOptions): () => void; /** * Registers a one-time event listener for a specific event. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to listen for. * @param callback - The callback to execute when the event is triggered. * @param options - Native `EventTarget` listener options. The `once` option is always treated as `true`. * @returns An idempotent function that removes only this registration. */ once(event: EventTypeWithData, callback: (e: Event, data: ET[K]) => void, options?: AddEventListenerOptions): () => void; /** * Registers a one-time event listener for a specific event, with a callback that only receives the event data. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to listen for. * @param callback - The callback to execute when the event is triggered. * @param options - Native `EventTarget` listener options. The `once` option is always treated as `true`. * @returns An idempotent function that removes only this registration. */ onceEvent(event: EventTypeWithoutData, callback: () => void, options?: AddEventListenerOptions): () => void; /** * Registers a one-time event listener for a specific event, with a callback that only receives the event data. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to listen for. * @param callback - The callback to execute when the event is triggered. * @param options - Native `EventTarget` listener options. The `once` option is always treated as `true`. * @returns An idempotent function that removes only this registration. */ onceEvent(event: EventTypeWithData, callback: (data: ET[K]) => void, options?: AddEventListenerOptions): () => void; /** * Waits for a specific event to be emitted. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to wait for. * @returns A Promise that resolves with null. */ waitFor(event: EventTypeWithoutData): Promise; /** * Waits for a specific event to be emitted and returns a promise that resolves with the event data. * * @template ET - The type of events. * @template K - The key of the event. * @param event - The event to wait for. * @returns A promise that resolves with the event data. */ waitFor(event: EventTypeWithData): Promise; }