mirror of
https://github.com/Ellpeck/Coroutine.git
synced 2024-11-21 21:33:29 +01:00
Fixed an issue with removing event-based coroutines after several event calls
Fixes #14
This commit is contained in:
parent
0d51954b63
commit
07623ddc1a
2 changed files with 11 additions and 8 deletions
|
@ -12,7 +12,7 @@ namespace Coroutine {
|
||||||
|
|
||||||
private readonly List<ActiveCoroutine> tickingCoroutines = new List<ActiveCoroutine>();
|
private readonly List<ActiveCoroutine> tickingCoroutines = new List<ActiveCoroutine>();
|
||||||
private readonly Dictionary<Event, List<ActiveCoroutine>> eventCoroutines = new Dictionary<Event, 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> outstandingEventCoroutines = new HashSet<ActiveCoroutine>();
|
||||||
private readonly HashSet<ActiveCoroutine> outstandingTickingCoroutines = new HashSet<ActiveCoroutine>();
|
private readonly HashSet<ActiveCoroutine> outstandingTickingCoroutines = new HashSet<ActiveCoroutine>();
|
||||||
private readonly Stopwatch stopwatch = new Stopwatch();
|
private readonly Stopwatch stopwatch = new Stopwatch();
|
||||||
|
@ -101,12 +101,13 @@ namespace Coroutine {
|
||||||
if (coroutines != null) {
|
if (coroutines != null) {
|
||||||
for (var i = 0; i < coroutines.Count; i++) {
|
for (var i = 0; i < coroutines.Count; i++) {
|
||||||
var c = coroutines[i];
|
var c = coroutines[i];
|
||||||
if (this.eventCoroutinesToRemove.Contains(c))
|
var tup = (c.Event, c);
|
||||||
|
if (this.eventCoroutinesToRemove.Contains(tup))
|
||||||
continue;
|
continue;
|
||||||
if (c.OnEvent(evt)) {
|
if (c.OnEvent(evt)) {
|
||||||
this.eventCoroutinesToRemove.Add(c);
|
this.eventCoroutinesToRemove.Add(tup);
|
||||||
} else if (!c.IsWaitingForEvent) {
|
} else if (!c.IsWaitingForEvent) {
|
||||||
this.eventCoroutinesToRemove.Add(c);
|
this.eventCoroutinesToRemove.Add(tup);
|
||||||
this.outstandingTickingCoroutines.Add(c);
|
this.outstandingTickingCoroutines.Add(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,7 +126,7 @@ namespace Coroutine {
|
||||||
// RemoveWhere is twice as fast as iterating and then clearing
|
// RemoveWhere is twice as fast as iterating and then clearing
|
||||||
if (this.eventCoroutinesToRemove.Count > 0) {
|
if (this.eventCoroutinesToRemove.Count > 0) {
|
||||||
this.eventCoroutinesToRemove.RemoveWhere(c => {
|
this.eventCoroutinesToRemove.RemoveWhere(c => {
|
||||||
this.GetEventCoroutines(c.Event, false).Remove(c);
|
this.GetEventCoroutines(c.Item1, false).Remove(c.Item2);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -302,9 +302,9 @@ namespace Tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void MovingCoroutineTest()
|
public void MovingCoroutineTest() {
|
||||||
{
|
|
||||||
var evt = new Event();
|
var evt = new Event();
|
||||||
|
|
||||||
IEnumerator<Wait> MovingCoroutine() {
|
IEnumerator<Wait> MovingCoroutine() {
|
||||||
while (true) {
|
while (true) {
|
||||||
yield return new Wait(evt);
|
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);
|
CoroutineHandler.RaiseEvent(evt);
|
||||||
CoroutineHandler.RaiseEvent(evt);
|
CoroutineHandler.RaiseEvent(evt);
|
||||||
|
@ -336,6 +336,8 @@ namespace Tests {
|
||||||
CoroutineHandler.RaiseEvent(evt);
|
CoroutineHandler.RaiseEvent(evt);
|
||||||
CoroutineHandler.Tick(1d);
|
CoroutineHandler.Tick(1d);
|
||||||
CoroutineHandler.RaiseEvent(evt);
|
CoroutineHandler.RaiseEvent(evt);
|
||||||
|
|
||||||
|
moving.Cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue