Browse Source

Fixed an issue with removing event-based coroutines after several event calls

Fixes #14
main
Ellpeck 1 year ago
parent
commit
07623ddc1a
  1. 11
      Coroutine/CoroutineHandlerInstance.cs
  2. 8
      Tests/EventBasedCoroutineTests.cs

11
Coroutine/CoroutineHandlerInstance.cs

@ -12,7 +12,7 @@ namespace Coroutine {
private readonly List<ActiveCoroutine> tickingCoroutines = new List<ActiveCoroutine>();
private readonly Dictionary<Event, List<ActiveCoroutine>> eventCoroutines = new Dictionary<Event, List<ActiveCoroutine>>();
private readonly HashSet<ActiveCoroutine> eventCoroutinesToRemove = new HashSet<ActiveCoroutine>();
private readonly HashSet<(Event, ActiveCoroutine)> eventCoroutinesToRemove = new HashSet<(Event, ActiveCoroutine)>();
private readonly HashSet<ActiveCoroutine> outstandingEventCoroutines = new HashSet<ActiveCoroutine>();
private readonly HashSet<ActiveCoroutine> outstandingTickingCoroutines = new HashSet<ActiveCoroutine>();
private readonly Stopwatch stopwatch = new Stopwatch();
@ -101,12 +101,13 @@ namespace Coroutine {
if (coroutines != null) {
for (var i = 0; i < coroutines.Count; i++) {
var c = coroutines[i];
if (this.eventCoroutinesToRemove.Contains(c))
var tup = (c.Event, c);
if (this.eventCoroutinesToRemove.Contains(tup))
continue;
if (c.OnEvent(evt)) {
this.eventCoroutinesToRemove.Add(c);
this.eventCoroutinesToRemove.Add(tup);
} else if (!c.IsWaitingForEvent) {
this.eventCoroutinesToRemove.Add(c);
this.eventCoroutinesToRemove.Add(tup);
this.outstandingTickingCoroutines.Add(c);
}
}
@ -125,7 +126,7 @@ namespace Coroutine {
// RemoveWhere is twice as fast as iterating and then clearing
if (this.eventCoroutinesToRemove.Count > 0) {
this.eventCoroutinesToRemove.RemoveWhere(c => {
this.GetEventCoroutines(c.Event, false).Remove(c);
this.GetEventCoroutines(c.Item1, false).Remove(c.Item2);
return true;
});
}

8
Tests/EventBasedCoroutineTests.cs

@ -302,9 +302,9 @@ namespace Tests {
}
[Test]
public void MovingCoroutineTest()
{
public void MovingCoroutineTest() {
var evt = new Event();
IEnumerator<Wait> MovingCoroutine() {
while (true) {
yield return new Wait(evt);
@ -312,7 +312,7 @@ namespace Tests {
}
}
CoroutineHandler.Start(MovingCoroutine(), "MovingCoroutine");
var moving = CoroutineHandler.Start(MovingCoroutine(), "MovingCoroutine");
CoroutineHandler.RaiseEvent(evt);
CoroutineHandler.RaiseEvent(evt);
CoroutineHandler.RaiseEvent(evt);
@ -336,6 +336,8 @@ namespace Tests {
CoroutineHandler.RaiseEvent(evt);
CoroutineHandler.Tick(1d);
CoroutineHandler.RaiseEvent(evt);
moving.Cancel();
}
}

Loading…
Cancel
Save