< Summary

Class:KernelConfig
Assembly:KernelConfiguration
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/KernelConfiguration/KernelConfig.cs
Covered lines:34
Uncovered lines:5
Coverable lines:39
Total lines:112
Line coverage:87.1% (34 of 39)
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%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using UnityEngine;
 3using DCL.Helpers;
 4
 5public class KernelConfig
 6{
 7    public delegate void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous);
 8
 9    public event OnKernelConfigChanged OnChange;
 10
 11    public static KernelConfig i
 12    {
 13        get
 14        {
 140415            if (config == null)
 16            {
 117                config = new KernelConfig();
 18            }
 140419            return config;
 20        }
 21    }
 22    static KernelConfig config = null;
 23
 124    KernelConfigModel value = new KernelConfigModel();
 25    List<Promise<KernelConfigModel>> initializationPromises;
 26
 27    internal bool initialized = false;
 28
 29    /// <summary>
 30    /// Set a new Kernel Configuration
 31    /// </summary>
 32    /// <param name="newValue">New Configuration</param>
 33    public void Set(KernelConfigModel newValue)
 34    {
 635        if (newValue == null)
 36        {
 037            return;
 38        }
 39
 640        if (!initialized)
 41        {
 242            Initialize(newValue);
 43        }
 44
 645        if (newValue.Equals(value))
 146            return;
 47
 548        var previous = value;
 549        value = newValue;
 550        OnChange?.Invoke(value, previous);
 551    }
 52
 53    /// <summary>
 54    /// Get Kernel Configuration
 55    /// </summary>
 56    /// <returns>Kernel Configuration</returns>
 057    public KernelConfigModel Get() { return value; }
 58
 59    /// <summary>
 60    /// Get a promise that is resolved when KernelConfig is initialized.
 61    /// Usefeul when you need to fetch the configuration once and make sure it values were set and are not it defaults.
 62    /// i.e: EnsureConfigInitialized.Then(config => //do stuff)
 63    /// </summary>
 64    /// <returns>Promise with latest values</returns>
 65    public Promise<KernelConfigModel> EnsureConfigInitialized()
 66    {
 34367        var newPromise = new Promise<KernelConfigModel>();
 34368        if (initialized)
 69        {
 18270            newPromise.Resolve(value);
 18271        }
 72        else
 73        {
 16174            if (initializationPromises == null)
 75            {
 276                initializationPromises = new List<Promise<KernelConfigModel>>();
 77            }
 16178            initializationPromises.Add(newPromise);
 79        }
 34380        return newPromise;
 81    }
 82
 83    private void Initialize(KernelConfigModel newValue)
 84    {
 285        if (initializationPromises?.Count > 0)
 86        {
 32687            for (int i = 0; i < initializationPromises.Count; i++)
 88            {
 16189                initializationPromises[i].Resolve(newValue);
 90            }
 291            initializationPromises.Clear();
 292            initializationPromises = null;
 93        }
 294        initialized = true;
 295    }
 96
 97    internal void Set(string json)
 98    {
 99        try
 100        {
 1101            var newConfig = value.Clone();
 1102            JsonUtility.FromJsonOverwrite(json, newConfig);
 1103            Set(newConfig);
 1104        }
 0105        catch (System.Exception e)
 106        {
 0107            Debug.LogError($"Error setting KernelConfig {e.Message}");
 0108        }
 1109    }
 110
 2111    private KernelConfig() { }
 112}