clean up recent pull request a bit

This commit is contained in:
Ell 2021-03-20 01:50:00 +01:00
parent 47ffd9b79a
commit 9f4d43d730

View file

@ -13,7 +13,7 @@ namespace Coroutine {
private readonly List<ActiveCoroutine> tickingCoroutines = new List<ActiveCoroutine>(); private readonly List<ActiveCoroutine> tickingCoroutines = new List<ActiveCoroutine>();
private readonly List<ActiveCoroutine> eventCoroutines = new List<ActiveCoroutine>(); private readonly List<ActiveCoroutine> eventCoroutines = new List<ActiveCoroutine>();
private readonly Queue<ActiveCoroutine> outstandingCoroutines = new Queue<ActiveCoroutine>(); private readonly Queue<ActiveCoroutine> outstandingCoroutines = new Queue<ActiveCoroutine>();
private readonly Dictionary<int, byte> eventcoroutinesIndexesToDelete = new Dictionary<int, byte>(); private readonly ISet<int> eventCoroutinesToRemove = new HashSet<int>();
private readonly Stopwatch stopwatch = new Stopwatch(); private readonly Stopwatch stopwatch = new Stopwatch();
/// <summary> /// <summary>
@ -66,11 +66,11 @@ namespace Coroutine {
/// <summary> /// <summary>
/// Ticks this coroutine handler, causing all time-based <see cref="Wait"/>s to be ticked. /// Ticks this coroutine handler, causing all time-based <see cref="Wait"/>s to be ticked.
/// Note that this method needs to be called even if only event-based coroutines are used.
/// </summary> /// </summary>
/// <param name="deltaSeconds">The amount of seconds that have passed since the last time this method was invoked</param> /// <param name="deltaSeconds">The amount of seconds that have passed since the last time this method was invoked</param>
public void Tick(double deltaSeconds) { public void Tick(double deltaSeconds) {
this.removeEventCoroutines(); this.MoveOutstandingCoroutines();
this.AddOutstandingCoroutines();
this.tickingCoroutines.RemoveAll(c => { this.tickingCoroutines.RemoveAll(c => {
if (c.Tick(deltaSeconds)) { if (c.Tick(deltaSeconds)) {
return true; return true;
@ -88,12 +88,12 @@ namespace Coroutine {
/// <param name="evt">The event to raise</param> /// <param name="evt">The event to raise</param>
public void RaiseEvent(Event evt) { public void RaiseEvent(Event evt) {
for (var i = 0; i < this.eventCoroutines.Count; i++) { for (var i = 0; i < this.eventCoroutines.Count; i++) {
var eventCoroutine = this.eventCoroutines[i]; var c = this.eventCoroutines[i];
if (eventCoroutine.OnEvent(evt)) { if (c.OnEvent(evt)) {
this.eventcoroutinesIndexesToDelete[i] = 1; this.eventCoroutinesToRemove.Add(i);
} else if (!eventCoroutine.IsWaitingForEvent()) { } else if (!c.IsWaitingForEvent()) {
this.outstandingCoroutines.Enqueue(eventCoroutine); this.outstandingCoroutines.Enqueue(c);
this.eventcoroutinesIndexesToDelete[i] = 1; this.eventCoroutinesToRemove.Add(i);
} }
} }
} }
@ -106,7 +106,11 @@ namespace Coroutine {
return this.tickingCoroutines.Concat(this.eventCoroutines); 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) { while (this.outstandingCoroutines.Count > 0) {
var coroutine = this.outstandingCoroutines.Dequeue(); var coroutine = this.outstandingCoroutines.Dequeue();
var list = coroutine.IsWaitingForEvent() ? this.eventCoroutines : this.tickingCoroutines; 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<Wait> InvokeLaterImpl(Wait wait, Action action) { private static IEnumerator<Wait> InvokeLaterImpl(Wait wait, Action action) {
yield return wait; yield return wait;
action(); action();