All files / Rindu/utils/configuration configurationManager.ts

87.71% Statements 50/57
75% Branches 9/12
83.33% Functions 10/12
88.88% Lines 48/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 13745x           45x   45x   47x 45x 66x 66x     66x 65x       69x 66x   69x       21x       65x 65x 65x 58x     7x         6x       6x 1x 1x 1x     5x 1x       4x   4x 4x 4x   1x 1x         32x 32x       32x   1x         34x         34x             8x         8x 8x       15x 15x       2x       4x         4x 4x                           45x   45x  
import { CURRENT_VERSION, migrations } from "./migrations";
import {
  CONFIGURATION_STORAGE_KEY,
  DEFAULT_CONFIG,
  isValidValue,
  validateConfig,
} from "./schema";
import type { Configuration, VersionedConfig } from "types/configuration";
import { isServer } from "utils/environment";
 
export class ConfigurationManager {
  private static instance: ConfigurationManager | null = null;
  private config: Configuration = DEFAULT_CONFIG;
  private readonly storageKey = CONFIGURATION_STORAGE_KEY;
 
  private constructor() {
    if (isServer()) return;
    this.config = this.loadConfig();
  }
 
  public static getInstance(): ConfigurationManager {
    if (!ConfigurationManager.instance) {
      ConfigurationManager.instance = new ConfigurationManager();
    }
    return ConfigurationManager.instance;
  }
 
  public static resetInstance(): void {
    ConfigurationManager.instance = null;
  }
 
  private loadConfig(): Configuration {
    try {
      const storedData = localStorage.getItem(this.storageKey);
      if (!storedData) {
        return DEFAULT_CONFIG;
      }
 
      const parsedData = JSON.parse(storedData) as
        | VersionedConfig
        | Configuration
        | null;
 
      Iif (parsedData === null || typeof parsedData !== "object") {
        return DEFAULT_CONFIG;
      }
 
      if (!("version" in parsedData)) {
        const migrated = migrations[0](parsedData);
        this.saveConfig(migrated);
        return migrated;
      }
 
      if (parsedData.version === CURRENT_VERSION) {
        return validateConfig(parsedData.data);
      }
 
      const migrationFunc =
        migrations[parsedData.version] || migrations["default"];
 
      const migrated = migrationFunc(parsedData.data);
      this.saveConfig(migrated);
      return migrated;
    } catch (error) {
      console.error("Error loading configuration:", error);
      return DEFAULT_CONFIG;
    }
  }
 
  private saveConfig(configToSave = this.config): void {
    try {
      const versionedConfig: VersionedConfig = {
        version: CURRENT_VERSION,
        data: configToSave,
      };
      localStorage.setItem(this.storageKey, JSON.stringify(versionedConfig));
    } catch (error) {
      console.error("Error saving configuration:", error);
    }
  }
 
  public get<K extends keyof Configuration>(key: K): Configuration[K] {
    Iif (!isValidValue(key, this.config[key])) {
      console.warn(`Invalid value found for ${key}, returning default value`);
      this.config[key] = DEFAULT_CONFIG[key];
      this.saveConfig();
    }
    return this.config[key];
  }
 
  public set<K extends keyof Configuration>(
    key: K,
    value: Configuration[K]
  ): void {
    const newConfig = {
      ...this.config,
      [key]: value,
    };
 
    this.config = validateConfig(newConfig);
    this.saveConfig();
  }
 
  public reset(): void {
    this.config = { ...DEFAULT_CONFIG };
    this.saveConfig();
  }
 
  public getAll(): Readonly<Configuration> {
    return { ...this.config };
  }
 
  public updateMultiple(updates: Partial<Configuration>): void {
    const newConfig = {
      ...this.config,
      ...updates,
    };
 
    this.config = validateConfig(newConfig);
    this.saveConfig();
  }
 
  public static getDefaultValue<K extends keyof Configuration>(
    key: K
  ): Configuration[K] {
    return DEFAULT_CONFIG[key];
  }
 
  public getVersion(): number {
    return CURRENT_VERSION;
  }
}
 
export const configuration = ConfigurationManager.getInstance();
 
export default ConfigurationManager;