|
| 1 | +using System; |
1 | 2 | using System.Collections; |
| 3 | +using CoroutineSubstitute.Utils; |
| 4 | +using UnityEngine; |
2 | 5 |
|
3 | 6 | namespace CoroutineSubstitute.Substitutes.Call |
4 | 7 | { |
5 | 8 | public class StartCoroutineCall : IStartCoroutineCall, IEnumerator |
6 | 9 | { |
7 | | - readonly IEnumerator enumerator; |
8 | | - |
9 | 10 | public int Id { get; } |
10 | | - public object Current => enumerator.Current; |
| 11 | + public object Current => nestedCoroutine ?? nestedCall?.Current ?? enumerator.Current; |
| 12 | + |
| 13 | + readonly IEnumerator enumerator; |
| 14 | + readonly IStartCoroutineCallFactory callFactory; |
| 15 | + IStartCoroutineCall nestedCall; |
| 16 | + Coroutine nestedCoroutine; |
11 | 17 |
|
12 | | - public StartCoroutineCall (int id, IEnumerator enumerator) |
| 18 | + public StartCoroutineCall ( |
| 19 | + int id, |
| 20 | + IEnumerator enumerator, |
| 21 | + IStartCoroutineCallFactory callFactory |
| 22 | + ) |
13 | 23 | { |
14 | 24 | Id = id; |
15 | 25 | this.enumerator = enumerator; |
| 26 | + this.callFactory = callFactory; |
| 27 | + } |
| 28 | + |
| 29 | + public bool MoveNext () |
| 30 | + { |
| 31 | + if (nestedCall != null) |
| 32 | + { |
| 33 | + if (nestedCall.MoveNext()) |
| 34 | + return true; |
| 35 | + nestedCall = null; |
| 36 | + } |
| 37 | + |
| 38 | + if (enumerator.MoveNext()) |
| 39 | + { |
| 40 | + if (Current is IEnumerator enumerator) |
| 41 | + nestedCall = callFactory.Create(enumerator); |
| 42 | + else if (Current is Coroutine coroutine) |
| 43 | + nestedCoroutine = coroutine; |
| 44 | + return true; |
| 45 | + } |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + public void SetNestedCoroutine (IStartCoroutineCall call) |
| 50 | + { |
| 51 | + if (nestedCall?.Current is Coroutine coroutine && coroutine.GetId() == call.Id) |
| 52 | + { |
| 53 | + nestedCall.SetNestedCoroutine(call); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (nestedCoroutine == null) |
| 58 | + throw new InvalidOperationException("Nested coroutine is not set"); |
| 59 | + |
| 60 | + if (nestedCoroutine.GetId() != call.Id) |
| 61 | + { |
| 62 | + throw new InvalidOperationException( |
| 63 | + $"Received Coroutine={call.Id}, expected Coroutine={nestedCoroutine.GetId()}" |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + nestedCall = call; |
| 68 | + nestedCoroutine = null; |
16 | 69 | } |
17 | 70 |
|
18 | | - public bool MoveNext () => enumerator.MoveNext(); |
19 | 71 | public void Reset () => enumerator.Reset(); |
20 | 72 | } |
21 | 73 | } |
0 commit comments