Skip to content

Commit

Permalink
Refactored Task to ValueTask (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvSapphire authored Dec 10, 2023
1 parent e27ab9b commit da96608
Show file tree
Hide file tree
Showing 39 changed files with 89 additions and 90 deletions.
2 changes: 1 addition & 1 deletion samples/PowerPipe.Sample/SamplePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ public class SamplePipelineContext : PipelineContext<SamplePipelineResult>
public int Test1 { get; set; }

public override SamplePipelineResult GetPipelineResult() => null;
};
}

2 changes: 1 addition & 1 deletion samples/PowerPipe.Sample/Steps/SampleGenericStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SampleGenericStep<T>: IPipelineStep<SamplePipelineContext>
{
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }

public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleGenericStep<T>)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
Expand Down
6 changes: 3 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps;

public class SampleParallelStep1 : IPipelineParallelStep<SamplePipelineContext>
{
public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep1)} Executed");
return Task.CompletedTask;
return ValueTask.CompletedTask;
}
}
}
6 changes: 3 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps;

public class SampleParallelStep2 : IPipelineParallelStep<SamplePipelineContext>
{
public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep2)} Executed");
return Task.CompletedTask;
return ValueTask.CompletedTask;
}
}
}
6 changes: 3 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps;

public class SampleParallelStep3 : IPipelineParallelStep<SamplePipelineContext>
{
public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep3)} Executed");
return Task.CompletedTask;
return ValueTask.CompletedTask;
}
}
}
6 changes: 3 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace PowerPipe.Sample.Steps;

public class SampleParallelStep4 : IPipelineParallelStep<SamplePipelineContext>
{
public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep4)} Executed");

throw new InvalidOperationException();

// return Task.CompletedTask;
// return ValueTask.CompletedTask;
}
}
}
6 changes: 3 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace PowerPipe.Sample.Steps;

public class SampleParallelStep5 : IPipelineParallelStep<SamplePipelineContext>
{
public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep5)} Executed");

// throw new InvalidOperationException();

return Task.CompletedTask;
return ValueTask.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps;

public class SampleParallelStep5Compensation : IPipelineCompensationStep<SamplePipelineContext>
{
public Task CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep5Compensation)} Executed");
return Task.CompletedTask;
return ValueTask.CompletedTask;
}
}
}
6 changes: 3 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps;

public class SampleParallelStep6 : IPipelineParallelStep<SamplePipelineContext>
{
public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep6)} Executed");
return Task.CompletedTask;
return ValueTask.CompletedTask;
}
}
}
6 changes: 3 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleParallelStep7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps;

public class SampleParallelStep7 : IPipelineParallelStep<SamplePipelineContext>
{
public Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleParallelStep7)} Executed");
return Task.CompletedTask;
return ValueTask.CompletedTask;
}
}
}
4 changes: 2 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps;
public class SampleStep1 : IPipelineStep<SamplePipelineContext>
{
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep1)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
}
6 changes: 3 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleStep1Compensation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps;

public class SampleStep1Compensation : IPipelineCompensationStep<SamplePipelineContext>
{
public Task CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep1Compensation)} Executed");
return Task.CompletedTask;
return ValueTask.CompletedTask;
}
}
}
4 changes: 2 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps;
public class SampleStep2 : IPipelineStep<SamplePipelineContext>
{
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep2)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
}
6 changes: 3 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleStep2Compensation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps;

public class SampleStep2Compensation : IPipelineCompensationStep<SamplePipelineContext>
{
public Task CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep2Compensation)} Executed");
return Task.CompletedTask;
return ValueTask.CompletedTask;
}
}
}
4 changes: 2 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps;
public class SampleStep3 : IPipelineStep<SamplePipelineContext>
{
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep3)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
}
4 changes: 2 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps;
public class SampleStep4 : IPipelineStep<SamplePipelineContext>
{
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep4)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
}
4 changes: 2 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps;
public class SampleStep5 : IPipelineStep<SamplePipelineContext>
{
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep5)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
}
4 changes: 2 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace PowerPipe.Sample.Steps;
public class SampleStep6 : IPipelineStep<SamplePipelineContext>
{
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep6)} Executed");
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
}
4 changes: 2 additions & 2 deletions samples/PowerPipe.Sample/Steps/SampleStep7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace PowerPipe.Sample.Steps;
public class SampleStep7 : IPipelineStep<SamplePipelineContext>
{
public IPipelineStep<SamplePipelineContext> NextStep { get; set; }
public async Task ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public async ValueTask ExecuteAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep7)} Executed");

// throw new InvalidOperationException();

await NextStep.ExecuteAsync(context, cancellationToken);
}
}
}
6 changes: 3 additions & 3 deletions samples/PowerPipe.Sample/Steps/SampleStep7Compensation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace PowerPipe.Sample.Steps;

public class SampleStep7Compensation : IPipelineCompensationStep<SamplePipelineContext>
{
public Task CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
public ValueTask CompensateAsync(SamplePipelineContext context, CancellationToken cancellationToken)
{
Console.WriteLine($"{nameof(SampleStep7Compensation)} Executed");
return Task.CompletedTask;
return ValueTask.CompletedTask;
}
}
}
4 changes: 2 additions & 2 deletions src/PowerPipe/Builder/Steps/AddIfElseStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal AddIfElseStep(Predicate<TContext> predicate, IPipelineStep<TContext> if
/// <param name="context">The context on which the if-else step operates.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>A task representing the asynchronous operation.</returns>
protected override async Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken)
protected override async ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken)
{
StepExecuted = true;

Expand All @@ -51,4 +51,4 @@ protected override async Task ExecuteInternalAsync(TContext context, Cancellatio
await _elseStep.ExecuteAsync(context, cancellationToken);
}
}
}
}
4 changes: 2 additions & 2 deletions src/PowerPipe/Builder/Steps/AddIfStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal AddIfStep(Predicate<TContext> predicate, IPipelineStep<TContext> step)
/// <param name="context">The context on which the if step operates.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>A task representing the asynchronous operation.</returns>
protected override async Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken)
protected override async ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken)
{
if (_predicate(context))
{
Expand All @@ -47,4 +47,4 @@ protected override async Task ExecuteInternalAsync(TContext context, Cancellatio
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
}
}
2 changes: 1 addition & 1 deletion src/PowerPipe/Builder/Steps/CompensationStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal CompensationStep(Func<IPipelineCompensationStep<TContext>> factory)
}

/// <inheritdoc/>
public async Task CompensateAsync(TContext context, CancellationToken cancellationToken)
public async ValueTask CompensateAsync(TContext context, CancellationToken cancellationToken)
{
IsCompensated = true;

Expand Down
4 changes: 2 additions & 2 deletions src/PowerPipe/Builder/Steps/IfPipelineStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public IfPipelineStep(Predicate<TContext> predicate, PipelineBuilder<TContext, T
/// <param name="context">The context on which the conditional pipeline step operates.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>A task representing the asynchronous operation.</returns>
protected override async Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken)
protected override async ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken)
{
if (_predicate(context))
{
Expand All @@ -45,4 +45,4 @@ protected override async Task ExecuteInternalAsync(TContext context, Cancellatio
if (NextStep is not null)
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
}
8 changes: 4 additions & 4 deletions src/PowerPipe/Builder/Steps/InternalStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void ConfigureErrorHandling(PipelineStepErrorHandling errorHandling, Time
}

/// <inheritdoc/>
public async Task ExecuteAsync(TContext context, CancellationToken cancellationToken)
public async ValueTask ExecuteAsync(TContext context, CancellationToken cancellationToken)
{
try
{
Expand Down Expand Up @@ -107,16 +107,16 @@ public async Task ExecuteAsync(TContext context, CancellationToken cancellationT
/// <param name="context">The context on which the step operates.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>A task representing the asynchronous operation.</returns>
protected virtual Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) =>
Task.CompletedTask;
protected virtual ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken) =>
ValueTask.CompletedTask;

/// <summary>
/// Handles an exception that occurred during the step's execution.
/// </summary>
/// <param name="context">The context on which the step operates.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>A task representing the asynchronous operation. Returns true if the exception is handled; otherwise, false.</returns>
protected virtual async Task<bool> HandleExceptionAsync(TContext context, CancellationToken cancellationToken)
protected virtual async ValueTask<bool> HandleExceptionAsync(TContext context, CancellationToken cancellationToken)
{
if (ErrorHandlingPredicate is not null && !ErrorHandlingPredicate(context))
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/PowerPipe/Builder/Steps/LazyStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ internal LazyStep(Func<IStepBase<TContext>> factory)
/// <param name="context">The context on which the step operates.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>A task representing the asynchronous operation.</returns>
protected override async Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken)
protected override async ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken)
{
StepExecuted = true;

await _step.Value.ExecuteAsync(context, cancellationToken);
}
}
}
4 changes: 2 additions & 2 deletions src/PowerPipe/Builder/Steps/ParallelStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ParallelStep(int maxDegreeOfParallelism, PipelineBuilder<TContext, TResul
/// <param name="context">The context on which the step operates.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>A task representing the asynchronous operation.</returns>
protected override async Task ExecuteInternalAsync(TContext context, CancellationToken cancellationToken)
protected override async ValueTask ExecuteInternalAsync(TContext context, CancellationToken cancellationToken)
{
StepExecuted = true;

Expand All @@ -47,4 +47,4 @@ protected override async Task ExecuteInternalAsync(TContext context, Cancellatio
if (NextStep is not null)
await NextStep.ExecuteAsync(context, cancellationToken);
}
}
}
Loading

0 comments on commit da96608

Please sign in to comment.