only move coroutines that relate to the required type

This commit is contained in:
Ell 2021-03-20 21:24:18 +01:00
parent 19e8a74d2b
commit 10590841d2

View file

@ -69,7 +69,7 @@ namespace Coroutine {
/// </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.MoveOutstandingCoroutines(); this.MoveOutstandingCoroutines(false);
this.tickingCoroutines.RemoveAll(c => { this.tickingCoroutines.RemoveAll(c => {
if (c.Tick(deltaSeconds)) { if (c.Tick(deltaSeconds)) {
return true; return true;
@ -95,7 +95,7 @@ namespace Coroutine {
/// </summary> /// </summary>
/// <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) {
this.MoveOutstandingCoroutines(); this.MoveOutstandingCoroutines(true);
var coroutines = this.GetEventCoroutines(evt, false); var coroutines = this.GetEventCoroutines(evt, false);
if (coroutines != null) { if (coroutines != null) {
for (var i = 0; i < coroutines.Count; i++) { for (var i = 0; i < coroutines.Count; i++) {
@ -120,19 +120,26 @@ namespace Coroutine {
return this.tickingCoroutines.Concat(this.eventCoroutines.Values.SelectMany(c => c)); return this.tickingCoroutines.Concat(this.eventCoroutines.Values.SelectMany(c => c));
} }
private void MoveOutstandingCoroutines() { private void MoveOutstandingCoroutines(bool evt) {
// 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) {
this.eventCoroutinesToRemove.RemoveWhere(c => { this.eventCoroutinesToRemove.RemoveWhere(c => {
this.GetEventCoroutines(c.Event, false).Remove(c); this.GetEventCoroutines(c.Event, false).Remove(c);
return true; return true;
}); });
}
if (this.outstandingCoroutines.Count > 0) {
this.outstandingCoroutines.RemoveWhere(c => { this.outstandingCoroutines.RemoveWhere(c => {
var list = c.IsWaitingForEvent ? this.GetEventCoroutines(c.Event, true) : this.tickingCoroutines; // we only want to enqueue coroutines that relate to the current type
if (c.IsWaitingForEvent != evt)
return false;
var list = evt ? this.GetEventCoroutines(c.Event, true) : this.tickingCoroutines;
var position = list.BinarySearch(c); var position = list.BinarySearch(c);
list.Insert(position < 0 ? ~position : position, c); list.Insert(position < 0 ? ~position : position, c);
return true; return true;
}); });
} }
}
private List<ActiveCoroutine> GetEventCoroutines(Event evt, bool create) { private List<ActiveCoroutine> GetEventCoroutines(Event evt, bool create) {
if (!this.eventCoroutines.TryGetValue(evt, out var ret) && create) { if (!this.eventCoroutines.TryGetValue(evt, out var ret) && create) {