commandHandler method

  1. @override
Future<void> commandHandler(
  1. Command command
)
override

Process a command through the full pipeline This is the main entry point for command processing.

Sequential execution is guaranteed by the lock in PersistentActor._handleCommand, which wraps this method.

Implementation

@override
Future<void> commandHandler(Command command) async {
  try {
    // Ensure aggregate is initialized
    if (!isInitialized) {
      _currentState = createInitialState();
    }

    // Check optimistic concurrency control
    await _checkConcurrency(command);

    // Handle the command and generate events
    final events = await handleCommand(currentState, command);

    // Persist all generated events (this also updates state via eventHandler)
    if (events.isNotEmpty) {
      await persistEvents(events);
    }

    // Call command processing hook
    await onCommandProcessed(command, events);
  } catch (e) {
    await onCommandFailure(command, e);
    rethrow;
  }
}