< Summary

Class:KernelConfig
Assembly:KernelConfiguration
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/KernelConfiguration/KernelConfig.cs
Covered lines:36
Uncovered lines:5
Coverable lines:41
Total lines:119
Line coverage:87.8% (36 of 41)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
KernelConfig()0%110100%
Set(...)0%5.025090%
Get()0%2100%
EnsureConfigInitialized()0%330100%
Initialize(...)0%550100%
Set(...)0%1.051062.5%
ClearPromises()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/KernelConfiguration/KernelConfig.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using DCL;
 3using UnityEngine;
 4using DCL.Helpers;
 5
 6public class KernelConfig
 7{
 8    public delegate void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous);
 9
 10    public event OnKernelConfigChanged OnChange;
 11
 12    public static KernelConfig i
 13    {
 14        get
 15        {
 202816            if (config == null)
 17            {
 118                config = new KernelConfig();
 19            }
 202820            return config;
 21        }
 22    }
 23    static KernelConfig config = null;
 24
 125    KernelConfigModel value = new KernelConfigModel();
 26    List<Promise<KernelConfigModel>> initializationPromises;
 27
 28    internal bool initialized = false;
 29
 30    /// <summary>
 31    /// Set a new Kernel Configuration
 32    /// </summary>
 33    /// <param name="newValue">New Configuration</param>
 34    public void Set(KernelConfigModel newValue)
 35    {
 636        if (newValue == null)
 37        {
 038            return;
 39        }
 40
 641        if (!initialized)
 42        {
 243            Initialize(newValue);
 44        }
 45
 646        if (newValue.Equals(value))
 147            return;
 48
 549        var previous = value;
 550        value = newValue;
 551        OnChange?.Invoke(value, previous);
 252    }
 53
 54    /// <summary>
 55    /// Get Kernel Configuration
 56    /// </summary>
 57    /// <returns>Kernel Configuration</returns>
 058    public KernelConfigModel Get() { return value; }
 59
 60    /// <summary>
 61    /// Get a promise that is resolved when KernelConfig is initialized.
 62    /// Usefeul when you need to fetch the configuration once and make sure it values were set and are not it defaults.
 63    /// i.e: EnsureConfigInitialized.Then(config => //do stuff)
 64    /// </summary>
 65    /// <returns>Promise with latest values</returns>
 66    public Promise<KernelConfigModel> EnsureConfigInitialized()
 67    {
 62468        var newPromise = new Promise<KernelConfigModel>();
 62469        if (initialized)
 70        {
 27571            newPromise.Resolve(value);
 27572        }
 73        else
 74        {
 34975            if (initializationPromises == null)
 76            {
 177                initializationPromises = new List<Promise<KernelConfigModel>>();
 78            }
 34979            initializationPromises.Add(newPromise);
 80        }
 62481        return newPromise;
 82    }
 83
 84    private void Initialize(KernelConfigModel newValue)
 85    {
 286        if (initializationPromises?.Count > 0)
 87        {
 488            for (int i = 0; i < initializationPromises.Count; i++)
 89            {
 190                initializationPromises[i].Resolve(newValue);
 91            }
 192            initializationPromises.Clear();
 193            initializationPromises = null;
 94        }
 95
 296        initialized = true;
 297    }
 98
 99    internal void Set(string json)
 100    {
 101        try
 102        {
 1103            var newConfig = value.Clone();
 1104            JsonUtility.FromJsonOverwrite(json, newConfig);
 1105            Set(newConfig);
 1106        }
 0107        catch (System.Exception e)
 108        {
 0109            Debug.LogError($"Error setting KernelConfig {e.Message}");
 0110        }
 1111    }
 112
 113    public void ClearPromises()
 114    {
 3115        initializationPromises?.Clear();
 2116    }
 117
 2118    private KernelConfig() { }
 119}