diff --git a/Coroutine/CoroutineHandlerInstance.cs b/Coroutine/CoroutineHandlerInstance.cs index 886416f..87f063a 100644 --- a/Coroutine/CoroutineHandlerInstance.cs +++ b/Coroutine/CoroutineHandlerInstance.cs @@ -13,7 +13,8 @@ namespace Coroutine { private readonly List tickingCoroutines = new List(); private readonly Dictionary> eventCoroutines = new Dictionary>(); private readonly HashSet eventCoroutinesToRemove = new HashSet(); - private readonly HashSet outstandingCoroutines = new HashSet(); + private readonly HashSet outstandingEventCoroutines = new HashSet(); + private readonly HashSet outstandingTickingCoroutines = new HashSet(); private readonly Stopwatch stopwatch = new Stopwatch(); /// @@ -47,7 +48,7 @@ namespace Coroutine { public ActiveCoroutine Start(IEnumerator coroutine, string name = "", int priority = 0) { var inst = new ActiveCoroutine(coroutine, name, priority, this.stopwatch); if (inst.MoveNext()) - this.outstandingCoroutines.Add(inst); + this.GetOutstandingCoroutines(inst.IsWaitingForEvent).Add(inst); return inst; } @@ -74,7 +75,7 @@ namespace Coroutine { if (c.Tick(deltaSeconds)) { return true; } else if (c.IsWaitingForEvent) { - this.outstandingCoroutines.Add(c); + this.outstandingEventCoroutines.Add(c); return true; } return false; @@ -106,7 +107,7 @@ namespace Coroutine { this.eventCoroutinesToRemove.Add(c); } else if (!c.IsWaitingForEvent) { this.eventCoroutinesToRemove.Add(c); - this.outstandingCoroutines.Add(c); + this.outstandingTickingCoroutines.Add(c); } } } @@ -128,12 +129,10 @@ namespace Coroutine { return true; }); } - if (this.outstandingCoroutines.Count > 0) { - this.outstandingCoroutines.RemoveWhere(c => { - // we only want to enqueue coroutines that relate to the current type - if (c.IsWaitingForEvent != evt) - return false; - var list = evt ? this.GetEventCoroutines(c.Event, true) : this.tickingCoroutines; + var coroutines = this.GetOutstandingCoroutines(evt); + if (coroutines.Count > 0) { + coroutines.RemoveWhere(c => { + var list = c.IsWaitingForEvent ? this.GetEventCoroutines(c.Event, true) : this.tickingCoroutines; var position = list.BinarySearch(c); list.Insert(position < 0 ? ~position : position, c); return true; @@ -141,6 +140,10 @@ namespace Coroutine { } } + private HashSet GetOutstandingCoroutines(bool evt) { + return evt ? this.outstandingEventCoroutines : this.outstandingTickingCoroutines; + } + private List GetEventCoroutines(Event evt, bool create) { if (!this.eventCoroutines.TryGetValue(evt, out var ret) && create) { ret = new List();