All files / modules localcfg.ts

98.07% Statements 51/52
100% Branches 16/16
100% Functions 11/11
97.95% Lines 48/49

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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 1895x 5x                                                         5x 40x 40x   40x           40x               40x 40x 32x                 40x 40x           40x             13x               2x                 43x                   13x 1x 1x 1x       12x 12x 12x       13x 10x               12x 11x 11x 11x                       5x 44x 44x               40x 40x 40x 40x   40x                 12x 12x 12x 12x                         48x 48x 48x 48x 47x     1x 1x      
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
 
/**
 * Represents a configurable value that can be stored in the configuration system.
 */
export type ConfigValue = string | number | boolean | object | null;
 
/**
 * Interface defining methods for storing and retrieving configuration elements.
 */
interface _ConfigStorage {
    /**
     * Stores a complete configuration element.
     * @param element - The name of the element to store.
     * @param data - An object representing the element's configuration data.
     */    
    setElement(element: string, data: { [key: string]: ConfigValue }): void;
 
    /**
     * Retrieves a configuration element.
     * @param element - The name of the element to retrieve.
     * @returns An object containing the configuration data, or `null` if the element does not exist.
     */
    getElement(element: string): { [key: string]: ConfigValue } | null;
}
 
/**
 * Represents a single configuration element, allowing for data manipulation and persistence.
 */
export class Configuration {
    private data: { [key: string]: ConfigValue } = {};
    private _dirty = false;
 
    private _onUpdated = new Subject <void> ();
 
    /**
     * An observable that emits whenever the configuration is updated.
     * Consumers can subscribe to react to updates.
     */    
    public OnUpdated = this._onUpdated.asObservable();
 
    /**
     * Loads the existing data for the configuration element from storage.
     * If no data exists, initializes an empty configuration.
     * @private
     */    
    private load(): void {
        const elementData = this.config.getElement(this.element);
        if (elementData) {
            this.data = elementData;
        }
    }
  
    /**
     * Initializes the `Configuration` instance and loads existing data for the element.
     * @param element - The name of the configuration element.
     * @param config - The storage service to use for persisting data.
     */    
    constructor(private element: string, private config: _ConfigStorage){
        this.load();
    }
  
    /**
     * Indicates whether changes to the configuration are saved automatically.
     */
    private autocommit = true;
 
    /**
     * Gets the `autoCommit` status.
     * @returns `true` if changes are saved automatically, otherwise `false`.
     */
    get autoCommit(): boolean{
        return this.autocommit;
    }
 
    /**
     * Sets the `autoCommit` status.
     * @param value - `true` to enable automatic saving of changes, `false` otherwise.
     */    
    set autoCommit(value: boolean) {
        this.autocommit = value;
    }
  
    /**
     * Retrieves the value of a specific configuration key.
     * @param item - The name of the configuration key.
     * @returns The value associated with the key, or `undefined` if it does not exist.
     */    
    get(item: string): ConfigValue {
        return this.data[item];
    }
 
    /**
     * Sets the value of a specific configuration key.
     * If the value is `null`, the key is removed from the configuration.
     * @param item - The name of the configuration key.
     * @param value - The value to assign to the key, or `null` to remove it.
     */    
    set(item: string, value: ConfigValue): void {
        if (value == null){
            if (item in this.data){
                this._dirty = true;
                delete this.data[item];
            }
        }
        else {
            if (this.data[item] != value){
                this.data[item] = value;
                this._dirty = true;
            }
        }
 
        if (this._dirty && this.autoCommit) {
            this.commit();
        }
    }
  
    /**
     * Saves all current configuration data to storage.
     */    
    commit(): void {
        if (this._dirty){
            this.config.setElement(this.element, this.data);
            this._dirty = false;
            this._onUpdated.next();
        }
    }
}
 
/**
 * Service for managing configuration elements, providing methods for
 * creating and interacting with `Configuration` instances.
 */
@Injectable({
    providedIn: 'root',
})
export class configService implements _ConfigStorage {
    private prefix = 'upp_cfg_';
    private _served = new Map <string, Configuration> ();
 
    /**
     * Retrieves a `Configuration` instance for a specific element.
     * @param element - The name of the configuration element.
     * @returns A `Configuration` instance for managing the element.
     */    
    getConfiguration(element: string): Configuration {
        let _served = this._served.get(element);
        if (!_served){
            _served = new Configuration(element, this);
            this._served.set(element, _served);
        }
        return _served
    }
 
    /**
     * Stores a complete configuration element in `localStorage`.
     * @param element - The name of the configuration element.
     * @param data - An object representing the element's configuration data.
     */    
    setElement(element: string, data: { [key: string]: ConfigValue }): void {
        const key = this.prefix + element;
        try {
            const serializedData = JSON.stringify(data);
            localStorage.setItem(key, serializedData);
        } 
        catch (error) {
            console.error(`[CONFIGURATION] Could not save element: "${element}":`, error);
        }
    }
 
    /**
     * Retrieves a configuration element from `localStorage`.
     * @param element - The name of the configuration element.
     * @returns An object containing the configuration data, or `null` if the element does not exist.
     */    
    getElement(element: string): { [key: string]: ConfigValue } | null {
        const key = this.prefix + element;
        try {
            const serializedData = localStorage.getItem(key);
            const parsedData = serializedData ? JSON.parse(serializedData) : null;
            return typeof parsedData === 'object' && parsedData !== null ? parsedData : null;
        } 
        catch (error) {
            console.error(`[CONFIGURATION] Could not retrieve element: "${element}":`, error);
            return null;
        }
    }
}