/** * Unified Client Event Management System * * Centralizes all event handling for API clients using the shared event emitter. * Handles internal events, external events, and configuration-based events. */ import type { RequestConfig, FetchResponse, ResponseError } from 'fetchff'; import { getEventManager } from '../events'; import type { ApiPackageError } from '../errors/adapter'; import type { ConfigConflict, DebugInfo, ApiClientInstance, ApiClientOptions, ApiConfig, UpdateConfigOptions, HandlerOptions, ApiClientWithEvents, ClientEventManagerLike, EventScopeWithTemporary } from '@plyaz/types/api'; import type { EndpointsList } from '../endpoints'; /** * Client Event Manager * Manages all event handling for a client instance using the shared emitter */ export declare class ClientEventManager implements ClientEventManagerLike> { private readonly client; private config; private readonly eventManager; readonly emitter: import("@plyaz/types").EventEmitter; private readonly monitoringState; private readonly handlerRegistry; private readonly eventStats; private static readonly MAX_RECENT_EVENTS; private static readonly DEFAULT_MAX_RETRY_ATTEMPTS; private static readonly DEFAULT_RETRY_DELAY; /** * Track an event for statistics * @private */ private trackEvent; /** * Setup universal event tracking for all events * @private */ private setupEventTracking; constructor(client: ApiClientInstance, config: Partial); /** * Register all namespace handlers from config */ private setupNamespaceHandlers; /** * Setup monitoring if enabled */ private setupMonitoringIfNeeded; /** * Setup temporary override handlers for network */ private setupTemporaryOverrideHandlers; /** * Setup all event handlers based on configuration */ private setupEventHandlers; /** * Register handlers for a specific namespace with proper scope * Maps handler names to the actual event names emitted by factories */ private registerHandlers; /** * Clear all handlers for a specific scope */ private clearHandlersForScope; /** * Get the proper handler namespace for an event type and scope * Delegates to the centralized function in namespaces.ts */ private getHandlerNamespace; /** * Add event handler with strategy */ addHandler(event: string, handler: Function | Function[], options?: HandlerOptions): () => void; /** * Get handlers for an event */ private getHandlers; /** * Clear handlers for an event */ private clearHandlers; /** * Try to find exact event name match from namespace events */ private findExactEventMatch; /** * Remove namespace-specific suffixes from handler name */ private removeNamespaceSuffixes; /** * Convert handler name to kebab-case event name */ private toKebabCaseEventName; /** * Map handler name to the actual event name emitted by factories * e.g., 'onNetworkError' -> 'errors:network' */ private mapHandlerNameToEventName; /** * Get the events object for a namespace using EVENT_NAMESPACES */ private getNamespaceEvents; /** * Type guard for NetworkQualityEvent */ private isNetworkQualityEvent; /** * Setup network event forwarding and listeners */ private setupNetworkEventForwarding; /** * Track ClientEventManager operations using the operation queue to avoid circular dependency */ private trackClientEventManagerOperation; /** * Setup event scopes based on strategy */ private setupEventScopes; /** * Apply config update based on strategy */ private applyConfigStrategy; /** * Register standard namespace handlers from updates */ private registerStandardHandlers; /** * Register network override handler if present */ private registerNetworkOverrideHandler; /** * Handle debug events and monitoring updates */ private handleDebugEventsUpdate; /** * Register all event handlers from updates */ private registerEventHandlers; /** * Update configuration and re-setup handlers */ updateConfig(updates: Partial, options?: UpdateConfigOptions): void; /** * Clear all temporary configuration overrides * Clears handlers at REQUEST/TEMPORARY scope */ clearTemporaryOverrides(): void; /** * Check for conflicts */ checkConflicts(): ConfigConflict[]; /** * Generate debug info */ getDebugInfo(): DebugInfo; /** * Calculate impact level */ private calculateImpactLevel; /** * Start monitoring */ startMonitoring(): void; /** * Stop monitoring */ stopMonitoring(): void; /** * Check if monitoring */ isMonitoring(): boolean; /** * Get event statistics for monitoring and debugging */ getEventStats(): { totalEvents: number; totalListeners: number; listenerCount: number; recentEvents: Array<{ type: string; timestamp: number; data?: unknown; }>; overrideCount: number; eventCountsByType: Record; }; /** * Get the active event scopes from the EventManager */ getActiveScopes(): EventScopeWithTemporary[]; /** * Emit error event at CLIENT scope * * Note: ApiPackageError instances automatically emit to ALL scopes when created. * This method emits specifically at CLIENT scope, useful for: * - Re-emitting errors that were caught and handled elsewhere * - Manual emission timing control * - Client-level error broadcasting */ emitError(error: ApiPackageError): void; /** * Emit request start event * Called by fetchff onRequest interceptor */ emitRequestStart(config: RequestConfig): void; /** * Emit response received event * Called by fetchff onResponse interceptor */ emitResponseReceived(response: FetchResponse): void; /** * Emit retry attempt event * Called by fetchff onRetry interceptor */ emitRetryAttempt(error: ResponseError, attemptNumber: number, config?: RequestConfig): void; /** * Dispose of all resources */ dispose(): void; } /** * Setup client events for an API client instance * Centralizes all event configuration and management */ export declare function setupClientEvents(client: ApiClientInstance, globalConfig: ApiConfig, clientConfig: ApiClientOptions): ApiClientWithEvents; /** * Check if there's meaningful configuration to apply * Returns false if config is empty and updateOptions only has default values * * @param config - The merged API configuration * @param updateOptions - The update configuration options * @returns True if updateConfig should be called, false otherwise * * @example * ```typescript * const mergedConfig = mergeConfigs(serviceDefaults, options?.apiConfig ?? {}); * const updateOptions = { strategy: 'temporary', ...options?.updateConfigOptions }; * * if (shouldApplyConfig(mergedConfig, updateOptions)) { * client.updateConfig(mergedConfig, updateOptions); * } * ``` */ export declare function shouldApplyConfig(config: Partial, updateOptions: UpdateConfigOptions): boolean; export declare function mergeEventConfigs(global?: Partial, client?: Partial, request?: Partial): Partial; //# sourceMappingURL=clientEventManager.d.ts.map