From 821c37d205dfcfaba5640327005e17c320001ba1 Mon Sep 17 00:00:00 2001 From: Matt Edwards Date: Thu, 31 Oct 2024 14:35:12 -0400 Subject: [PATCH] WIP: mostly working --- .../Binders/Abstractions/Binder.cs | 2 - .../Binders/Abstractions/StatementBinder.cs | 37 +++++++++++++------ .../Binders/CallBlockBinder.cs | 26 +++++++------ .../Binders/CallStatementBinder.cs | 28 ++++++-------- src/Hyperbee.Pipeline/Binders/HookBinder.cs | 6 +-- .../Binders/PipeBlockBinder.cs | 4 ++ .../Binders/PipeStatementBinder.cs | 2 - .../Builders/PipeStatementBuilder.cs | 13 ++++++- .../Hyperbee.Pipeline.csproj | 2 +- src/Hyperbee.Pipeline/PipelineBuilder.cs | 1 - .../Hyperbee.Pipeline.Auth.Tests.csproj | 6 +-- .../Hyperbee.Pipeline.Tests.csproj | 4 +- .../PipelineEnumerationTests.cs | 2 +- .../PipelineExecutionTests.cs | 2 - .../Hyperbee.Pipeline.Caching.Tests.csproj | 6 +-- 15 files changed, 80 insertions(+), 61 deletions(-) diff --git a/src/Hyperbee.Pipeline/Binders/Abstractions/Binder.cs b/src/Hyperbee.Pipeline/Binders/Abstractions/Binder.cs index 342a1c9..35e616a 100644 --- a/src/Hyperbee.Pipeline/Binders/Abstractions/Binder.cs +++ b/src/Hyperbee.Pipeline/Binders/Abstractions/Binder.cs @@ -40,8 +40,6 @@ protected virtual Expression ProcessPipelineAsync( ParameterExpression context, Assign( result, Await( Invoke( Pipeline, context, argument ), configureAwait: false ) ), Assign( canceled, HandleCancellationRequested( contextControl, result ) ), - Invoke( LoggerExpression.Log( "Binder.ProcessPipelineAsync" + Random.Shared.Next( 0, 1000 ) ), Convert( result, typeof( object ) ) ), - Condition( canceled, New( tupleCtor, Default( typeof( TOutput ) ), canceled ), diff --git a/src/Hyperbee.Pipeline/Binders/Abstractions/StatementBinder.cs b/src/Hyperbee.Pipeline/Binders/Abstractions/StatementBinder.cs index e3f5b78..2a36ddd 100644 --- a/src/Hyperbee.Pipeline/Binders/Abstractions/StatementBinder.cs +++ b/src/Hyperbee.Pipeline/Binders/Abstractions/StatementBinder.cs @@ -39,7 +39,7 @@ protected virtual Expression ProcessStatementAsync( Expression( Expression> Bind( Expression>( BlockAsync( - [awaitedResult], - Assign( awaitedResult, Await( ProcessPipelineAsync( context, argument ) ) ), - - Invoke( LoggerExpression.Log( "CallBlockBinder.Bind" + Random.Shared.Next( 0, 1000 ) ), Convert( nextArgument, typeof( object ) ) ), - - Condition( canceled, - Default( typeof( TOutput ) ), - // TODO: Think there is a bug here, we shouldn't need a child state machine. + [awaitedResult, result], + Assign( awaitedResult, Await( ProcessPipelineAsync( context, argument ), configureAwait: false ) ), + IfThenElse( canceled, + Block( + Assign( result, Default( typeof(TOutput) ) )//, + //Return( returnValue, result ) + ), Block( Await( ProcessBlockAsync( next, context, nextArgument ), configureAwait: false ), - nextArgument + Assign( result, nextArgument )//, + //Return( returnValue, result ) ) - - ) + ), + result + //Label( returnValue, result ) ), parameters: [context, argument] ); diff --git a/src/Hyperbee.Pipeline/Binders/CallStatementBinder.cs b/src/Hyperbee.Pipeline/Binders/CallStatementBinder.cs index d65a728..94d4b30 100644 --- a/src/Hyperbee.Pipeline/Binders/CallStatementBinder.cs +++ b/src/Hyperbee.Pipeline/Binders/CallStatementBinder.cs @@ -42,6 +42,7 @@ public Expression> Bind( Expression> Bind( Expression>( BlockAsync( Await( Invoke( next, ctx, arg ), configureAwait: false ), - argument + arg ), parameters: [ctx, arg] ); - return Lambda>( + var l = Lambda>( BlockAsync( [awaitedResult], Assign( awaitedResult, Await( ProcessPipelineAsync( context, argument ), configureAwait: false ) ), + Condition( canceled, - Default( typeof( TOutput ) ), - // TODO: Think there is a bug here, we shouldn't need a child state machine. - // Await( - // BlockAsync( - // [awaitedResult], - Block( - // [awaitedResult], - Await( - ProcessStatementAsync( nextExpression, context, nextArgument, defaultName ), - configureAwait: false - ), - nextArgument - ) - // ) - // ) + Default( typeof(TOutput) ), + Await( + ProcessStatementAsync( nextExpression, context, nextArgument, defaultName ), + configureAwait: false + ) ) ), parameters: [context, argument] ); + + return l; } } diff --git a/src/Hyperbee.Pipeline/Binders/HookBinder.cs b/src/Hyperbee.Pipeline/Binders/HookBinder.cs index 816cc0e..0af21e2 100644 --- a/src/Hyperbee.Pipeline/Binders/HookBinder.cs +++ b/src/Hyperbee.Pipeline/Binders/HookBinder.cs @@ -38,9 +38,9 @@ public Expression> Bind( Expression>( - BlockAsync( - Await( Invoke( Middleware, ctx, arg, function ), configureAwait: false ), - argument + BlockAsync( + [function], + Await( Invoke( Middleware, ctx, arg, function ), configureAwait: false ) ), parameters: [ctx, arg] ); diff --git a/src/Hyperbee.Pipeline/Binders/PipeBlockBinder.cs b/src/Hyperbee.Pipeline/Binders/PipeBlockBinder.cs index 7c3023b..8af251a 100644 --- a/src/Hyperbee.Pipeline/Binders/PipeBlockBinder.cs +++ b/src/Hyperbee.Pipeline/Binders/PipeBlockBinder.cs @@ -39,6 +39,10 @@ public Expression> Bind( Expression> Bind( Expression Pipe( var (parentFunction, parentMiddleware) = parent.GetPipelineFunction(); - Expression> nextExpression = ( ctx, arg ) => Task.FromResult( next( ctx, arg ) ); + Expression> nextExpression = ( ctx, arg ) => AsyncNext( next, ctx, arg ); Expression> configExpression = config == null ? null : ctx => config( ctx ); return new PipelineBuilder { - Function = new PipeStatementBinder( parentFunction, parentMiddleware, configExpression ).Bind( nextExpression ).Reduce() as Expression>, + Function = new PipeStatementBinder( parentFunction, parentMiddleware, configExpression ).Bind( nextExpression ), Middleware = parentMiddleware }; } @@ -89,4 +89,13 @@ public static IPipelineBuilder PipeAsync( Middleware = parentMiddleware }; } + + private static async Task AsyncNext( Function next, IPipelineContext ctx, TOutput arg ) + { + var result = next( ctx, arg ); + await Task.CompletedTask.ConfigureAwait( false ); + + return result; + } + } diff --git a/src/Hyperbee.Pipeline/Hyperbee.Pipeline.csproj b/src/Hyperbee.Pipeline/Hyperbee.Pipeline.csproj index 46c12be..038922d 100644 --- a/src/Hyperbee.Pipeline/Hyperbee.Pipeline.csproj +++ b/src/Hyperbee.Pipeline/Hyperbee.Pipeline.csproj @@ -30,7 +30,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/src/Hyperbee.Pipeline/PipelineBuilder.cs b/src/Hyperbee.Pipeline/PipelineBuilder.cs index 197ac49..b522d47 100644 --- a/src/Hyperbee.Pipeline/PipelineBuilder.cs +++ b/src/Hyperbee.Pipeline/PipelineBuilder.cs @@ -85,7 +85,6 @@ Expression> IPipelineBuilder.CastExpression( return Lambda>( BlockAsync( - [context, argument], Convert( Await( Invoke( Function, context, Convert( argument, typeof( TInput ) ) ), configureAwait: false ), typeof( TOut ) ) ), parameters: [context, argument] diff --git a/test/Hyperbee.Pipeline.Auth.Tests/Hyperbee.Pipeline.Auth.Tests.csproj b/test/Hyperbee.Pipeline.Auth.Tests/Hyperbee.Pipeline.Auth.Tests.csproj index 67d34b5..d6b5970 100644 --- a/test/Hyperbee.Pipeline.Auth.Tests/Hyperbee.Pipeline.Auth.Tests.csproj +++ b/test/Hyperbee.Pipeline.Auth.Tests/Hyperbee.Pipeline.Auth.Tests.csproj @@ -14,9 +14,9 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + diff --git a/test/Hyperbee.Pipeline.Tests/Hyperbee.Pipeline.Tests.csproj b/test/Hyperbee.Pipeline.Tests/Hyperbee.Pipeline.Tests.csproj index a5a66b2..854a675 100644 --- a/test/Hyperbee.Pipeline.Tests/Hyperbee.Pipeline.Tests.csproj +++ b/test/Hyperbee.Pipeline.Tests/Hyperbee.Pipeline.Tests.csproj @@ -4,9 +4,9 @@ false - + - + diff --git a/test/Hyperbee.Pipeline.Tests/PipelineEnumerationTests.cs b/test/Hyperbee.Pipeline.Tests/PipelineEnumerationTests.cs index f5e729e..e044856 100644 --- a/test/Hyperbee.Pipeline.Tests/PipelineEnumerationTests.cs +++ b/test/Hyperbee.Pipeline.Tests/PipelineEnumerationTests.cs @@ -53,7 +53,7 @@ public async Task Pipeline_should_enumerate() await command( new PipelineContext(), "e f" ); - Assert.AreEqual( count, 25 ); + Assert.AreEqual( 25, count ); } [TestMethod] diff --git a/test/Hyperbee.Pipeline.Tests/PipelineExecutionTests.cs b/test/Hyperbee.Pipeline.Tests/PipelineExecutionTests.cs index b0499dc..e32f93d 100644 --- a/test/Hyperbee.Pipeline.Tests/PipelineExecutionTests.cs +++ b/test/Hyperbee.Pipeline.Tests/PipelineExecutionTests.cs @@ -16,8 +16,6 @@ public async Task Pipeline_should_execute_function() var command = PipelineFactory .Start() .Pipe( ( ctx, arg ) => int.Parse( arg ) ) - .Pipe( ( ctx, arg ) => arg ) - //.Pipe( ( ctx, arg ) => arg ) .Build(); var result = await command( new PipelineContext(), "5" ); diff --git a/test/Hyperbee.PipelineCaching.Tests/Hyperbee.Pipeline.Caching.Tests.csproj b/test/Hyperbee.PipelineCaching.Tests/Hyperbee.Pipeline.Caching.Tests.csproj index b07f8af..eef89b6 100644 --- a/test/Hyperbee.PipelineCaching.Tests/Hyperbee.Pipeline.Caching.Tests.csproj +++ b/test/Hyperbee.PipelineCaching.Tests/Hyperbee.Pipeline.Caching.Tests.csproj @@ -14,10 +14,10 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + - - + +