< Summary

Class:DCL.ComponentUpdateHandler
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ComponentUpdateHandler.cs
Covered lines:37
Uncovered lines:2
Coverable lines:39
Total lines:106
Line coverage:94.8% (37 of 39)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ComponentUpdateHandler(...)0%110100%
ApplyChangesIfModified(...)0%110100%
HandleUpdate(...)0%330100%
Stop()0%220100%
Cleanup()0%110100%
HandleUpdateCoroutines()0%550100%
ApplyChangesWrapper()0%550100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ComponentUpdateHandler.cs

#LineLine coverage
 1using DCL.Components;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using UnityEngine.Assertions;
 6
 7namespace DCL
 8{
 9    public class ComponentUpdateHandler
 10    {
 61211        protected Queue<BaseModel> queue = new Queue<BaseModel>();
 012        public Coroutine routine { get; protected set; }
 13
 14        public WaitForComponentUpdate yieldInstruction;
 15
 16        public IComponent owner;
 17
 018        public bool isRoutineRunning { get { return routine != null; } }
 19
 20#if UNITY_EDITOR
 21        bool applyChangesRunning = false;
 22#endif
 61223        public ComponentUpdateHandler(IDelayedComponent owner)
 24        {
 61225            this.owner = owner;
 61226            this.routine = null;
 27
 61228            yieldInstruction = new WaitForComponentUpdate(owner);
 61229        }
 30
 145831        public void ApplyChangesIfModified(BaseModel model) { HandleUpdate(model); }
 32
 33        protected void HandleUpdate(BaseModel newSerialization)
 34        {
 72935            queue.Enqueue(newSerialization);
 36
 72937            if (!isRoutineRunning)
 38            {
 72339                var enumerator = HandleUpdateCoroutines();
 40
 72341                if (enumerator != null)
 42                {
 72343                    routine = CoroutineStarter.Start(enumerator);
 44                }
 45            }
 72946        }
 47
 48        public void Stop()
 49        {
 7550            if (routine != null)
 351                CoroutineStarter.Stop(routine);
 52
 7553            routine = null;
 54#if UNITY_EDITOR
 7555            applyChangesRunning = false;
 56#endif
 7557        }
 58
 59        public void Cleanup()
 60        {
 7561            Stop();
 62
 7563            queue.Clear();
 7564        }
 65
 66        protected IEnumerator HandleUpdateCoroutines()
 67        {
 144968            while (queue.Count > 0)
 69            {
 72970                BaseModel model = queue.Dequeue();
 71
 72972                IEnumerator updateRoutine = ApplyChangesWrapper(model);
 73
 72974                if (updateRoutine != null)
 75                {
 76                    // we don't want to start coroutines if we have early finalization in IEnumerators
 77                    // ergo, we return null without yielding any result
 72978                    yield return updateRoutine;
 79                }
 80            }
 81
 72082            routine = null;
 72083        }
 84
 85        public virtual IEnumerator ApplyChangesWrapper(BaseModel model)
 86        {
 87#if UNITY_EDITOR
 72988            Assert.IsFalse(applyChangesRunning, "ApplyChanges routine was interrupted when it shouldn't!");
 72989            applyChangesRunning = true;
 90#endif
 72991            if (owner.IsValid())
 92            {
 72893                var enumerator = owner.ApplyChanges(model);
 94
 72895                if (enumerator != null)
 96                {
 22697                    yield return enumerator;
 98                }
 99            }
 100#if UNITY_EDITOR
 726101            applyChangesRunning = false;
 102#endif
 726103            owner.RaiseOnAppliedChanges();
 726104        }
 105    }
 106}