interface ContextAwareEventHandler { /** Dispatch the event to the bound event handler with the provided context. */ dispatch(event: Event, data?: unknown): void; } /** Context event handler function. */ type EventHandler = ContextAwareEventHandler['dispatch']; /** Error handler function for handling errors in event listeners. */ type ErrorHandler = (error: Error, eventName: string, event: Event, data?: unknown) => void; /** Represents a subscription to an event with a context-aware event handler. */ type Subscription = { eventName: string; contextEventHandler: ContextAwareEventHandler; }; /** Subscription options. */ type SubscriptionOptions = { /** The context to bind to the event handler. Defaults to the event handler itself. */ context?: unknown; /** If true, the subscription will automatically unsubscribe after the first event. Defaults to false. */ once?: boolean; /** Handles synchronous errors thrown by this subscription's event handler. */ errorHandler?: ErrorHandler; }; /** Publish options. */ type PublishOptions = { /** The event to be handled. Defaults to a new `CustomEvent` named after the event. */ event?: Event; /** The value to be passed to the event handler as a parameter. */ data?: T; /** Handles synchronous event handler errors for this publish when the subscription has no error handler. */ errorHandler?: ErrorHandler; }; /** A wrapper for an event handler that binds a context to the event handler. */ declare class ContextEventHandler implements ContextAwareEventHandler { #private; /** * @param context The context to bind to the event handler. * @param eventHandler The event handler to call when the event is published. */ constructor(context: unknown, eventHandler: EventHandler); /** * Dispatch the event to the bound event handler with the provided context. * * @param event The event to handle * @param data The value to be passed to the event handler as a parameter. */ dispatch(event: Event, data?: unknown): void; /** * A String value that is used in the creation of the default string * description of an object. Called by the built-in method {@link Object.prototype.toString}. * * @returns The default string description of this object. */ get [Symbol.toStringTag](): string; } /** A class that allows objects to subscribe to events and be notified when the event is published. */ declare class Subscribr { #private; /** * Subscribe to an event * * @param eventName The event name to subscribe to. * @param eventHandler The event handler to call when the event is published. * @param options Subscription options. * @param options.context The context to bind to the event handler. Defaults to the event handler itself if not provided. * @param options.once If true, the subscription is removed before the handler is first invoked. * @returns An object used to check if the subscription still exists and to unsubscribe from the event. */ subscribe(eventName: string, eventHandler: EventHandler, { context, once, errorHandler }?: SubscriptionOptions): Subscription; /** * Unsubscribe from the event * * @param subscription The subscription to unsubscribe. * @returns true if eventListener has been removed successfully. false if the value is invalid or not found. */ unsubscribe(subscription: Subscription): boolean; /** * Publish an event * Listeners are snapshotted at the start; additions wait for the next publish, * and listeners removed before their turn are skipped. * * @template T * @param eventName The name of the event. * @param options Publish options. * @param options.event The event to be handled. Defaults to a new `CustomEvent` named after the event. * @param options.data The value to be passed to the event handler as a parameter. * @param options.errorHandler Handles synchronous event handler errors when a subscription has no error handler. */ publish(eventName: string, { event, data, errorHandler }?: PublishOptions): void; /** * Check if the event and handler are subscribed. * * @param subscription The subscription object. * @returns true if the event name and handler are subscribed, false otherwise. */ isSubscribed(subscription: Subscription): boolean; /** Clears all subscriptions. The instance should not be used after calling this method. */ destroy(): void; /** * A String value that is used in the creation of the default string * description of an object. Called by the built-in method {@link Object.prototype.toString}. * * @returns The default string description of this object. */ get [Symbol.toStringTag](): string; } export { ContextEventHandler, ErrorHandler, EventHandler, PublishOptions, Subscribr, Subscription, SubscriptionOptions };