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

Fixes #14
This commit is contained in:
Ell 2021-04-08 19:31:11 +02:00
parent 0d51954b63
commit 07623ddc1a
2 changed files with 11 additions and 8 deletions

View file

@ -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;
});
}

View file

@ -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();
}
}