Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/services/config-registry.ts is written in an unsupported language. File is not indexed.

0001 import { Injectable } from '@angular/core';
0002 import {ServerConfig, ServerConfigService} from './server-config.service';
0003 import { BehaviorSubject, Observable } from 'rxjs';
0004 
0005 /**
0006  * Base interface for all configuration types
0007  */
0008 export interface BaseConfig {
0009   ConfigType: string;
0010   ConfigName: string;
0011 }
0012 
0013 /**
0014  * Schema definition for a configuration type
0015  */
0016 export interface ConfigSchema<T extends BaseConfig> {
0017   type: string;
0018   validate: (config: any) => boolean;
0019   createDefault: (name?: string) => T;
0020   normalize: (partialConfig: Partial<T>) => T;
0021 }
0022 
0023 /**
0024  * Global registry of configuration schemas
0025  * This is used by the decorator to register schemas
0026  */
0027 export const GLOBAL_CONFIG_SCHEMAS: Map<string, ConfigSchema<any>> = new Map();
0028 
0029 /**
0030  * Decorator for registering a configuration schema
0031  * @param schema The configuration schema to register
0032  */
0033 export function RegisterConfigSchema<T extends BaseConfig>(schema: ConfigSchema<T>) {
0034   return function(target: any) {
0035     // Register the schema globally
0036     GLOBAL_CONFIG_SCHEMAS.set(schema.type, schema);
0037     console.log(`Registered config schema: ${schema.type}`);
0038     return target;
0039   };
0040 }
0041 
0042 /**
0043  * Central registry for subsystem configurations
0044  */
0045 @Injectable({
0046   providedIn: 'root'
0047 })
0048 export class ConfigRegistry {
0049   private serverConfig: ServerConfig;
0050 
0051   constructor(private serverConfigService: ServerConfigService) {
0052     // Subscribe to config changes from the server config service
0053     this.serverConfig = serverConfigService.config;
0054   }
0055 
0056   /**
0057    * Get all registered configuration schemas
0058    */
0059   getRegisteredSchemas(): Map<string, ConfigSchema<any>> {
0060     return GLOBAL_CONFIG_SCHEMAS;
0061   }
0062 
0063   /**
0064    * Get a specific schema by type
0065    */
0066   getSchema<T extends BaseConfig>(configType: string): ConfigSchema<T> | undefined {
0067     return GLOBAL_CONFIG_SCHEMAS.get(configType) as ConfigSchema<T> | undefined;
0068   }
0069 
0070 
0071   /**
0072    * Gets all available configurations of a specific type
0073    */
0074   getConfigs<T extends BaseConfig>(configType: string): T[] {
0075     const schema = this.getSchema<T>(configType);
0076     if (!schema) {
0077       console.warn(`No schema registered for config type: ${configType}`);
0078       return [];
0079     }
0080 
0081     const allConfigs = this.serverConfigService.config.configs || [];
0082     const filteredConfigs = allConfigs.filter(config =>
0083       typeof config === 'object' &&
0084       config !== null &&
0085       config.ConfigType === configType
0086     );
0087 
0088     // Normalize and validate each config using the registered schema
0089     return filteredConfigs
0090       .filter(config => schema.validate(config))
0091       .map(config => schema.normalize(config as Partial<T>));
0092   }
0093 
0094   /**
0095    * Gets a specific configuration by type and name
0096    */
0097   getConfigByName<T extends BaseConfig>(configType: string, configName: string): T | undefined {
0098     const configs = this.getConfigs<T>(configType);
0099     return configs.find(config => config.ConfigName === configName);
0100   }
0101 
0102   /**
0103    * Gets all registered configuration types
0104    */
0105   getConfigTypes(): string[] {
0106     return Array.from(GLOBAL_CONFIG_SCHEMAS.keys());
0107   }
0108 
0109   /**
0110    * Gets configuration names for a specific type
0111    */
0112   getConfigNames(configType: string): string[] {
0113     const configs = this.getConfigs(configType);
0114     return configs.map(config => config.ConfigName);
0115   }
0116 
0117   /**
0118    * Creates a new configuration instance with default values
0119    */
0120   createConfig<T extends BaseConfig>(configType: string, configName: string): T | undefined {
0121     const schema = this.getSchema<T>(configType);
0122     if (!schema) {
0123       console.warn(`No schema registered for config type: ${configType}`);
0124       return undefined;
0125     }
0126 
0127     return schema.createDefault(configName);
0128   }
0129 }