2017-01-31 12:01:08 +01:00
|
|
|
|
using System;
|
2017-02-15 22:05:01 +01:00
|
|
|
|
using System.Linq.Expressions;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2018-12-19 20:50:56 +01:00
|
|
|
|
// ReSharper disable UnusedMember.Global
|
|
|
|
|
|
2017-01-31 12:01:08 +01:00
|
|
|
|
namespace Tapeti.Flow
|
|
|
|
|
{
|
2019-08-14 20:48:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides methods to build an IYieldPoint to indicate if and how Flow should continue.
|
|
|
|
|
/// </summary>
|
2017-01-31 12:01:08 +01:00
|
|
|
|
public interface IFlowProvider
|
|
|
|
|
{
|
2019-08-14 20:48:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Publish a request message and continue the flow when the response arrives.
|
|
|
|
|
/// The request message must be marked with the [Request] attribute, and the
|
|
|
|
|
/// Response type must match. Used for asynchronous response handlers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <param name="responseHandler"></param>
|
|
|
|
|
/// <typeparam name="TRequest"></typeparam>
|
|
|
|
|
/// <typeparam name="TResponse"></typeparam>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IYieldPoint YieldWithRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task<IYieldPoint>> responseHandler) where TRequest : class where TResponse : class;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
2022-02-09 12:19:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Publish a request message and continue the flow when the response arrives.
|
|
|
|
|
/// The request message must be marked with the [Request] attribute, and the
|
|
|
|
|
/// Response type must match. Used for asynchronous response handlers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <param name="responseHandler"></param>
|
|
|
|
|
/// <typeparam name="TRequest"></typeparam>
|
|
|
|
|
/// <typeparam name="TResponse"></typeparam>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IYieldPoint YieldWithRequest<TRequest, TResponse>(TRequest message, Func<TResponse, ValueTask<IYieldPoint>> responseHandler) where TRequest : class where TResponse : class;
|
2022-02-09 12:19:05 +01:00
|
|
|
|
|
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Publish a request message and continue the flow when the response arrives.
|
|
|
|
|
/// The request message must be marked with the [Request] attribute, and the
|
|
|
|
|
/// Response type must match. Used for synchronous response handlers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// The reason why this requires the extra 'Sync' in the name: one does not simply overload methods
|
|
|
|
|
/// with Task vs non-Task Funcs. "Ambiguous call". Apparantly this is because a return type
|
|
|
|
|
/// of a method is not part of its signature,according to:
|
|
|
|
|
/// http://stackoverflow.com/questions/18715979/ambiguity-with-action-and-func-parameter
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <param name="responseHandler"></param>
|
|
|
|
|
/// <typeparam name="TRequest"></typeparam>
|
|
|
|
|
/// <typeparam name="TResponse"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IYieldPoint YieldWithRequestSync<TRequest, TResponse>(TRequest message, Func<TResponse, IYieldPoint> responseHandler) where TRequest : class where TResponse : class;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a request builder to publish one or more requests messages. Call Yield on the resulting builder
|
|
|
|
|
/// to acquire an IYieldPoint.
|
|
|
|
|
/// </summary>
|
2017-01-31 12:01:08 +01:00
|
|
|
|
IFlowParallelRequestBuilder YieldWithParallelRequest();
|
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// End the flow by publishing the specified response message. Only allowed, and required, when the
|
|
|
|
|
/// current flow was started by a message handler for a Request message.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <typeparam name="TResponse"></typeparam>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IYieldPoint EndWithResponse<TResponse>(TResponse message) where TResponse : class;
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// End the flow and dispose any state.
|
|
|
|
|
/// </summary>
|
2017-01-31 12:01:08 +01:00
|
|
|
|
IYieldPoint End();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
2017-02-15 22:05:01 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Allows starting a flow outside of a message handler.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public interface IFlowStarter
|
|
|
|
|
{
|
2019-08-14 20:48:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts a new flow.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="methodSelector"></param>
|
2017-02-15 22:05:01 +01:00
|
|
|
|
Task Start<TController>(Expression<Func<TController, Func<IYieldPoint>>> methodSelector) where TController : class;
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts a new flow.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="methodSelector"></param>
|
2017-02-15 22:05:01 +01:00
|
|
|
|
Task Start<TController>(Expression<Func<TController, Func<Task<IYieldPoint>>>> methodSelector) where TController : class;
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts a new flow and passes the parameter to the method.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="methodSelector"></param>
|
|
|
|
|
/// <param name="parameter"></param>
|
2017-09-22 11:19:49 +02:00
|
|
|
|
Task Start<TController, TParameter>(Expression<Func<TController, Func<TParameter, IYieldPoint>>> methodSelector, TParameter parameter) where TController : class;
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts a new flow and passes the parameter to the method.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="methodSelector"></param>
|
|
|
|
|
/// <param name="parameter"></param>
|
2017-09-22 11:19:49 +02:00
|
|
|
|
Task Start<TController, TParameter>(Expression<Func<TController, Func<TParameter, Task<IYieldPoint>>>> methodSelector, TParameter parameter) where TController : class;
|
2017-02-15 22:05:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
2017-02-15 22:05:01 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Internal interface. Do not call directly.
|
|
|
|
|
/// </summary>
|
2017-01-31 12:01:08 +01:00
|
|
|
|
public interface IFlowHandler
|
|
|
|
|
{
|
2019-08-14 20:48:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes the YieldPoint for the given message context.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
/// <param name="yieldPoint"></param>
|
2022-02-09 11:26:56 +01:00
|
|
|
|
ValueTask Execute(IFlowHandlerContext context, IYieldPoint yieldPoint);
|
2021-12-09 23:52:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the parallel request for the given message context.
|
|
|
|
|
/// </summary>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IFlowParallelRequest? GetParallelRequest(IFlowHandlerContext context);
|
2021-12-10 11:45:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Calls the converge method for a parallel flow.
|
|
|
|
|
/// </summary>
|
2022-02-09 11:26:56 +01:00
|
|
|
|
ValueTask Converge(IFlowHandlerContext context);
|
2021-12-10 11:45:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines how the Yield method of a parallel request behaves when no requests have been added.
|
|
|
|
|
/// Useful in cases where requests are sent conditionally.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public enum FlowNoRequestsBehaviour
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Throw an exception. This is the default behaviour to prevent subtle bugs when not specifying the behaviour explicitly,
|
|
|
|
|
/// as well as for backwards compatibility.
|
|
|
|
|
/// </summary>
|
|
|
|
|
Exception,
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Immediately call the continuation method.
|
|
|
|
|
/// </summary>
|
|
|
|
|
Converge,
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// End the flow without calling the converge method.
|
|
|
|
|
/// </summary>
|
|
|
|
|
EndFlow
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
2021-12-10 11:45:09 +01:00
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Builder to publish one or more request messages and continuing the flow when the responses arrive.
|
|
|
|
|
/// </summary>
|
2017-01-31 12:01:08 +01:00
|
|
|
|
public interface IFlowParallelRequestBuilder
|
|
|
|
|
{
|
2019-08-14 20:48:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Publish a request message and continue the flow when the response arrives.
|
|
|
|
|
/// Note that the response handler can not influence the flow as it does not return a YieldPoint.
|
|
|
|
|
/// It can instead store state in the controller for the continuation passed to the Yield method.
|
|
|
|
|
/// Used for asynchronous response handlers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <param name="responseHandler"></param>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IFlowParallelRequestBuilder AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler) where TRequest : class where TResponse : class;
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
2022-02-09 12:19:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Publish a request message and continue the flow when the response arrives.
|
|
|
|
|
/// Note that the response handler can not influence the flow as it does not return a YieldPoint.
|
|
|
|
|
/// It can instead store state in the controller for the continuation passed to the Yield method.
|
|
|
|
|
/// Used for asynchronous response handlers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <param name="responseHandler"></param>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IFlowParallelRequestBuilder AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, ValueTask> responseHandler) where TRequest : class where TResponse : class;
|
2022-02-09 12:19:05 +01:00
|
|
|
|
|
2021-12-09 23:52:25 +01:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This overload allows the response handler access to the IFlowParallelRequest interface, which
|
|
|
|
|
/// can be used to add additional requests to the parallel request before the continuation method passed to the Yield method is called.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <inheritdoc cref="AddRequest{TRequest,TResponse}(TRequest,Func{TResponse,Task})"/>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IFlowParallelRequestBuilder AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler) where TRequest : class where TResponse : class;
|
2021-12-09 23:52:25 +01:00
|
|
|
|
|
2022-02-09 12:19:05 +01:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This overload allows the response handler access to the IFlowParallelRequest interface, which
|
|
|
|
|
/// can be used to add additional requests to the parallel request before the continuation method passed to the Yield method is called.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <inheritdoc cref="AddRequest{TRequest,TResponse}(TRequest,Func{TResponse,ValueTask})"/>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IFlowParallelRequestBuilder AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, ValueTask> responseHandler) where TRequest : class where TResponse : class;
|
2022-02-09 12:19:05 +01:00
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Publish a request message and continue the flow when the response arrives.
|
|
|
|
|
/// Note that the response handler can not influence the flow as it does not return a YieldPoint.
|
|
|
|
|
/// It can instead store state in the controller for the continuation passed to the Yield method.
|
|
|
|
|
/// Used for synchronous response handlers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <param name="responseHandler"></param>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IFlowParallelRequestBuilder AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler) where TRequest : class where TResponse : class;
|
2017-01-31 12:01:08 +01:00
|
|
|
|
|
2022-02-09 12:19:05 +01:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This overload allows the response handler access to the IFlowParallelRequest interface, which
|
|
|
|
|
/// can be used to add additional requests to the parallel request before the continuation method passed to the Yield method is called.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <inheritdoc cref="AddRequestSync{TRequest,TResponse}(TRequest,Action{TResponse})"/>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
IFlowParallelRequestBuilder AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse, IFlowParallelRequest> responseHandler) where TRequest : class where TResponse : class;
|
2022-02-09 12:19:05 +01:00
|
|
|
|
|
2021-12-09 23:52:25 +01:00
|
|
|
|
/// There is no Sync overload with an IFlowParallelRequest parameter, as the AddRequest methods for that are
|
|
|
|
|
/// async, so you should always await them.
|
2019-08-14 20:48:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs an IYieldPoint to continue the flow when responses arrive.
|
|
|
|
|
/// The continuation method is called when all responses have arrived.
|
|
|
|
|
/// Response handlers and the continuation method are guaranteed thread-safe access to the
|
|
|
|
|
/// controller and can store state.
|
|
|
|
|
/// Used for asynchronous continuation methods.
|
|
|
|
|
/// </summary>
|
2021-12-10 11:45:09 +01:00
|
|
|
|
/// <param name="continuation">The converge continuation method to be called when all responses have been handled.</param>
|
|
|
|
|
/// <param name="noRequestsBehaviour">How the Yield method should behave when no requests have been added to the parallel request builder.</param>
|
|
|
|
|
IYieldPoint Yield(Func<Task<IYieldPoint>> continuation, FlowNoRequestsBehaviour noRequestsBehaviour = FlowNoRequestsBehaviour.Exception);
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs an IYieldPoint to continue the flow when responses arrive.
|
|
|
|
|
/// The continuation method is called when all responses have arrived.
|
|
|
|
|
/// Response handlers and the continuation method are guaranteed thread-safe access to the
|
|
|
|
|
/// controller and can store state.
|
|
|
|
|
/// Used for synchronous continuation methods.
|
|
|
|
|
/// </summary>
|
2021-12-10 11:45:09 +01:00
|
|
|
|
/// <param name="continuation">The converge continuation method to be called when all responses have been handled.</param>
|
|
|
|
|
/// <param name="noRequestsBehaviour">How the Yield method should behave when no requests have been added to the parallel request builder.</param>
|
|
|
|
|
IYieldPoint YieldSync(Func<IYieldPoint> continuation, FlowNoRequestsBehaviour noRequestsBehaviour = FlowNoRequestsBehaviour.Exception);
|
2017-01-31 12:01:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
|
2021-12-09 23:52:25 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides means of adding one or more requests to a parallel request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Add a parameter of this type to a parallel request's response handler to gain access to it's functionality.
|
|
|
|
|
/// Not available in other contexts.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public interface IFlowParallelRequest
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Publish a request message and continue the flow when the response arrives.
|
|
|
|
|
/// Note that the response handler can not influence the flow as it does not return a YieldPoint.
|
|
|
|
|
/// It can instead store state in the controller for the continuation passed to the Yield method.
|
|
|
|
|
/// Used for asynchronous response handlers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <param name="responseHandler"></param>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
Task AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, Task> responseHandler) where TRequest : class where TResponse : class;
|
2021-12-09 23:52:25 +01:00
|
|
|
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This overload allows the response handler access to the IFlowParallelRequest interface, which
|
|
|
|
|
/// can be used to add additional requests to the parallel request before the continuation method passed to the Yield method is called.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <inheritdoc cref="AddRequest{TRequest,TResponse}(TRequest,Func{TResponse,Task})"/>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
Task AddRequest<TRequest, TResponse>(TRequest message, Func<TResponse, IFlowParallelRequest, Task> responseHandler) where TRequest : class where TResponse : class;
|
2021-12-09 23:52:25 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Publish a request message and continue the flow when the response arrives.
|
|
|
|
|
/// Note that the response handler can not influence the flow as it does not return a YieldPoint.
|
|
|
|
|
/// It can instead store state in the controller for the continuation passed to the Yield method.
|
|
|
|
|
/// Used for synchronous response handlers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
/// <param name="responseHandler"></param>
|
2022-11-23 09:13:38 +01:00
|
|
|
|
Task AddRequestSync<TRequest, TResponse>(TRequest message, Action<TResponse> responseHandler) where TRequest : class where TResponse : class;
|
2021-12-09 23:52:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-14 20:48:40 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Defines if and how the Flow should continue. Construct using any of the IFlowProvider methods.
|
|
|
|
|
/// </summary>
|
2017-01-31 12:01:08 +01:00
|
|
|
|
public interface IYieldPoint
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|