< Summary

Class:DCL.DCLCoroutineRunner
Assembly:CoroutineHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/CoroutineHelpers/DCLCoroutineRunner.cs
Covered lines:27
Uncovered lines:4
Coverable lines:31
Total lines:127
Line coverage:87% (27 of 31)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DCLCoroutineRunner()0%110100%
Run()0%12.3412086.67%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/CoroutineHelpers/DCLCoroutineRunner.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5
 6namespace DCL
 7{
 8    /// <summary>
 9    /// Based on work from: http://JacksonDunstan.com/articles/3718
 10    /// </summary>
 11    public static class DCLCoroutineRunner
 12    {
 117113        public static Func<float> realtimeSinceStartup = () => Time.realtimeSinceStartup;
 14
 15        /// <summary>
 16        /// Run an iterator function that might throw an exception. Call the callback with the exception
 17        /// if it does or null if it finishes without throwing an exception.
 18        /// </summary>
 19        /// <param name="enumerator">Iterator function to run</param>
 20        /// <param name="onException">Callback to call when the iterator has thrown an exception or finished.
 21        /// The thrown exception or null is passed as the parameter.</param>
 22        /// <param name="timeBudgetCounter">A func that takes elapsed time as parameter, and returns a bool
 23        /// indicating if a frame should be skipped or not. Use this in combination with ThrottlingCounter.EvaluateTimeB
 24        /// If this is passed as null, no time budget will be used.</param>
 25        /// <returns>An enumerator that runs the given enumerator</returns>
 26        public static IEnumerator Run(
 27            IEnumerator enumerator,
 28            Action<Exception> onException,
 29            Func<double, bool> timeBudgetCounter = null
 30        )
 31        {
 13932            float currentTime = realtimeSinceStartup();
 33            // The enumerator might yield return enumerators, in which case
 34            // we need to enumerate those here rather than yield-returning
 35            // them. Otherwise, any exceptions thrown by those "inner enumerators"
 36            // would actually escape to an outer level of iteration, outside this
 37            // code here, and not be passed to the done callback.
 38            // So, this stack holds any inner enumerators.
 13939            var stack = new Stack<IEnumerator>();
 13940            stack.Push(enumerator);
 41
 565942            while (stack.Count > 0)
 43            {
 44                // any inner enumerator will be at the top of the stack
 45                // otherwise the original one
 552546                var currentEnumerator = stack.Peek();
 47                // this is what get "yield returned" in the work enumerator
 48                object currentYieldedObject;
 49                // the contents of this try block run the work enumerator until
 50                // it gets to a yield return statement
 51                try
 52                {
 552553                    if (currentEnumerator.MoveNext() == false)
 54                    {
 55                        // in this case, the enumerator has finished
 128456                        stack.Pop();
 57                        // if the stack is empty, then everything has finished,
 58                        // and the while (stack.Count &gt; 0) will pick it up
 128459                        continue;
 60                    }
 61
 424162                    currentYieldedObject = currentEnumerator.Current;
 424163                }
 064                catch (Exception ex)
 65                {
 66                    // this part is the whole point of this method!
 067                    onException(ex);
 068                    yield break;
 69                }
 70
 424171                float newCurrentTime = realtimeSinceStartup();
 424172                float elapsedTime = newCurrentTime - currentTime;
 424173                currentTime = newCurrentTime;
 74
 75                // NOTE: SkipFrameIfDepletedTimeBudget object type is used as a special token here and will not
 76                // yield unless the time budget is exceeded for this frame.
 77                //
 78                // Handling the time budget frame skip for yield return null; calls was also considered.
 79                // But this means that yield return null; will no longer skip a frame unless the time budget
 80                // is exceeded. If the user wanted to skip the frame explicitly this detail would change
 81                // the intended behaviour and introduce bugs.
 424182                bool handleTimeBudget = timeBudgetCounter != null
 83                                        && currentYieldedObject != null
 84                                        && currentYieldedObject is SkipFrameIfDepletedTimeBudget;
 85
 424186                if ( handleTimeBudget )
 87                {
 304688                    if ( timeBudgetCounter( elapsedTime ) )
 89                    {
 18290                        yield return null;
 18291                        currentTime = realtimeSinceStartup();
 92                    }
 93
 18294                    continue;
 95                }
 96
 97                // in unity you can yield return whatever the hell you want,
 98                // so this will pick up whether it's something to enumerate
 99                // here, or pass through by yield returning it
 1195100                if (currentYieldedObject is IEnumerator)
 101                {
 1155102                    stack.Push(currentYieldedObject as IEnumerator);
 1155103                }
 104                else
 105                {
 40106                    yield return currentYieldedObject;
 35107                    currentTime = realtimeSinceStartup();
 108
 109                    // Force reset of time budget if a frame is skipped on purpose
 35110                    if ( timeBudgetCounter != null )
 0111                        timeBudgetCounter( double.MaxValue );
 112                }
 113            }
 134114        }
 115    }
 116
 117    /// <summary>
 118    /// When a coroutine is started with throttling, yielding this object
 119    /// will make the frame skip ONLY if the time budget is exceeded.
 120    ///
 121    /// If the time budget is not exceeded, no frames will be skipped by yielding this object.
 122    /// </summary>
 123    public class SkipFrameIfDepletedTimeBudget : CustomYieldInstruction
 124    {
 125        public override bool keepWaiting => false;
 126    }
 127}

Methods/Properties

DCLCoroutineRunner()
Run()