< 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:108
Line coverage:100% (39 of 39)
Covered branches:0
Total branches:0
Covered methods:10
Total methods:10
Method coverage:100% (10 of 10)

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    {
 130511        protected Queue<BaseModel> queue = new Queue<BaseModel>();
 620512        public Coroutine routine { get; protected set; }
 13
 14        public WaitForComponentUpdate yieldInstruction;
 15
 16        public IComponent owner;
 17
 92618        public bool isRoutineRunning { get { return routine != null; } }
 19
 20#if UNITY_EDITOR
 21        bool applyChangesRunning = false;
 22#endif
 130523        public ComponentUpdateHandler(IDelayedComponent owner)
 24        {
 130525            this.owner = owner;
 130526            this.routine = null;
 27
 130528            yieldInstruction = new WaitForComponentUpdate(owner);
 130529        }
 30
 183231        public void ApplyChangesIfModified(BaseModel model) { HandleUpdate(model); }
 32
 33        protected void HandleUpdate(BaseModel newSerialization)
 34        {
 91635            queue.Enqueue(newSerialization);
 36
 91637            if (!isRoutineRunning)
 38            {
 91139                var enumerator = HandleUpdateCoroutines();
 40
 91141                if (enumerator != null)
 42                {
 91143                    routine = CoroutineStarter.Start(enumerator);
 44                }
 45            }
 91646        }
 47
 48        public void Stop()
 49        {
 70050            if (routine != null)
 251                CoroutineStarter.Stop(routine);
 52
 70053            routine = null;
 54#if UNITY_EDITOR
 70055            applyChangesRunning = false;
 56#endif
 70057        }
 58
 59        public void Cleanup()
 60        {
 70061            Stop();
 62
 70063            queue.Clear();
 70064        }
 65
 66        protected IEnumerator HandleUpdateCoroutines()
 67        {
 182368            while (queue.Count > 0)
 69            {
 91670                BaseModel model = queue.Dequeue();
 71
 91672                IEnumerator updateRoutine = ApplyChangesWrapper(model);
 73
 91674                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
 91678                    yield return updateRoutine;
 79                }
 80            }
 81
 90782            routine = null;
 90783        }
 84
 85        protected virtual IEnumerator ApplyChangesWrapper(BaseModel model)
 86        {
 87#if UNITY_EDITOR
 91688            Assert.IsFalse(applyChangesRunning, "ApplyChanges routine was interrupted when it shouldn't!");
 91689            applyChangesRunning = true;
 90#endif
 91691            if (owner.IsValid())
 92            {
 91693                var enumerator = owner.ApplyChanges(model);
 94
 91695                if (enumerator != null)
 96                {
 35397                    yield return enumerator;
 98                }
 99            }
 100
 101#if UNITY_EDITOR
 912102            applyChangesRunning = false;
 103#endif
 104
 912105            owner.RaiseOnAppliedChanges();
 912106        }
 107    }
 108}