From 9f4d43d73012655998d0ebf37db1c8ce437a7ded Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 20 Mar 2021 01:50:00 +0100 Subject: [PATCH] clean up recent pull request a bit --- Coroutine/CoroutineHandlerInstance.cs | 33 ++++++++++++--------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/Coroutine/CoroutineHandlerInstance.cs b/Coroutine/CoroutineHandlerInstance.cs index 7bcc2bf..1392370 100644 --- a/Coroutine/CoroutineHandlerInstance.cs +++ b/Coroutine/CoroutineHandlerInstance.cs @@ -13,7 +13,7 @@ namespace Coroutine { private readonly List tickingCoroutines = new List(); private readonly List eventCoroutines = new List(); private readonly Queue outstandingCoroutines = new Queue(); - private readonly Dictionary eventcoroutinesIndexesToDelete = new Dictionary(); + private readonly ISet eventCoroutinesToRemove = new HashSet(); private readonly Stopwatch stopwatch = new Stopwatch(); /// @@ -66,11 +66,11 @@ namespace Coroutine { /// /// Ticks this coroutine handler, causing all time-based s to be ticked. + /// Note that this method needs to be called even if only event-based coroutines are used. /// /// The amount of seconds that have passed since the last time this method was invoked public void Tick(double deltaSeconds) { - this.removeEventCoroutines(); - this.AddOutstandingCoroutines(); + this.MoveOutstandingCoroutines(); this.tickingCoroutines.RemoveAll(c => { if (c.Tick(deltaSeconds)) { return true; @@ -88,12 +88,12 @@ namespace Coroutine { /// The event to raise public void RaiseEvent(Event evt) { for (var i = 0; i < this.eventCoroutines.Count; i++) { - var eventCoroutine = this.eventCoroutines[i]; - if (eventCoroutine.OnEvent(evt)) { - this.eventcoroutinesIndexesToDelete[i] = 1; - } else if (!eventCoroutine.IsWaitingForEvent()) { - this.outstandingCoroutines.Enqueue(eventCoroutine); - this.eventcoroutinesIndexesToDelete[i] = 1; + var c = this.eventCoroutines[i]; + if (c.OnEvent(evt)) { + this.eventCoroutinesToRemove.Add(i); + } else if (!c.IsWaitingForEvent()) { + this.outstandingCoroutines.Enqueue(c); + this.eventCoroutinesToRemove.Add(i); } } } @@ -106,7 +106,11 @@ namespace Coroutine { return this.tickingCoroutines.Concat(this.eventCoroutines); } - private void AddOutstandingCoroutines() { + private void MoveOutstandingCoroutines() { + var i = 0; + this.eventCoroutines.RemoveAll(c => this.eventCoroutinesToRemove.Contains(i++)); + this.eventCoroutinesToRemove.Clear(); + while (this.outstandingCoroutines.Count > 0) { var coroutine = this.outstandingCoroutines.Dequeue(); var list = coroutine.IsWaitingForEvent() ? this.eventCoroutines : this.tickingCoroutines; @@ -115,15 +119,6 @@ namespace Coroutine { } } - private void removeEventCoroutines() { - int counter = 0; - this.eventCoroutines.RemoveAll(c => { - return this.eventcoroutinesIndexesToDelete.ContainsKey(counter++); - }); - - this.eventcoroutinesIndexesToDelete.Clear(); - } - private static IEnumerator InvokeLaterImpl(Wait wait, Action action) { yield return wait; action();