@akala/core
Version:
106 lines (105 loc) • 5.41 kB
TypeScript
import { AsyncTeardownManager, type Subscription } from "../teardown-manager.js";
import type { AllEventKeys, EventBus, SpecialEvents } from "./event-bus.js";
import { Event, type EventArgs, type EventKeys, type EventListener, type EventOptions, type EventReturnType, type IEvent } from "./shared.js";
export type AllEvents<T extends object> = T & SpecialEvents;
/**
* EventEmitter class to manage events and listeners.
* @template T
* @implements {Disposable}
*/
export declare class EventEmitter<T extends {
[key in keyof T]: IEvent<any[], any>;
} = Record<PropertyKey, IEvent<unknown[], unknown>>> extends AsyncTeardownManager implements EventBus<T & SpecialEvents> {
/**
* Checks if there are listeners for a given event.
* @template TKey
* @param {TKey} name - The name of the event.
* @returns {boolean} - True if there are listeners, false otherwise.
*/
hasListener<const TKey extends AllEventKeys<T>>(name: TKey): boolean;
protected readonly events: Partial<AllEvents<T>>;
maxListeners: number;
/**
* Gets the defined events.
* @returns {AllEventKeys<T>[]} - An array of defined event names.
*/
get definedEvents(): EventKeys<T & SpecialEvents>[];
/**
* Creates an instance of EventEmitter.
* @param {number | T} [init] - Initial value or number of max listeners.
*/
constructor(init?: number | T, abort?: AbortSignal);
protected eventFactory<const TEvent extends AllEventKeys<T>>(_name: TEvent): AllEvents<T>[TEvent];
/**
* Sets an event.
* @template TEvent
* @param {TEvent} eventName - The name of the event.
* @param {AllEvents<T>[TEvent]} event - The event to set.
*/
set<const TEvent extends AllEventKeys<T>>(eventName: TEvent, event: AllEvents<T>[TEvent]): void;
/**
* Gets an event.
* @template TEvent
* @param {TEvent} eventName - The name of the event.
* @returns {AllEvents<T>[TEvent]} - The event.
*/
get<const TEvent extends AllEventKeys<T>>(eventName: TEvent): AllEvents<T>[TEvent];
/**
* Gets or creates an event.
* @template TEvent
* @param {TEvent} eventName - The name of the event.
* @returns {AllEvents<T>[TEvent]} - The event.
*/
getOrCreate<const TEvent extends AllEventKeys<T>>(eventName: TEvent): AllEvents<T>[TEvent];
/**
* Sets the maximum number of listeners for an event.
* @template TEvent
* @param {number} maxListeners - The maximum number of listeners.
* @param {TEvent} [event] - The event to set the max listeners for.
*/
setMaxListeners<const TEvent extends AllEventKeys<T>>(maxListeners: number, event?: TEvent): void;
/**
* Emits an event.
* @template TEvent
* @param {TEvent} event - The event to emit.
* @param {...EventArgs<T[TEvent]>} args - The arguments to pass to the event listeners.
* @returns {false | EventReturnType<T[TEvent]>} - The return value of the event listeners or false if the event does not exist.
*/
emit<const TEvent extends AllEventKeys<T>>(event: TEvent, ...args: EventArgs<AllEvents<T>[TEvent]>): false | EventReturnType<AllEvents<T>[TEvent]>;
/**
* Adds a listener for an event.
* @template TEvent
* @param {TEvent} event - The event to listen to.
* @param {EventListener<AllEvents<T>[TEvent]>} handler - The event handler.
* @param {EventOptions<AllEvents<T>[TEvent]>} [options] - The event options.
* @returns {Subscription} - The subscription.
*/
on<const TEvent extends AllEventKeys<T>>(event: TEvent, handler: EventListener<AllEvents<T>[TEvent]>, options?: EventOptions<AllEvents<T>[TEvent]>): Subscription;
/**
* Adds a one-time listener for an event.
* @template TEvent
* @param {TEvent} event - The event to listen to.
* @param {EventListener<AllEvents<T>[TEvent]>} handler - The event handler.
* @param {Omit<EventOptions<AllEvents<T>[TEvent]>, 'once'>} [options] - The event options.
* @returns {Subscription} - The subscription.
*/
once<const TEvent extends AllEventKeys<T>>(event: TEvent, handler: EventListener<AllEvents<T>[TEvent]>, options?: Omit<EventOptions<AllEvents<T>[TEvent]>, 'once'>): Subscription;
/**
* Removes a listener for an event.
* @template TEvent
* @param {TEvent} event - The event to remove the listener from.
* @param {EventListener<AllEvents<T>[TEvent]>} handler - The event handler.
* @returns {boolean} - True if the listener was removed, false otherwise.
*/
off<const TEvent extends AllEventKeys<T>>(event: TEvent, handler: EventListener<AllEvents<T>[TEvent]>): boolean;
/**
* Disposes the event emitter.
*/
[Symbol.dispose](): void;
}
export declare class TopDownNamespaceEventEmitter<T extends Record<string, IEvent<unknown[], unknown>> = Record<string, Event<unknown[]>>> extends EventEmitter<T> {
on<const TEvent extends AllEventKeys<T>>(event: TEvent, handler: EventListener<AllEvents<T>[TEvent]>, options?: EventOptions<AllEvents<T>[TEvent]>): Subscription;
}
export declare class BottomUpNamespaceEventEmitter<T extends Record<string, IEvent<unknown[], unknown>> = Record<string, Event<unknown[]>>> extends EventEmitter<T> {
emit<const TEvent extends AllEventKeys<T>>(event: TEvent, ...args: EventArgs<AllEvents<T>[TEvent]>): false | EventReturnType<AllEvents<T>[TEvent]>;
}