< Summary

Class:DCL.ComponentUpdateHandler
Assembly:DCL.Runtime.Interfaces
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/Interfaces/ComponentUpdateHandler.cs
Covered lines:39
Uncovered lines:0
Coverable lines:39
Total lines:106
Line coverage:100% (39 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/Interfaces/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    {
 100011        protected Queue<BaseModel> queue = new Queue<BaseModel>();
 525212        public Coroutine routine { get; protected set; }
 13
 14        public WaitForComponentUpdate yieldInstruction;
 15
 16        public IComponent owner;
 17
 90118        public bool isRoutineRunning { get { return routine != null; } }
 19
 20#if UNITY_EDITOR
 21        bool applyChangesRunning = false;
 22#endif
 100023        public ComponentUpdateHandler(IDelayedComponent owner)
 24        {
 100025            this.owner = owner;
 100026            this.routine = null;
 27
 100028            yieldInstruction = new WaitForComponentUpdate(owner);
 100029        }
 30
 180231        public void ApplyChangesIfModified(BaseModel model) { HandleUpdate(model); }
 32
 33        protected void HandleUpdate(BaseModel newSerialization)
 34        {
 90135            queue.Enqueue(newSerialization);
 36
 90137            if (!isRoutineRunning)
 38            {
 89539                var enumerator = HandleUpdateCoroutines();
 40
 89541                if (enumerator != null)
 42                {
 89543                    routine = CoroutineStarter.Start(enumerator);
 44                }
 45            }
 90146        }
 47
 48        public void Stop()
 49        {
 40650            if (routine != null)
 751                CoroutineStarter.Stop(routine);
 52
 40653            routine = null;
 54#if UNITY_EDITOR
 40655            applyChangesRunning = false;
 56#endif
 40657        }
 58
 59        public void Cleanup()
 60        {
 40661            Stop();
 62
 40663            queue.Clear();
 40664        }
 65
 66        protected IEnumerator HandleUpdateCoroutines()
 67        {
 178968            while (queue.Count > 0)
 69            {
 90170                BaseModel model = queue.Dequeue();
 71
 90172                IEnumerator updateRoutine = ApplyChangesWrapper(model);
 73
 90174                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
 90178                    yield return updateRoutine;
 79                }
 80            }
 81
 88882            routine = null;
 88883        }
 84
 85        public virtual IEnumerator ApplyChangesWrapper(BaseModel model)
 86        {
 87#if UNITY_EDITOR
 90188            Assert.IsFalse(applyChangesRunning, "ApplyChanges routine was interrupted when it shouldn't!");
 90189            applyChangesRunning = true;
 90#endif
 90191            if (owner.IsValid())
 92            {
 90093                var enumerator = owner.ApplyChanges(model);
 94
 90095                if (enumerator != null)
 96                {
 32697                    yield return enumerator;
 98                }
 99            }
 100#if UNITY_EDITOR
 894101            applyChangesRunning = false;
 102#endif
 894103            owner.RaiseOnAppliedChanges();
 894104        }
 105    }
 106}