refactor!: Major code cleanup for clearer state (#27)
.NET Build and Publish / build (push) Has been cancelled

* Quick pass

* no locks

* github actions

* publish via github actions

* pack

* Parent non-optional

* Generics are fun

* second pass

* Group workers, CTS, and Task in a class for simpler nullability

* pollysharp

* reverted cancellation changes

* reset state

* Update samples

* changes

* still working

* the fix

* private workers

* comments

* comments 2

* comments 3

* public worker count
This commit is contained in:
Jedd Morgan
2025-06-16 13:00:44 +01:00
committed by GitHub
parent 442b06dff7
commit a50809bc91
5 changed files with 178 additions and 133 deletions
@@ -1,10 +1,10 @@
using System.Windows.Forms;
using System.Windows.Forms;
using Grasshopper.Kernel;
using GrasshopperAsyncComponent;
namespace GrasshopperAsyncComponentDemo.SampleImplementations;
public class Sample_PrimeCalculatorAsyncComponent : GH_AsyncComponent
public class Sample_PrimeCalculatorAsyncComponent : GH_AsyncComponent<Sample_PrimeCalculatorAsyncComponent>
{
public override Guid ComponentGuid => new Guid("22C612B0-2C57-47CE-B9FE-E10621F18933");
@@ -46,25 +46,38 @@ public class Sample_PrimeCalculatorAsyncComponent : GH_AsyncComponent
);
}
private sealed class PrimeCalculatorWorker : WorkerInstance
private sealed class PrimeCalculatorWorker : WorkerInstance<Sample_PrimeCalculatorAsyncComponent>
{
private int TheNthPrime { get; set; } = 100;
private long ThePrime { get; set; } = -1;
public PrimeCalculatorWorker(
GH_Component? parent,
Sample_PrimeCalculatorAsyncComponent parent,
string id = "baseworker",
CancellationToken cancellationToken = default
)
: base(parent, id, cancellationToken) { }
public override void DoWork(Action<string, double> reportProgress, Action done)
public override Task DoWork(Action<string, double> reportProgress, Action done)
{
try
{
CalculatePrimes(reportProgress);
done();
}
catch (OperationCanceledException) when (CancellationToken.IsCancellationRequested)
{
//No need to call `done()` - GrasshopperAsyncComponent assumes immediate cancel,
//thus it has already performed clean-up actions that would normally be done on `done()`
}
return Task.CompletedTask;
}
public void CalculatePrimes(Action<string, double> reportProgress)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested)
{
return;
}
CancellationToken.ThrowIfCancellationRequested();
int count = 0;
long a = 2;
@@ -73,20 +86,14 @@ public class Sample_PrimeCalculatorAsyncComponent : GH_AsyncComponent
while (count < TheNthPrime)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested)
{
return;
}
CancellationToken.ThrowIfCancellationRequested();
long b = 2;
int prime = 1; // to check if found a prime
while (b * b <= a)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested)
{
return;
}
CancellationToken.ThrowIfCancellationRequested();
if (a % b == 0)
{
@@ -106,11 +113,12 @@ public class Sample_PrimeCalculatorAsyncComponent : GH_AsyncComponent
}
ThePrime = --a;
done();
}
public override WorkerInstance Duplicate(string id, CancellationToken cancellationToken) =>
new PrimeCalculatorWorker(Parent, id, cancellationToken);
public override WorkerInstance<Sample_PrimeCalculatorAsyncComponent> Duplicate(
string id,
CancellationToken cancellationToken
) => new PrimeCalculatorWorker(Parent, id, cancellationToken);
public override void GetData(IGH_DataAccess da, GH_ComponentParamServer parameters)
{
@@ -131,12 +139,6 @@ public class Sample_PrimeCalculatorAsyncComponent : GH_AsyncComponent
public override void SetData(IGH_DataAccess da)
{
// 👉 Checking for cancellation!
if (CancellationToken.IsCancellationRequested)
{
return;
}
da.SetData(0, ThePrime);
}
}