/** * Provides a simple, type-safe event notification implementation. This module allows components to implement the * observer pattern with minimal boilerplate and proper type checking. */ /** * A type-safe event notifier that manages event listeners and notifications. * * @template T The type of events this notifier will handle * @template E The type of error that can be handled (defaults to Error) * * EventNotifier provides: * * - Type-safe event subscriptions via `onEvent` * - Synchronized event notifications via `notify` * - Automatic cleanup of resources via `close` * - Status tracking via `active` property * - Error handling via optional error callback */ export type EventNotifier = { /** * Registers a listener function to be called when events are notified. Listeners are notified in the order they * were registered. * * @example * * ```ts * const notifier = createEventNotifier(); * const subscription = notifier.onEvent((message) => { * console.log(`Received message: ${message}`); * }); * * // Later, to stop listening: * subscription.close(); * ``` * * @param listener A function that will be called with the notified event * @returns A closeable to unregister the listener (all listeners are unregistered when the notifier is closed). */ onEvent: (listener: (event: T) => unknown) => { close: () => void; }; /** * Notifies all registered listeners with the provided event. * * This method: * * - Calls all registered listeners with the event in their registration order * - Ignores errors thrown by listeners (they won't affect other listeners) * - Ignores returned promises (results are not awaited) * - Does nothing if there are no listeners * - If the event is a function, it will be called if there are listeners and its return value will be * used as the event. * * @example * * ```ts * const notifier = createEventNotifier<{ type: string; value: number }>(); * notifier.onEvent((event) => { * console.log(`Received ${event.type} with value:`, event.value); * }); * * notifier.notify({ type: 'progress', value: 75 }); * ``` * * @param event The event to send to all listeners or a function that returns such event. */ notify: (event: T | (() => T)) => void; /** * Sets an error handler for the notifier. This handler will be called when a listener throws an error. * * @param handler A function that will be called with any errors thrown by listeners. */ onError: (handler: (error: E) => void) => void; /** * Closes the notifier and removes all listeners. * * @warning Failing to call close() on subscriptions or the notifier itself may lead to memory leaks. */ close: () => void; }; /** * Creates a type-safe event notifier. * * @example * * ```ts * // Simple string event notifier * const stringNotifier = createEventNotifier(); * * // Complex object event notifier * interface TaskEvent { * type: 'started' | 'completed' | 'failed'; * taskId: number; * details: { * name: string; * duration?: number; * }; * } * const taskNotifier = createEventNotifier(); * ``` * * @template T The type of events this notifier will handle * @template E The type of error that can be handled (defaults to Error) * @returns A new EventNotifier instance. */ export declare const createEventNotifier: () => EventNotifier; //# sourceMappingURL=eventNotifier.d.ts.map