execute method

  1. @nonVirtual
Future<ActionResult> execute(
  1. NodeSpec action,
  2. ActionContext context
)

The action's actual execution.

Implementation

@nonVirtual
Future<ActionResult> execute(NodeSpec action, ActionContext context) async {
  if (!context.buildContext.mounted) {
    log.severe("BuildContext is not mounted (${action.type})");
    return ActionResult(false);
  }

  final resolver = getResolver(action.type);
  if (resolver == null) {
    log.severe("Action resolver for '${action.type}' not found");
    return ActionResult(false);
  }

  if (!(resolver is ExecutorFunction<SilentActionResult> ||
      resolver is ExecutorFunction<SilentActionResult> ||
      parseBool(action.props["silent"]))) {
    onExecuting();
  }

  late ActionResult result;
  try {
    result = await resolver(action, context);
    log.infoWithContext("Action '${action.type}' executed successfully", {
      "returnData": result.returnData,
      "next": result.nextAction == null
          ? null
          : {
              "id": result.nextAction?["_id"],
              "type": result.nextAction?["_type"],
            }
    });
  } catch (e, stack) {
    log.severe("Error executing action '${action.type}'", e, stack);
    if ((await handleException(action, context, e)).retry) {
      return execute(action, context);
    }
    result = ActionResult(false);
  }
  onExecuted();
  return result;
}