public void AddAsyncStep <TLocalIn, TLocalOut>( Func <TLocalIn, Task <TLocalOut> > stepFunc, int?localMaxDoP = null, bool?ensureOrdered = null, int?bufferSizeOverride = null) { if (Ready) { throw new InvalidOperationException(ExceptionMessages.InvalidAddError); } var options = GetExecuteStepOptions(localMaxDoP, ensureOrdered, bufferSizeOverride); var pipelineStep = new PipelineStep { IsAsync = true, StepIndex = StepCount++, }; if (Steps.Count == 0) { pipelineStep.Block = new TransformBlock <TLocalIn, Task <TLocalOut> >(stepFunc, options); Steps.Add(pipelineStep); } else { var lastStep = Steps.Last(); if (lastStep.IsAsync) { var step = new TransformBlock <Task <TLocalIn>, Task <TLocalOut> >( async(input) => stepFunc(await input), options); if (lastStep.Block is ISourceBlock <Task <TLocalIn> > targetBlock) { targetBlock.LinkTo(step, _linkStepOptions); pipelineStep.Block = step; Steps.Add(pipelineStep); } else { throw new InvalidOperationException(ExceptionMessages.InvalidStepFound); } } else { var step = new TransformBlock <TLocalIn, Task <TLocalOut> >(stepFunc, options); if (lastStep.Block is ISourceBlock <TLocalIn> targetBlock) { targetBlock.LinkTo(step, _linkStepOptions); pipelineStep.Block = step; Steps.Add(pipelineStep); } else { throw new InvalidOperationException(ExceptionMessages.InvalidStepFound); } } } }
public void Finalize(Func <TOut, Task> finalizeStep = null) { if (Ready) { throw new InvalidOperationException(ExceptionMessages.AlreadyFinalized); } if (Steps.Count == 0) { throw new InvalidOperationException(ExceptionMessages.CantFinalize); } if (finalizeStep != null) { var pipelineStep = new PipelineStep { IsAsync = true, StepIndex = StepCount++, IsLastStep = true, }; var lastStep = Steps.Last(); if (lastStep.IsAsync) { var step = new ActionBlock <Task <TOut> >( async t => await finalizeStep(await t), _executeStepOptions); if (lastStep.Block is ISourceBlock <Task <TOut> > targetBlock) { pipelineStep.Block = step; targetBlock.LinkTo(step, _linkStepOptions); Steps.Add(pipelineStep); } } else { var step = new ActionBlock <TOut>(t => finalizeStep(t), _executeStepOptions); if (lastStep.Block is ISourceBlock <TOut> targetBlock) { pipelineStep.Block = step; targetBlock.LinkTo(step, _linkStepOptions); Steps.Add(pipelineStep); } } } else { var lastStep = Steps.Last(); lastStep.IsLastStep = true; } Ready = true; }
public void Finalize(Action <TOut> finalizeStep) { if (Ready) { throw new InvalidOperationException(Utils.ExceptionMessages.AlreadyFinalized); } if (Steps.Count == 0) { throw new InvalidOperationException(Utils.ExceptionMessages.CantFinalize); } if (finalizeStep != null) { var pipelineStep = new PipelineStep { IsAsync = false, StepIndex = StepCount++, IsLastStep = true, }; var lastStep = Steps.Last(); if (lastStep.IsAsync) { var step = new ActionBlock <Task <TOut> >( async input => finalizeStep(await input.ConfigureAwait(false)), _executeStepOptions); if (lastStep.Block is ISourceBlock <Task <TOut> > targetBlock) { pipelineStep.Block = step; targetBlock.LinkTo(step, _linkStepOptions); Steps.Add(pipelineStep); } else { throw new InvalidOperationException(Utils.ExceptionMessages.InvalidStepFound); } } else { var step = new ActionBlock <TOut>( finalizeStep, _executeStepOptions); if (lastStep.Block is ISourceBlock <TOut> targetBlock) { pipelineStep.Block = step; targetBlock.LinkTo(step, _linkStepOptions); Steps.Add(pipelineStep); } else { throw new InvalidOperationException(Utils.ExceptionMessages.InvalidStepFound); } } } else { Steps.Last().IsLastStep = true; } Ready = true; }