Fixed empty coroutines throwing a NRE

Closes #3
This commit is contained in:
Ellpeck 2020-05-19 16:06:38 +02:00
parent fa4d021368
commit a3af44becb
3 changed files with 8 additions and 1 deletions

View file

@ -11,7 +11,6 @@ namespace Coroutine {
internal ActiveCoroutine(IEnumerator<IWait> enumerator) { internal ActiveCoroutine(IEnumerator<IWait> enumerator) {
this.enumerator = enumerator; this.enumerator = enumerator;
this.enumerator.MoveNext();
} }
public bool Cancel() { public bool Cancel() {

View file

@ -9,6 +9,8 @@ namespace Coroutine {
private static readonly List<ActiveCoroutine> EventCoroutines = new List<ActiveCoroutine>(); private static readonly List<ActiveCoroutine> EventCoroutines = new List<ActiveCoroutine>();
public static ActiveCoroutine Start(IEnumerator<IWait> coroutine) { public static ActiveCoroutine Start(IEnumerator<IWait> coroutine) {
if (!coroutine.MoveNext())
return null;
var inst = new ActiveCoroutine(coroutine); var inst = new ActiveCoroutine(coroutine);
var type = inst.GetCurrentType(); var type = inst.GetCurrentType();
if (type == WaitType.Tick) if (type == WaitType.Tick)

View file

@ -12,6 +12,8 @@ namespace Test {
var seconds = CoroutineHandler.Start(WaitSeconds()); var seconds = CoroutineHandler.Start(WaitSeconds());
CoroutineHandler.Start(PrintEvery10Seconds(seconds)); CoroutineHandler.Start(PrintEvery10Seconds(seconds));
CoroutineHandler.Start(EmptyCoroutine());
CoroutineHandler.InvokeLater(new WaitSeconds(10), () => { CoroutineHandler.InvokeLater(new WaitSeconds(10), () => {
Console.WriteLine("Raising test event"); Console.WriteLine("Raising test event");
CoroutineHandler.RaiseEvent(TestEvent); CoroutineHandler.RaiseEvent(TestEvent);
@ -53,5 +55,9 @@ namespace Test {
} }
} }
private static IEnumerator<IWait> EmptyCoroutine() {
yield break;
}
} }
} }