added coroutine priority

Closes #6
This commit is contained in:
Ell 2021-03-16 19:07:28 +01:00
parent 5c6621ace3
commit c68121614f
4 changed files with 64 additions and 47 deletions

View file

@ -7,7 +7,7 @@ namespace Coroutine {
/// A reference to a currently running coroutine. /// A reference to a currently running coroutine.
/// This is returned by <see cref="CoroutineHandler.Start(IEnumerator{Wait},string)"/>. /// This is returned by <see cref="CoroutineHandler.Start(IEnumerator{Wait},string)"/>.
/// </summary> /// </summary>
public class ActiveCoroutine { public class ActiveCoroutine : IComparable<ActiveCoroutine> {
private readonly IEnumerator<Wait> enumerator; private readonly IEnumerator<Wait> enumerator;
private readonly Stopwatch stopwatch; private readonly Stopwatch stopwatch;
@ -52,11 +52,17 @@ namespace Coroutine {
/// When not specified on startup of this coroutine, the name defaults to an empty string. /// When not specified on startup of this coroutine, the name defaults to an empty string.
/// </summary> /// </summary>
public readonly string Name; public readonly string Name;
/// <summary>
/// The priority of this coroutine. The higher the priority, the earlier it is advanced compared to other coroutines that advance around the same time.
/// When not specified at startup of this coroutine, the priority defaults to 0.
/// </summary>
public readonly int Priority;
internal ActiveCoroutine(IEnumerator<Wait> enumerator, string name, Stopwatch stopwatch) { internal ActiveCoroutine(IEnumerator<Wait> enumerator, string name, int priority, Stopwatch stopwatch) {
this.enumerator = enumerator; this.enumerator = enumerator;
this.stopwatch = stopwatch;
this.Name = name; this.Name = name;
this.Priority = priority;
this.stopwatch = stopwatch;
} }
/// <summary> /// <summary>
@ -93,8 +99,7 @@ namespace Coroutine {
var result = this.enumerator.MoveNext(); var result = this.enumerator.MoveNext();
this.stopwatch.Stop(); this.stopwatch.Stop();
this.TotalMoveNextTime += this.stopwatch.Elapsed; this.TotalMoveNextTime += this.stopwatch.Elapsed;
if (this.stopwatch.Elapsed > this.MaxMoveNextTime) if (this.stopwatch.Elapsed > this.MaxMoveNextTime) {
{
this.MaxMoveNextTime = this.stopwatch.Elapsed; this.MaxMoveNextTime = this.stopwatch.Elapsed;
} }
this.MoveNextCount++; this.MoveNextCount++;
@ -118,5 +123,10 @@ namespace Coroutine {
/// <param name="coroutine">The coroutine that finished</param> /// <param name="coroutine">The coroutine that finished</param>
public delegate void FinishCallback(ActiveCoroutine coroutine); public delegate void FinishCallback(ActiveCoroutine coroutine);
/// <inheritdoc />
public int CompareTo(ActiveCoroutine other) {
return other.Priority.CompareTo(this.Priority);
}
} }
} }

View file

@ -15,19 +15,19 @@ namespace Coroutine {
/// <inheritdoc cref="CoroutineHandlerInstance.EventCount"/> /// <inheritdoc cref="CoroutineHandlerInstance.EventCount"/>
public static int EventCount => Instance.EventCount; public static int EventCount => Instance.EventCount;
/// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerable{Wait},string)"/> /// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerable{Wait},string,int)"/>
public static ActiveCoroutine Start(IEnumerable<Wait> coroutine, string name = "") { public static ActiveCoroutine Start(IEnumerable<Wait> coroutine, string name = "", int priority = 0) {
return Instance.Start(coroutine, name); return Instance.Start(coroutine, name, priority);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerator{Wait},string)"/> /// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerator{Wait},string,int)"/>
public static ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "") { public static ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) {
return Instance.Start(coroutine, name); return Instance.Start(coroutine, name, priority);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.InvokeLater"/> /// <inheritdoc cref="CoroutineHandlerInstance.InvokeLater"/>
public static ActiveCoroutine InvokeLater(Wait wait, Action action) { public static ActiveCoroutine InvokeLater(Wait wait, Action action, string name = "", int priority = 0) {
return Instance.InvokeLater(wait, action); return Instance.InvokeLater(wait, action, name, priority);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.Tick"/> /// <inheritdoc cref="CoroutineHandlerInstance.Tick"/>

View file

@ -28,27 +28,24 @@ namespace Coroutine {
/// Note that this calls <see cref="IEnumerable{T}.GetEnumerator"/> to get the enumerator. /// Note that this calls <see cref="IEnumerable{T}.GetEnumerator"/> to get the enumerator.
/// </summary> /// </summary>
/// <param name="coroutine">The coroutine to start</param> /// <param name="coroutine">The coroutine to start</param>
/// <param name="name">The name that this coroutine should have. Defaults to an empty string.</param> /// <param name="name">The <see cref="ActiveCoroutine.Name"/> that this coroutine should have. Defaults to an empty string.</param>
/// <param name="priority">The <see cref="ActiveCoroutine.Priority"/> that this coroutine should have. The higher the priority, the earlier it is advanced. Defaults to 0.</param>
/// <returns>An active coroutine object representing this coroutine</returns> /// <returns>An active coroutine object representing this coroutine</returns>
public ActiveCoroutine Start(IEnumerable<Wait> coroutine, string name = "") { public ActiveCoroutine Start(IEnumerable<Wait> coroutine, string name = "", int priority = 0) {
return this.Start(coroutine.GetEnumerator(), name); return this.Start(coroutine.GetEnumerator(), name, priority);
} }
/// <summary> /// <summary>
/// Starts the given coroutine, returning a <see cref="ActiveCoroutine"/> object for management. /// Starts the given coroutine, returning a <see cref="ActiveCoroutine"/> object for management.
/// </summary> /// </summary>
/// <param name="coroutine">The coroutine to start</param> /// <param name="coroutine">The coroutine to start</param>
/// <param name="name">The name that this coroutine should have. Defaults to an empty string.</param> /// <param name="name">The <see cref="ActiveCoroutine.Name"/> that this coroutine should have. Defaults to an empty string.</param>
/// <param name="priority">The <see cref="ActiveCoroutine.Priority"/> that this coroutine should have. The higher the priority, the earlier it is advanced compared to other coroutines that advance around the same time. Defaults to 0.</param>
/// <returns>An active coroutine object representing this coroutine</returns> /// <returns>An active coroutine object representing this coroutine</returns>
public ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "") { public ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) {
var inst = new ActiveCoroutine(coroutine, name, this.stopwatch); var inst = new ActiveCoroutine(coroutine, name, priority, this.stopwatch);
if (inst.MoveNext()) { if (inst.MoveNext())
if (inst.IsWaitingForEvent()) { AddSorted(inst.IsWaitingForEvent() ? this.eventCoroutines : this.tickingCoroutines, inst);
this.eventCoroutines.Add(inst);
} else {
this.tickingCoroutines.Add(inst);
}
}
return inst; return inst;
} }
@ -58,9 +55,11 @@ namespace Coroutine {
/// </summary> /// </summary>
/// <param name="wait">The wait to wait for</param> /// <param name="wait">The wait to wait for</param>
/// <param name="action">The action to execute after waiting</param> /// <param name="action">The action to execute after waiting</param>
/// <param name="name">The <see cref="ActiveCoroutine.Name"/> that the underlying coroutine should have. Defaults to an empty string.</param>
/// <param name="priority">The <see cref="ActiveCoroutine.Priority"/> that the underlying coroutine should have. The higher the priority, the earlier it is advanced compared to other coroutines that advance around the same time. Defaults to 0.</param>
/// <returns>An active coroutine object representing this coroutine</returns> /// <returns>An active coroutine object representing this coroutine</returns>
public ActiveCoroutine InvokeLater(Wait wait, Action action) { public ActiveCoroutine InvokeLater(Wait wait, Action action, string name = "", int priority = 0) {
return this.Start(InvokeLaterImpl(wait, action)); return this.Start(InvokeLaterImpl(wait, action), name, priority);
} }
/// <summary> /// <summary>
@ -68,15 +67,15 @@ 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) {
for (var i = this.tickingCoroutines.Count - 1; i >= 0; i--) { this.tickingCoroutines.RemoveAll(c => {
var coroutine = this.tickingCoroutines[i]; if (c.Tick(deltaSeconds)) {
if (coroutine.Tick(deltaSeconds)) { return true;
this.tickingCoroutines.RemoveAt(i); } else if (c.IsWaitingForEvent()) {
} else if (coroutine.IsWaitingForEvent()) { AddSorted(this.eventCoroutines, c);
this.tickingCoroutines.RemoveAt(i); return true;
this.eventCoroutines.Add(coroutine);
} }
} return false;
});
} }
/// <summary> /// <summary>
@ -84,15 +83,15 @@ 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) {
for (var i = this.eventCoroutines.Count - 1; i >= 0; i--) { this.eventCoroutines.RemoveAll(c => {
var coroutine = this.eventCoroutines[i]; if (c.OnEvent(evt)) {
if (coroutine.OnEvent(evt)) { return true;
this.eventCoroutines.RemoveAt(i); } else if (!c.IsWaitingForEvent()) {
} else if (!coroutine.IsWaitingForEvent()) { AddSorted(this.tickingCoroutines, c);
this.eventCoroutines.RemoveAt(i); return true;
this.tickingCoroutines.Add(coroutine);
} }
} return false;
});
} }
/// <summary> /// <summary>
@ -108,5 +107,10 @@ namespace Coroutine {
action(); action();
} }
private static void AddSorted(List<ActiveCoroutine> list, ActiveCoroutine coroutine) {
var position = list.BinarySearch(coroutine);
list.Insert(position < 0 ? ~position : position, coroutine);
}
} }
} }

View file

@ -20,6 +20,9 @@ namespace Test {
}); });
CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("Test event received")); CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("Test event received"));
CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("I am invoked after 'Test event received'"), priority: -5);
CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("I am invoked before 'Test event received'"), priority: 2);
var lastTime = DateTime.Now; var lastTime = DateTime.Now;
while (true) { while (true) {
var currTime = DateTime.Now; var currTime = DateTime.Now;
@ -51,9 +54,9 @@ namespace Test {
if (first.IsFinished) { if (first.IsFinished) {
Console.WriteLine("By the way, the first coroutine has finished!"); Console.WriteLine("By the way, the first coroutine has finished!");
Console.WriteLine($"{first.Name} data: {first.MoveNextCount} moves, " + Console.WriteLine($"{first.Name} data: {first.MoveNextCount} moves, " +
$"{first.TotalMoveNextTime.TotalMilliseconds} total time, " + $"{first.TotalMoveNextTime.TotalMilliseconds} total time, " +
$"{first.AverageMoveNextTime.TotalMilliseconds} average, " + $"{first.AverageMoveNextTime.TotalMilliseconds} average, " +
$"{first.MaxMoveNextTime.TotalMilliseconds} maximum"); $"{first.MaxMoveNextTime.TotalMilliseconds} maximum");
Environment.Exit(0); Environment.Exit(0);
} }
} }