PipelineNet icon indicating copy to clipboard operation
PipelineNet copied to clipboard

Add cancellation tokens

Open mariusz96 opened this issue 1 year ago • 1 comments

I noticed that the library does not support CancellationTokens, like say, MediatR IPipelineBehaviors does: https://github.com/jbogard/MediatR/blob/v12.4.0/src/MediatR/IPipelineBehavior.cs.

This can be worked around with a custom class:

public class CustomClass
{
    public Bitmap Image { get; set; }
    public CancellationToken { get; set; }
}

And I cannot think of a way of adding them to core interfaces without breaking every IAsyncMiddleware<TParameter> implementation...

But still, it would be nice to be able to pass the CancellationToken directly:

IAsyncPipeline<TParameter> longRunningPipeline = CreatePipelineSomehow();

Bitmap image = (Bitmap) Image.FromFile("party-photo.png");

await longRunningPipeline.Execute(image, cancellationToken);

mariusz96 avatar Aug 07 '24 18:08 mariusz96

~~Oh, but with new interfaces this could work perhaps?~~

+ public interface ICancellebaleAsyncMiddleware<TParameter>
+ {
+     Task Run(TParameter parameter, Func<TParameter, Task> next, CancellationToken cancellationToken);
+ }
public interface IAsyncPipeline<TParameter>
{
+    IAsyncPipeline<TParameter> AddCancellebale<TMiddleware>() where TMiddleware : ICancellebaleAsyncMiddleware<TParameter>; // Does not compile with the same method name.
+    Task Execute(TParameter paramter, CancellationToken cancellationToken);
}

~~And for chains:~~

+ public interface ICancellebaleAsyncMiddleware<TParameter, TRetrun>
+ {
+     Task<TRetrun> Run(TParameter parameter, Func<TParameter, Task<TRetrun>> next, CancellationToken cancellationToken);
+ }
public interface IAsyncResponsibilityChain<TParameter, TReturn>
{
+    IAsyncResponsibilityChain<TParameter, TReturn> ChainCancellebale<TMiddleware>() where TMiddleware : ICancellebaleAsyncMiddleware<TParameter, TReturn>;
+    Task<TReturn> Execute(TParameter paramter, CancellationToken cancellationToken);
}

~~.~~

~~Then it would only break custom pipeline / chain implementations and inheritance. In core implementation, it could just convert one delegate to the other using default(CancellationToken) / CancellationToken.None.~~

What do you think, is it a worthwhile addition?

mariusz96 avatar Aug 07 '24 19:08 mariusz96