private async Task ReceiveActivityInternal(IBotContext context, IReceiveActivity[] middleware, Func <IBotContext, Task> callback) { BotAssert.MiddlewareNotNull(middleware); if (middleware.Length == 0) // No middleware to run. { // If all the Middlware ran, the "leading edge" of the tree is now complete. // This means it's time to run any developer specified callback. // Once this callback is done, the "trailing edge" calls are then completed. This // allows code that looks like: // console.print("before"); // await next(); // console.print("after"); // to run as expected. if (callback != null) { await callback(context); } return; } // Default to "No more Middleware after this" async Task next() { // Remove the first item from the list of middleware to call, // so that the next call just has the remaining items to worry about. IReceiveActivity[] remainingMiddleware = middleware.Skip(1).ToArray(); await ReceiveActivityInternal(context, remainingMiddleware, callback).ConfigureAwait(false); } // Grab the current middleware, which is the 1st element in the array, and execute it await middleware[0].ReceiveActivity(context, next).ConfigureAwait(false); }
private async Task <bool> ReceiveActivityInternal(IBotContext context, IReceiveActivity[] middleware) { BotAssert.MiddlewareNotNull(middleware); bool didAllRun = false; if (middleware.Length == 0) // No middleware to run. { // If all the Middlware ran, let the caller know. return(true); } // Default to "No more Middleware after this" async Task next() { // Remove the first item from the list of middleware to call, // so that the next call just has the remaining items to worry about. IReceiveActivity[] remainingMiddleware = middleware.Skip(1).ToArray(); didAllRun |= await ReceiveActivityInternal(context, remainingMiddleware).ConfigureAwait(false); } // Grab the current middleware, which is the 1st element in the array, and execute it await middleware[0].ReceiveActivity(context, next).ConfigureAwait(false); return(didAllRun); }
private async Task ContextCreatedInternal(IBotContext context, IContextCreated[] middleware) { BotAssert.MiddlewareNotNull(middleware); if (middleware.Length == 0) // No middleware to run. { return; } async Task next() { // Remove the first item from the list of middleware to call, // so that the next call just has the remaining items to worry about. IContextCreated[] remainingMiddleware = middleware.Skip(1).ToArray(); await ContextCreatedInternal(context, remainingMiddleware).ConfigureAwait(false); } // Grab the current middleware, which is the 1st element in the array, and execute it await middleware[0].ContextCreated(context, next).ConfigureAwait(false); }
public MiddlewareSet Use(IMiddleware middleware) { BotAssert.MiddlewareNotNull(middleware); _middleware.Add(middleware); return(this); }