getEnforceMiddleware<T> function

Middleware<T> getEnforceMiddleware<T>(
  1. Middleware<T> beforeMiddleware,
  2. Middleware<T> middleware,
  3. Middleware<T> afterMiddleware
)

Runs middleware before and after the main

Example:

getEnforceMiddleware(
  prepareData,
  sendData,
  clearData
);

Implementation

Middleware<T> getEnforceMiddleware<T>(
  Middleware<T> beforeMiddleware,
  Middleware<T> middleware,
  Middleware<T> afterMiddleware,
// ignore: prefer_expression_function_bodies
) {
  return (context, next) async {
    final beforeCalled =
        await wrapMiddlewareNextCall(context, beforeMiddleware);

    if (!beforeCalled) {
      return;
    }

    final middlewareCalled = await wrapMiddlewareNextCall(context, middleware);
    if (!middlewareCalled) {
      return;
    }

    return afterMiddleware(context, next);
  };
}