/** * Event Manager * Centralized manager for all event factories and event coordination */ import { BaseEventFactory } from './factories'; import type { ConfigEventFactory, PerformanceEventFactory, NetworkEventFactory, HeaderEventFactory, ErrorEventFactory, DebugEventFactory, ClientEventFactory, CacheEventFactory } from './factories'; import { type EventScopeWithTemporary } from '@plyaz/types/api'; import type { EventEmitter } from '../pubsub'; /** * Centralized Event Manager * Provides unified access to all event factories and the shared emitter * * ## Event Scope System * * Events are emitted with scope awareness to control which handlers receive them: * * ### Default Behavior * - **ALL scopes are active by default**: ['global', 'config', 'client', 'request'] * - Events are emitted to handlers at ALL scope levels unless restricted * - Factories emit events without scope awareness; EventManager adds scopes based on context * * ### Scope Contexts * 1. **Global Config**: `setGlobalConfig()` sets scopes to ['global', 'config'] * 2. **Client Instance**: Creates with ALL scopes by default (merged behavior) * 3. **Temporary Override**: Sets scopes to ['request'] only (isolated) * 4. **Custom**: Can explicitly set any combination of scopes * * ### How It Works * ```typescript * // Factory emits simple event (no scope): * headerFactory.emit({ type: 'headers:changed', data: {...} }) * * // EventManager adds scopes based on current context: * // If scopes = ['global', 'client'], emits to: * // - 'global:headers:changed' * // - 'global:headers:*' * // - 'client:headers:changed' * // - 'client:headers:*' * // - 'headers:changed' (backwards compat) * // - 'headers:*' (backwards compat) * ``` * * ### Priority Order (lowest to highest) * 1. GLOBAL - App-wide handlers * 2. CONFIG - Configuration-level handlers * 3. CLIENT - Instance-specific handlers * 4. REQUEST/TEMPORARY - Request-level handlers */ export declare class EventManager extends BaseEventFactory<'eventManager'> { private static instance; private readonly headerFactory; private readonly networkFactory; private readonly errorFactory; private readonly debugFactory; private readonly configFactory; private readonly performanceFactory; private readonly cacheFactory; private readonly clientFactory; private eventScopes; private currentEventScopes; private constructor(); /** * Track EventManager operations for debugging and analysis * Uses the operation queue to avoid circular dependency with UnifiedDebugger */ private trackEventManagerOperation; /** * Wrap a factory to make it use EventManager's scope configuration * This ensures that when factories emit events through EventManager, * they respect the configured scopes (e.g., excluding global when needed) */ private createScopeAwareFactory; /** * Set up internal event handlers for cross-cutting concerns */ private setupInternalHandlers; /** * Set up header event handlers */ private setupHeaderHandlers; /** * Set up network event handlers */ private setupNetworkHandlers; /** * Set up error event handlers */ private setupErrorHandlers; /** * Set up debug event handlers */ private setupDebugHandlers; /** * Set up performance event handlers */ private setupPerformanceHandlers; /** * Set up config event handlers */ private setupConfigHandlers; /** * Get singleton instance */ static getInstance(): EventManager; /** * Set event scopes for this manager instance * Used by clients to configure which scopes receive events */ setEventScopes(scopes: EventScopeWithTemporary[]): void; /** * Get current event scopes */ getEventScopes(): EventScopeWithTemporary[]; /** * Override emitAcrossScopes to use EventManager's configured scopes * This allows EventManager to control which scopes are active * * @param eventType - The type of event (e.g., 'error', 'debug', 'headers', 'network') * @param eventName - The specific event name (e.g., 'onNetworkError', 'conflict', 'changed') * @param data - The event data * @param scopes - Optional scopes to emit to (defaults to configured scopes) */ emitAcrossScopes(eventType: string, eventName: string, data: unknown, scopes?: EventScopeWithTemporary[]): void; /** * Determine priority based on event type and name */ private determinePriority; /** * Get the shared event emitter */ getEmitter(): EventEmitter; /** * Get header event factory */ get headers(): HeaderEventFactory; /** * Get network event factory */ get network(): NetworkEventFactory; /** * Get error event factory */ get errors(): ErrorEventFactory; /** * Get debug event factory */ get debug(): DebugEventFactory; /** * Get config event factory */ get config(): ConfigEventFactory; /** * Get performance event factory */ get performance(): PerformanceEventFactory; /** * Get cache event factory */ get cache(): CacheEventFactory; /** * Get client event factory */ get client(): ClientEventFactory; /** * Override on to support any event string (not just namespace-prefixed) * @param event - Any event string * @param handler - Event handler */ on(event: string, handler: (data: T) => void): () => void; /** * Override once to support any event string (not just namespace-prefixed) * @param event - Any event string * @param handler - Event handler */ once(event: string, handler: (data: T) => void): () => void; /** * Get the shared event emitter instance * @returns The underlying EventEmitter */ getSharedEmitter(): EventEmitter; /** * Get event statistics * @returns Object with totalEvents and totalListeners */ getEventStats(): { totalEvents: number; totalListeners: number; }; /** * Get active scopes * @returns Array of active event scopes */ getActiveScopes(): EventScopeWithTemporary[]; /** * Reset the event manager (mainly for testing) */ static reset(): void; } /** * Export singleton getter */ export declare function getEventManager(): EventManager; /** * Convenience exports for direct factory access */ export declare const eventManager: { readonly headers: HeaderEventFactory; readonly network: NetworkEventFactory; readonly errors: ErrorEventFactory; readonly debug: DebugEventFactory; readonly config: ConfigEventFactory; readonly performance: PerformanceEventFactory; readonly cache: CacheEventFactory; readonly client: ClientEventFactory; readonly emitter: ReturnType; on: (event: string, handler: (data: T) => void) => (() => void); once: (event: string, handler: (data: T) => void) => (() => void); emit: (event: string, data?: T) => void; off: (event: string, handler?: Function) => void; removeAllListeners: (event?: string) => void; listeners: (event: string) => Function[]; listenerCount: (event: string) => number; emitAcrossScopes: (eventType: string, eventName: string, data: unknown, scopes?: EventScopeWithTemporary[]) => void; setEventScopes: (scopes: EventScopeWithTemporary[]) => void; getEventScopes: () => EventScopeWithTemporary[]; getSharedEmitter: () => ReturnType; getEventStats: () => ReturnType; getActiveScopes: () => EventScopeWithTemporary[]; }; //# sourceMappingURL=EventManager.d.ts.map