Compare commits

...

4 commits

Author SHA1 Message Date
Ell
7501ffaf9c 2.1.5 2023-02-23 19:11:38 +01:00
Ell
aa8a2b8ff9 made Coroutine trimmable 2023-02-23 19:11:10 +01:00
Ell
dc5206fced allow invoking later with an event directly 2023-02-23 19:08:26 +01:00
Ell
201369f167 code cleanup 2023-02-23 19:04:16 +01:00
7 changed files with 44 additions and 24 deletions

View file

@ -84,7 +84,7 @@ namespace Coroutine {
} }
internal bool OnEvent(Event evt) { internal bool OnEvent(Event evt) {
if (!this.WasCanceled && Equals(this.current.Event, evt)) if (!this.WasCanceled && object.Equals(this.current.Event, evt))
this.MoveNext(); this.MoveNext();
return this.IsFinished; return this.IsFinished;
} }

View file

@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;net6.0</TargetFrameworks> <TargetFrameworks>net45;netstandard2.0;net6.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
@ -13,7 +14,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile> <PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>Logo.png</PackageIcon> <PackageIcon>Logo.png</PackageIcon>
<VersionPrefix>2.1.4</VersionPrefix> <VersionPrefix>2.1.5</VersionPrefix>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View file

@ -11,42 +11,48 @@ namespace Coroutine {
private static readonly CoroutineHandlerInstance Instance = new CoroutineHandlerInstance(); private static readonly CoroutineHandlerInstance Instance = new CoroutineHandlerInstance();
/// <inheritdoc cref="CoroutineHandlerInstance.TickingCount"/> /// <inheritdoc cref="CoroutineHandlerInstance.TickingCount"/>
public static int TickingCount => Instance.TickingCount; public static int TickingCount => CoroutineHandler.Instance.TickingCount;
/// <inheritdoc cref="CoroutineHandlerInstance.EventCount"/> /// <inheritdoc cref="CoroutineHandlerInstance.EventCount"/>
public static int EventCount => Instance.EventCount; public static int EventCount => CoroutineHandler.Instance.EventCount;
/// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerable{Wait},string,int)"/> /// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerable{Wait},string,int)"/>
public static ActiveCoroutine Start(IEnumerable<Wait> coroutine, string name = "", int priority = 0) { public static ActiveCoroutine Start(IEnumerable<Wait> coroutine, string name = "", int priority = 0) {
return Instance.Start(coroutine, name, priority); return CoroutineHandler.Instance.Start(coroutine, name, priority);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerator{Wait},string,int)"/> /// <inheritdoc cref="CoroutineHandlerInstance.Start(IEnumerator{Wait},string,int)"/>
public static ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) { public static ActiveCoroutine Start(IEnumerator<Wait> coroutine, string name = "", int priority = 0) {
return Instance.Start(coroutine, name, priority); return CoroutineHandler.Instance.Start(coroutine, name, priority);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.InvokeLater"/> /// <inheritdoc cref="CoroutineHandlerInstance.InvokeLater(Wait,Action,string,int)"/>
public static ActiveCoroutine InvokeLater(Wait wait, Action action, string name = "", int priority = 0) { public static ActiveCoroutine InvokeLater(Wait wait, Action action, string name = "", int priority = 0) {
return Instance.InvokeLater(wait, action, name, priority); return CoroutineHandler.Instance.InvokeLater(wait, action, name, priority);
}
/// <inheritdoc cref="CoroutineHandlerInstance.InvokeLater(Event,Action,string,int)"/>
public static ActiveCoroutine InvokeLater(Event evt, Action action, string name = "", int priority = 0) {
return CoroutineHandler.Instance.InvokeLater(evt, action, name, priority);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.Tick(double)"/> /// <inheritdoc cref="CoroutineHandlerInstance.Tick(double)"/>
public static void Tick(double deltaSeconds) { public static void Tick(double deltaSeconds) {
Instance.Tick(deltaSeconds); CoroutineHandler.Instance.Tick(deltaSeconds);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.Tick(TimeSpan)"/> /// <inheritdoc cref="CoroutineHandlerInstance.Tick(TimeSpan)"/>
public static void Tick(TimeSpan delta) { public static void Tick(TimeSpan delta) {
Instance.Tick(delta); CoroutineHandler.Instance.Tick(delta);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.RaiseEvent"/> /// <inheritdoc cref="CoroutineHandlerInstance.RaiseEvent"/>
public static void RaiseEvent(Event evt) { public static void RaiseEvent(Event evt) {
Instance.RaiseEvent(evt); CoroutineHandler.Instance.RaiseEvent(evt);
} }
/// <inheritdoc cref="CoroutineHandlerInstance.GetActiveCoroutines"/> /// <inheritdoc cref="CoroutineHandlerInstance.GetActiveCoroutines"/>
public static IEnumerable<ActiveCoroutine> GetActiveCoroutines() { public static IEnumerable<ActiveCoroutine> GetActiveCoroutines() {
return Instance.GetActiveCoroutines(); return CoroutineHandler.Instance.GetActiveCoroutines();
} }
} }

View file

@ -75,7 +75,20 @@ namespace Coroutine {
/// <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> /// <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, string name = "", int priority = 0) { public ActiveCoroutine InvokeLater(Wait wait, Action action, string name = "", int priority = 0) {
return this.Start(InvokeLaterImpl(wait, action), name, priority); return this.Start(CoroutineHandlerInstance.InvokeLaterImpl(wait, action), name, priority);
}
/// <summary>
/// Causes the given action to be invoked after the given <see cref="Event"/>.
/// This is equivalent to a coroutine that waits for the given wait and then executes the given <see cref="Action"/>.
/// </summary>
/// <param name="evt">The event 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(Event evt, Action action, string name = "", int priority = 0) {
return this.InvokeLater(new Wait(evt), action, name, priority);
} }
/// <summary> /// <summary>

View file

@ -9,19 +9,19 @@ namespace Example {
private static readonly Event TestEvent = new Event(); private static readonly Event TestEvent = new Event();
public static void Main() { public static void Main() {
var seconds = CoroutineHandler.Start(WaitSeconds(), "Awesome Waiting Coroutine"); var seconds = CoroutineHandler.Start(Example.WaitSeconds(), "Awesome Waiting Coroutine");
CoroutineHandler.Start(PrintEvery10Seconds(seconds)); CoroutineHandler.Start(Example.PrintEvery10Seconds(seconds));
CoroutineHandler.Start(EmptyCoroutine()); CoroutineHandler.Start(Example.EmptyCoroutine());
CoroutineHandler.InvokeLater(new Wait(5), () => { CoroutineHandler.InvokeLater(new Wait(5), () => {
Console.WriteLine("Raising test event"); Console.WriteLine("Raising test event");
CoroutineHandler.RaiseEvent(TestEvent); CoroutineHandler.RaiseEvent(Example.TestEvent);
}); });
CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("Example event received")); CoroutineHandler.InvokeLater(new Wait(Example.TestEvent), () => Console.WriteLine("Example event received"));
CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("I am invoked after 'Example event received'"), priority: -5); CoroutineHandler.InvokeLater(new Wait(Example.TestEvent), () => Console.WriteLine("I am invoked after 'Example event received'"), priority: -5);
CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("I am invoked before 'Example event received'"), priority: 2); CoroutineHandler.InvokeLater(new Wait(Example.TestEvent), () => Console.WriteLine("I am invoked before 'Example event received'"), priority: 2);
var lastTime = DateTime.Now; var lastTime = DateTime.Now;
while (true) { while (true) {
@ -38,7 +38,7 @@ namespace Example {
Console.WriteLine("After 1 second " + DateTime.Now); Console.WriteLine("After 1 second " + DateTime.Now);
yield return new Wait(9); yield return new Wait(9);
Console.WriteLine("After 10 seconds " + DateTime.Now); Console.WriteLine("After 10 seconds " + DateTime.Now);
CoroutineHandler.Start(NestedCoroutine()); CoroutineHandler.Start(Example.NestedCoroutine());
yield return new Wait(5); yield return new Wait(5);
Console.WriteLine("After 5 more seconds " + DateTime.Now); Console.WriteLine("After 5 more seconds " + DateTime.Now);
yield return new Wait(10); yield return new Wait(10);

View file

@ -128,7 +128,7 @@ namespace Tests {
var p = CoroutineHandler.Start(Parent()); var p = CoroutineHandler.Start(Parent());
CoroutineHandler.RaiseEvent(onParentCreated); CoroutineHandler.RaiseEvent(onParentCreated);
// Nested corotuine starting in OnFinished. // Nested corotuine starting in OnFinished.
p.OnFinished += ac => { p.OnFinished += _ => {
CoroutineHandler.Start(Child()); CoroutineHandler.Start(Child());
CoroutineHandler.RaiseEvent(onChildCreated); CoroutineHandler.RaiseEvent(onChildCreated);
}; };

View file

@ -164,7 +164,7 @@ namespace Tests {
// Nested corotuine starting. // Nested corotuine starting.
var p = CoroutineHandler.Start(Parent()); var p = CoroutineHandler.Start(Parent());
// Nested corotuine starting in OnFinished. // Nested corotuine starting in OnFinished.
p.OnFinished += ac => CoroutineHandler.Start(Child()); p.OnFinished += _ => CoroutineHandler.Start(Child());
} }
var always = CoroutineHandler.Start(AlwaysRunning()); var always = CoroutineHandler.Start(AlwaysRunning());
@ -408,7 +408,7 @@ namespace Tests {
var p = CoroutineHandler.Start(Parent()); var p = CoroutineHandler.Start(Parent());
CoroutineHandler.RaiseEvent(onParentCreated); CoroutineHandler.RaiseEvent(onParentCreated);
// Nested corotuine starting in OnFinished. // Nested corotuine starting in OnFinished.
p.OnFinished += ac => { p.OnFinished += _ => {
CoroutineHandler.Start(Child()); CoroutineHandler.Start(Child());
CoroutineHandler.RaiseEvent(onChildCreated); CoroutineHandler.RaiseEvent(onChildCreated);
}; };