| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace 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 | | { |
| 0 | 26 | | 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 | | { |
| 0 | 45 | | return CoroutineStarter.Start(DCLCoroutineRunner.Run(enumerator, onException, null)); |
| | 46 | | } |
| | 47 | |
|
| | 48 | |
|
| | 49 | | public static IEnumerator WaitForAllIEnumerators(params IEnumerator[] coroutines) |
| | 50 | | { |
| 0 | 51 | | List<Coroutine> coroutineGroup = new List<Coroutine>(); |
| 0 | 52 | | for (int i = 0; i < coroutines.Length; i++) |
| | 53 | | { |
| 0 | 54 | | coroutineGroup.Add(CoroutineStarter.Start(coroutines[i])); |
| | 55 | | } |
| | 56 | |
|
| 0 | 57 | | int coroutineGroupCount = coroutineGroup.Count; |
| 0 | 58 | | for (int index = 0; index < coroutineGroupCount; index++) |
| | 59 | | { |
| 0 | 60 | | var coroutine = coroutineGroup[index]; |
| 0 | 61 | | yield return coroutine; |
| | 62 | | } |
| 0 | 63 | | } |
| | 64 | | } |
| | 65 | | } |