/** * Base Event Factory * Abstract base class for all event factories providing common functionality */ import type { EventScope, HandlerStrategy, EventScopeWithTemporary, BaseEvent, EventOptions, HandlerOptions } from '@plyaz/types/api'; import type { EventEmitter } from '../../pubsub'; /** * Abstract base class for event factories * Provides common functionality for creating and emitting events */ export declare abstract class BaseEventFactory { protected readonly emitter: EventEmitter; protected readonly namespace: TNamespace; readonly scope: string; private scopedHandlers; private originalHandlers; constructor(namespace: TNamespace, emitter: EventEmitter); /** * Track factory operations using the queue pattern * This avoids circular dependency and ensures all operations are captured */ private trackFactoryOperation; /** * Generic emit method that works with any event string * Intelligently routes through scope system if applicable */ emit(event: string, data?: T): void; /** * Direct off method that works with any event string * This is what EventManager inherits and uses */ off(event: string, handler?: Function): void; /** * Direct removeAllListeners that works with any event string * This is what EventManager inherits and uses */ removeAllListeners(event?: string): void; /** * Direct listeners method that works with any event string * This is what EventManager inherits and uses */ listeners(event: string): Function[]; /** * Direct listenerCount that works with any event string * This is what EventManager inherits and uses */ listenerCount(event: string): number; /** * Create base event structure */ protected createBaseEvent(type: string, source?: string): BaseEvent; /** * Emit an event to the shared emitter * When used through EventManager, respects its configured scopes */ protected emitEvent(event: T): void; /** * Create namespaced event key */ protected createEventKey(eventName: string): string; /** * Validate event data before creation * Can be overridden by subclasses for specific validation */ protected validateEventData(data: unknown): void; /** * Create and optionally emit an event */ protected createAndEmit(event: T, options?: EventOptions): T; /** * Public emit method for testing purposes * In production, use the specific emit methods on each factory */ emitGeneric(eventNameOrType: string, data?: unknown): void; /** * Emit events across all configured scopes with proper priority order * Ensures handlers at all scopes are called in the correct order * * @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 all scopes) * * 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 (highest priority) * * This method emits: * - Scoped specific events (e.g., global:headers:changed) * - Scoped wildcard events (e.g., global:headers:*) * - Non-scoped events for backwards compatibility (e.g., headers:changed) * - Non-scoped wildcards for backwards compatibility (e.g., headers:*) */ emitAcrossScopes(eventType: string, eventName: string, data: unknown, scopes?: EventScopeWithTemporary[]): void; /** * Subscribe to events in this namespace */ on(eventName: string, handler: (data: T) => void): () => void; /** * Subscribe to all events in this namespace */ onAny(handler: (data: T) => void): () => void; /** * Subscribe once to events in this namespace * Handler will be automatically removed after first execution */ once(eventName: string, handler: (data: T) => void): () => void; /** * Subscribe to scoped events in this namespace * This listens to events emitted via emitAcrossScopes for a specific scope * * @param scope - The scope to listen to ('global', 'config', 'client', 'request') * @param eventName - The event name (e.g., 'validation', 'network', '*') * @param handler - The event handler */ onScoped(scope: EventScope, eventName: string, handler: (data: T) => void): () => void; /** * Subscribe once to scoped events in this namespace * Handler will be automatically removed after first execution * * @param scope - The scope to listen to ('global', 'config', 'client', 'request') * @param eventName - The event name (e.g., 'validation', 'network', '*') * @param handler - The event handler */ onceScoped(scope: EventScope, eventName: string, handler: (data: T) => void): () => void; /** * Apply handler strategy to determine final handler list */ private applyHandlerStrategy; /** * Register handlers with the emitter based on strategy */ private registerHandlersWithEmitter; /** * Create unsubscribe function for handler */ private createHandlerUnsubscribe; /** * Add multiple handlers to a scoped event (for chaining) * This is used when multiple handlers need to be called for the same event * * @param scope - The scope to listen to ('global', 'config', 'client', 'request') * @param eventName - The event name (e.g., 'validation', 'network', '*') * @param handler - The event handler to add * @param options - Options for handler registration including strategy and originalHandler */ addScopedHandler(scope: EventScopeWithTemporary, eventName: string, handler: (data: T) => void, options?: HandlerOptions & { originalHandler?: (data: T) => void; }): () => void; /** * Remove a specific handler from a scoped event * * @param scope - The scope to remove from ('global', 'config', 'client', 'request') * @param eventName - The event name (e.g., 'validation', 'network', '*') * @param handler - The specific handler to remove (should be the original handler, not wrapped) */ removeScopedHandler(scope: EventScopeWithTemporary, eventName: string, handler: (data: T) => void): void; /** * Remove all listeners for a scoped event * * @param scope - The scope to clear ('global', 'config', 'client', 'request') * @param eventName - The event name (e.g., 'validation', 'network', '*') */ removeAllScopedListeners(scope: EventScopeWithTemporary, eventName?: string): void; /** * Subscribe once to all events in this namespace * Handler will be automatically removed after first execution of any event */ onceAny(handler: (data: T) => void): () => void; /** * Unsubscribe from events in this namespace (namespace-aware) * Use this for factory-specific events */ offNamespaced(eventName: string, handler?: (data: unknown) => void): void; /** * Unsubscribe from all events in this namespace */ offAny(handler?: (data: unknown) => void): void; /** * Remove all handlers for an event in this namespace (namespace-aware) * Use this for factory-specific events */ removeAllListenersNamespaced(eventName?: string): void; /** * Get all listeners for an event in this namespace (namespace-aware) * Use this for factory-specific events */ listenersNamespaced(eventName: string): Function[]; /** * Get listener count for an event in this namespace (namespace-aware) * Use this for factory-specific events */ listenerCountNamespaced(eventName: string): number; /** * Get the namespace for this factory */ getNamespace(): string; /** * Get all handlers for a specific scope * @param scope - The scope to get handlers for * @returns Map of event names to handlers */ getScopedHandlers(scope: EventScope): Map void>>; /** * Get handlers for a specific scope and event * @param scope - The scope to get handlers for * @param eventName - The event name * @returns Array of handlers for the event */ getScopedEventHandlers(scope: EventScope, eventName: string): Array<(data: unknown) => void>; /** * Get original handlers for a specific scope (before wrapping) * @param scope - The scope to get handlers for * @returns Map of event names to original handlers */ getOriginalScopedHandlers(scope: EventScopeWithTemporary): Map void>>; /** * Get original handlers for a specific scope and event (before wrapping) * @param scope - The scope to get handlers for * @param eventName - The event name * @returns Array of original handlers for the event */ getOriginalScopedEventHandlers(scope: EventScope, eventName: string): Array<(data: unknown) => void>; /** * Clear all handlers for a specific scope * @param scope - The scope to clear */ clearScopedHandlers(scope: EventScope): void; /** * Merge handlers from multiple scopes using the event emitter's merge logic * Follows precedence: global → client → request (request has highest priority) * This is used for event handlers that need to be chained together * * @param global - Global scope handler(s) * @param client - Client scope handler(s) * @param request - Request scope handler(s) * @returns The first merged handler or undefined */ mergeHandlerGroup(global?: T, client?: T, request?: T): T | T[] | undefined; /** * Merge handlers with explicit priority handling * Used for config override strategies where the highest priority wins * Priority order: request > client > config > global * * @param global - Global scope handler(s) * @param config - Config scope handler(s) * @param client - Client scope handler(s) * @param request - Request scope handler(s) * @returns Merged handler(s) based on priority */ mergeHandlersByPriority(global?: T, config?: T, client?: T, request?: T): T | undefined; /** * Main handler merging method that respects handler strategy and priority * This is the primary entry point for all handler merging operations * * @param options - Options containing strategy, usePriority, and handlers * @returns Merged handler(s) based on the strategy and priority setting */ mergeHandlers(options: { strategy: HandlerStrategy; usePriority: boolean; global?: T; client?: T; request?: T; }): T | undefined; /** * Check if a value is a handler object (has function properties) */ private isHandlerObject; /** * Merge handler objects by merging individual properties */ private mergeHandlerObjects; } //# sourceMappingURL=base.d.ts.map