/** * Service Registry - Frontend Service Auto-Initialization * * Provides a generic, scalable pattern for initializing domain services. * Services implement the InitializableDomainService interface and are * initialized automatically without hardcoded switch statements. * * @example * ```typescript * import { ServiceRegistry } from '@plyaz/core'; * import { FeatureFlagDomainService } from '@plyaz/core/frontend'; * import { ExampleDomainService } from '@plyaz/core/domain'; * * // Initialize services * await ServiceRegistry.initialize({ * apiClient: { baseURL: '/api' }, * services: [ * { service: FeatureFlagDomainService, config: { enabled: true } }, * { service: ExampleDomainService, config: { enabled: true, useRealApi: false } }, * ], * }); * * // Access services by key * const flags = ServiceRegistry.get('featureFlags'); * const isEnabled = await flags.isEnabled('my-flag'); * * // Or get with type safety * const example = ServiceRegistry.get('example'); * ``` */ import type { CoreDomainServiceInstance, CoreServiceEntry, CoreServiceInitConfig, CoreServiceRegistryConfig } from '@plyaz/types/core'; import type { ObservabilityAdapter } from '@plyaz/types/observability'; /** * Service Registry * * Manages frontend domain service instances with auto-initialization. * Supports immediate, lazy, and conditional initialization. * * Key features: * - Generic: No switch statements, services implement common interface * - Scalable: Add new services without modifying registry code * - Flexible: Immediate, lazy, or conditional initialization * - Type-safe: Get services with proper typing */ export declare class ServiceRegistry { private static readonly logger; private static get _services(); private static get _pending(); private static get _metadata(); private static get _environment(); private static set _environment(value); private static get _runtime(); private static set _runtime(value); private static get _apiClientOptions(); private static set _apiClientOptions(value); private static get _dbConfig(); private static set _dbConfig(value); private static get _cacheConfig(); private static set _cacheConfig(value); private static get _observabilityConfig(); private static set _observabilityConfig(value); private static get _observabilityInstance(); private static set _observabilityInstance(value); private static get _storageConfig(); private static set _storageConfig(value); private static get _notificationsConfig(); private static set _notificationsConfig(value); private static get _storeRegistry(); private static set _storeRegistry(value); private static get _initPromises(); /** * Initialize the service registry with the provided configuration. * * @param config - Registry configuration with services to initialize * * @example * ```typescript * await ServiceRegistry.initialize({ * apiClient: { baseURL: '/api' }, * services: [ * { service: FeatureFlagDomainService, config: { enabled: true } }, * { service: ExampleDomainService, config: { enabled: true, initWhen: 'lazy' } }, * ], * }); * ``` */ static initialize(config: CoreServiceRegistryConfig): Promise; /** * Get a singleton service instance by key (synchronous). * Throws if the service is not initialized or is non-singleton. * * @param key - Service key (e.g., 'featureFlags', 'example') * @returns The service instance * @throws CorePackageError if service not found, not initialized, or non-singleton * * @example * ```typescript * const flags = ServiceRegistry.get('featureFlags'); * const isEnabled = await flags.isEnabled('my-flag'); * ``` */ static get(key: string): T; /** * Create a new instance of a service. * Use this for non-singleton services or when you need a fresh instance. * * Note: The caller is responsible for disposing the returned instance. * * @param key - Service key * @param configOverrides - Optional config overrides for this instance * @returns Promise resolving to a new service instance * * @example * ```typescript * // Non-singleton service - always creates new instance * const instance1 = await ServiceRegistry.create('worker'); * const instance2 = await ServiceRegistry.create('worker'); * instance1 !== instance2; // true * * // With config overrides * const customWorker = await ServiceRegistry.create('worker', { * apiClient: { baseURL: 'https://custom-api.com' }, * }); * * // Don't forget to dispose when done! * instance1.dispose(); * instance2.dispose(); * ``` */ static create(key: string, configOverrides?: Partial): Promise; /** * Get a service instance by key with async initialization if needed. * Use this for lazy-initialized services. * * @param key - Service key * @returns Promise resolving to the service instance * * @example * ```typescript * const example = await ServiceRegistry.getAsync('example'); * const entities = await example.getAll(); * ``` */ static getAsync(key: string): Promise; /** * Check if a service is registered (either initialized or pending). * * @param key - Service key */ static has(key: string): boolean; /** * Check if a service is initialized and ready. * * @param key - Service key */ static isInitialized(key: string): boolean; /** * Get all initialized service keys. */ static getInitializedKeys(): string[]; /** * Get all pending (lazy) service keys. */ static getPendingKeys(): string[]; /** Build API client options and instance for service */ private static buildApiClientOptions; /** Build DB config and instance for service with runtime validation */ private static buildDbConfig; /** Build cache config and instance for service with runtime validation */ private static buildCacheConfig; /** Build observability config and instance for service */ private static buildObservabilityConfig; /** Build storage config and instance for service with runtime validation (backend-only) */ private static buildStorageConfig; /** Build notifications config and instance for service with runtime validation (backend-only) */ private static buildNotificationsConfig; /** * Create a dedicated ObservabilityService instance for a service. * Uses the same adapter pattern as global observability but with service-specific config. */ private static createDedicatedObservability; /** * Set the global observability adapter instance. * Called by Core.initialize() after creating the adapter. */ static setObservabilityInstance(instance: ObservabilityAdapter): void; /** * Get the global observability adapter instance. */ static getObservabilityInstance(): ObservabilityAdapter | null; /** * Initialize a single service from an entry. * Uses the service class's static create() method. * Merges per-service config with global config. */ private static initializeService; /** Build stores for service injection */ private static buildStoresForService; /** Perform the actual service initialization */ private static doInitializeService; /** * Dispose a specific service by key. * * @param key - Service key to dispose */ static dispose(key: string): void; /** * Dispose all services and clear the registry. */ static disposeAll(): void; /** * Check if a service is configured as singleton. * * @param key - Service key * @returns true if singleton (default), false if non-singleton */ static isSingleton(key: string): boolean; /** * Get all non-singleton service keys. * These services must be created via `create()` method. */ static getNonSingletonKeys(): string[]; /** * Register a new service dynamically after initialization. * * @param entry - Service entry to register * @param initNow - Whether to initialize immediately (default: true) * * @example * ```typescript * // Register and initialize immediately * await ServiceRegistry.register({ * service: NewDomainService, * config: { enabled: true }, * }); * * // Register for lazy initialization * ServiceRegistry.register({ * service: LazyService, * config: { enabled: true, initWhen: 'lazy' }, * }, false); * ``` */ static register(entry: CoreServiceEntry, initNow?: boolean): Promise; /** * Unregister a service (dispose if initialized). * * @param key - Service key to unregister */ static unregister(key: string): void; } //# sourceMappingURL=ServiceRegistry.d.ts.map