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.
/// This is returned by <see cref="CoroutineHandler.Start(IEnumerator{Wait},string)"/>.
/// </summary>
public class ActiveCoroutine {
public class ActiveCoroutine : IComparable<ActiveCoroutine> {
private readonly IEnumerator<Wait> enumerator;
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.
/// </summary>
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.stopwatch = stopwatch;
this.Name = name;
this.Priority = priority;
this.stopwatch = stopwatch;
}
/// <summary>
@ -93,8 +99,7 @@ namespace Coroutine {
var result = this.enumerator.MoveNext();
this.stopwatch.Stop();
this.TotalMoveNextTime += this.stopwatch.Elapsed;
if (this.stopwatch.Elapsed > this.MaxMoveNextTime)
{
if (this.stopwatch.Elapsed > this.MaxMoveNextTime) {
this.MaxMoveNextTime = this.stopwatch.Elapsed;
}
this.MoveNextCount++;
@ -118,5 +123,10 @@ namespace Coroutine {
/// <param name="coroutine">The coroutine that finished</param>
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"/>
public static int EventCount => Instance.EventCount;
/// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerable{Wait},string)"/>
public static ActiveCoroutine Start(IEnumerable<Wait> coroutine, string name = "") {
return Instance.Start(coroutine, name);
/// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerable{Wait},string,int)"/>
public static ActiveCoroutine Start(IEnumerable<Wait> coroutine, string name = "", int priority = 0) {
return Instance.Start(coroutine, name, priority);
}
/// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerator{Wait},string)"/>
public static ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "") {
return Instance.Start(coroutine, name);
/// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerator{Wait},string,int)"/>
public static ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) {
return Instance.Start(coroutine, name, priority);
}
/// <inheritdoc cref="CoroutineHandlerInstance.InvokeLater"/>
public static ActiveCoroutine InvokeLater(Wait wait, Action action) {
return Instance.InvokeLater(wait, action);
public static ActiveCoroutine InvokeLater(Wait wait, Action action, string name = "", int priority = 0) {
return Instance.InvokeLater(wait, action, name, priority);
}
/// <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.
/// </summary>
/// <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>
public ActiveCoroutine Start(IEnumerable<Wait> coroutine, string name = "") {
return this.Start(coroutine.GetEnumerator(), name);
public ActiveCoroutine Start(IEnumerable<Wait> coroutine, string name = "", int priority = 0) {
return this.Start(coroutine.GetEnumerator(), name, priority);
}
/// <summary>
/// Starts the given coroutine, returning a <see cref="ActiveCoroutine"/> object for management.
/// </summary>
/// <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>
public ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "") {
var inst = new ActiveCoroutine(coroutine, name, this.stopwatch);
if (inst.MoveNext()) {
if (inst.IsWaitingForEvent()) {
this.eventCoroutines.Add(inst);
} else {
this.tickingCoroutines.Add(inst);
}
}
public ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) {
var inst = new ActiveCoroutine(coroutine, name, priority, this.stopwatch);
if (inst.MoveNext())
AddSorted(inst.IsWaitingForEvent() ? this.eventCoroutines : this.tickingCoroutines, inst);
return inst;
}
@ -58,9 +55,11 @@ namespace Coroutine {
/// </summary>
/// <param name="wait">The wait to wait for</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>
public ActiveCoroutine InvokeLater(Wait wait, Action action) {
return this.Start(InvokeLaterImpl(wait, action));
public ActiveCoroutine InvokeLater(Wait wait, Action action, string name = "", int priority = 0) {
return this.Start(InvokeLaterImpl(wait, action), name, priority);
}
/// <summary>
@ -68,15 +67,15 @@ namespace Coroutine {
/// </summary>
/// <param name="deltaSeconds">The amount of seconds that have passed since the last time this method was invoked</param>
public void Tick(double deltaSeconds) {
for (var i = this.tickingCoroutines.Count - 1; i >= 0; i--) {
var coroutine = this.tickingCoroutines[i];
if (coroutine.Tick(deltaSeconds)) {
this.tickingCoroutines.RemoveAt(i);
} else if (coroutine.IsWaitingForEvent()) {
this.tickingCoroutines.RemoveAt(i);
this.eventCoroutines.Add(coroutine);
}
this.tickingCoroutines.RemoveAll(c => {
if (c.Tick(deltaSeconds)) {
return true;
} else if (c.IsWaitingForEvent()) {
AddSorted(this.eventCoroutines, c);
return true;
}
return false;
});
}
/// <summary>
@ -84,15 +83,15 @@ namespace Coroutine {
/// </summary>
/// <param name="evt">The event to raise</param>
public void RaiseEvent(Event evt) {
for (var i = this.eventCoroutines.Count - 1; i >= 0; i--) {
var coroutine = this.eventCoroutines[i];
if (coroutine.OnEvent(evt)) {
this.eventCoroutines.RemoveAt(i);
} else if (!coroutine.IsWaitingForEvent()) {
this.eventCoroutines.RemoveAt(i);
this.tickingCoroutines.Add(coroutine);
}
this.eventCoroutines.RemoveAll(c => {
if (c.OnEvent(evt)) {
return true;
} else if (!c.IsWaitingForEvent()) {
AddSorted(this.tickingCoroutines, c);
return true;
}
return false;
});
}
/// <summary>
@ -108,5 +107,10 @@ namespace Coroutine {
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("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;
while (true) {
var currTime = DateTime.Now;