< 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    {
 129611        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
 129623        public ComponentUpdateHandler(IDelayedComponent owner)
 24        {
 129625            this.owner = owner;
 129626            this.routine = null;
 27
 129628            yieldInstruction = new WaitForComponentUpdate(owner);
 129629        }
 30
 159431        public void ApplyChangesIfModified(BaseModel model) { HandleUpdate(model); }
 32
 33        protected void HandleUpdate(BaseModel newSerialization)
 34        {
 79735            queue.Enqueue(newSerialization);
 36
 79737            if (!isRoutineRunning)
 38            {
 79139                var enumerator = HandleUpdateCoroutines();
 40
 79141                if (enumerator != null)
 42                {
 79143                    routine = CoroutineStarter.Start(enumerator);
 44                }
 45            }
 79746        }
 47
 48        public void Stop()
 49        {
 75050            if (routine != null)
 451                CoroutineStarter.Stop(routine);
 52
 75053            routine = null;
 54#if UNITY_EDITOR
 75055            applyChangesRunning = false;
 56#endif
 75057        }
 58
 59        public void Cleanup()
 60        {
 75061            Stop();
 62
 75063            queue.Clear();
 75064        }
 65
 66        protected IEnumerator HandleUpdateCoroutines()
 67        {
 158468            while (queue.Count > 0)
 69            {
 79770                BaseModel model = queue.Dequeue();
 71
 79772                IEnumerator updateRoutine = ApplyChangesWrapper(model);
 73
 79774                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
 79778                    yield return updateRoutine;
 79                }
 80            }
 81
 78782            routine = null;
 78783        }
 84
 85        public virtual IEnumerator ApplyChangesWrapper(BaseModel model)
 86        {
 87#if UNITY_EDITOR
 79788            Assert.IsFalse(applyChangesRunning, "ApplyChanges routine was interrupted when it shouldn't!");
 79789            applyChangesRunning = true;
 90#endif
 79791            if (owner.IsValid())
 92            {
 79693                var enumerator = owner.ApplyChanges(model);
 94
 79695                if (enumerator != null)
 96                {
 23297                    yield return enumerator;
 98                }
 99            }
 100#if UNITY_EDITOR
 793101            applyChangesRunning = false;
 102#endif
 793103            owner.RaiseOnAppliedChanges();
 793104        }
 105    }
 106}