< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
StartThrottledCoroutine(...)0%2100%
StartThrowingCoroutine(...)0%2100%
WaitForAllIEnumerators()0%30500%
RunCoroutineSync(...)0%20400%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5
 6namespace DCL
 7{
 8    public static class CoroutineUtils
 9    {
 10        /// <summary>
 11        /// Start a coroutine that might throw an exception. Call the callback with the exception if it
 12        /// does or null if it finishes without throwing an exception.
 13        /// </summary>
 14        /// <param name="monoBehaviour">MonoBehaviour to start the coroutine on</param>
 15        /// <param name="enumerator">Iterator function to run as the coroutine</param>
 16        /// <param name="onException">Callback to call when the coroutine has thrown an exception or finished.
 17        /// The thrown exception or null is passed as the parameter.</param>
 18        /// <returns>The started coroutine</returns>
 19        public static Coroutine StartThrottledCoroutine(
 20            this MonoBehaviour monoBehaviour,
 21            IEnumerator enumerator,
 22            Action<Exception> onException,
 23            Func<double, bool> timeBudgetCounter
 24        )
 25        {
 026            return CoroutineStarter.Start(DCLCoroutineRunner.Run(enumerator, onException, timeBudgetCounter));
 27        }
 28
 29
 30        /// <summary>
 31        /// Start a coroutine that might throw an exception. Call the callback with the exception if it
 32        /// does or null if it finishes without throwing an exception.
 33        /// </summary>
 34        /// <param name="monoBehaviour">MonoBehaviour to start the coroutine on</param>
 35        /// <param name="enumerator">Iterator function to run as the coroutine</param>
 36        /// <param name="onException">Callback to call when the coroutine has thrown an exception or finished.
 37        /// The thrown exception or null is passed as the parameter.</param>
 38        /// <returns>The started coroutine</returns>
 39        public static Coroutine StartThrowingCoroutine(
 40            this MonoBehaviour monoBehaviour,
 41            IEnumerator enumerator,
 42            Action<Exception> onException
 43        )
 44        {
 045            return CoroutineStarter.Start(DCLCoroutineRunner.Run(enumerator, onException, null));
 46        }
 47
 48
 49        public static IEnumerator WaitForAllIEnumerators(params IEnumerator[] coroutines)
 50        {
 051            List<Coroutine> coroutineGroup = new List<Coroutine>();
 052            for (int i = 0; i < coroutines.Length; i++)
 53            {
 054                coroutineGroup.Add(CoroutineStarter.Start(coroutines[i]));
 55            }
 56
 057            int coroutineGroupCount = coroutineGroup.Count;
 058            for (int index = 0; index < coroutineGroupCount; index++)
 59            {
 060                var coroutine = coroutineGroup[index];
 061                yield return coroutine;
 62            }
 063        }
 64
 65        /// <summary>
 66        /// There are some cases when we need to run a coroutine syncronously for some editor scripts so we use this
 67        /// </summary>
 68        /// <param name="coroutine"></param>
 69        public static void RunCoroutineSync(IEnumerator coroutine)
 70        {
 071            var stack = new Stack<IEnumerator>();
 072            stack.Push(coroutine);
 073            while (stack.Count > 0)
 74            {
 075                var enumerator = stack.Pop();
 076                if (enumerator.MoveNext())
 77                {
 078                    stack.Push(enumerator);
 079                    if (enumerator.Current is IEnumerator subEnumerator)
 80                    {
 081                        stack.Push(subEnumerator);
 82                    }
 83                }
 84            }
 085        }
 86    }
 87}