onCommandProcessed method

  1. @override
Future<void> onCommandProcessed(
  1. Command command,
  2. List<Event> events
)

Send response messages after successful command processing Only active when aggregate is used as an actor in the actor system

Implementation

@override
Future<void> onCommandProcessed(Command command, List<Event> events) async {
  await super.onCommandProcessed(command, events);

  // Send actor system responses FIRST (non-blocking) so callers don't timeout
  // waiting for secure storage writes to complete.
  if (_isInActorSystem()) {
    final sender = _capturedSenders[command.commandId];
    if (sender != null) {
      for (final event in events) {
        if (event is WalletCreatedEvent) {
          sender.tell(WalletCreatedResponse(
            walletId: event.walletId,
            rootAddress: event.rootAddress,
            success: true,
          ));
        } else if (event is AddressGeneratedEvent) {
          sender.tell(AddressGeneratedResponse(
            walletId: event.walletId,
            address: event.address,
            derivationIndex: event.derivationIndex,
            success: true,
            publicKeyHex: event.publicKeyHex,
            metadata: event.metadata,
          ));
        } else if (event is UTXOReceivedEvent) {
          sender.tell(UTXOReceivedResponse(
            walletId: event.walletId,
            txid: event.txid,
            vout: event.vout,
            success: true,
          ));
        } else if (event is TransactionImportedEvent) {
          sender.tell(TransactionRecordedResponse(
            walletId: event.walletId,
            txid: event.txid,
            success: true,
          ));
        }
      }
    }
  }

  // Store key material AFTER events are persisted AND response is sent.
  // Events are the source of truth; keys in secure storage are a side effect.
  // This maintains CQRS atomicity without blocking the caller.
  if (command is CreateWalletCommand && events.isNotEmpty) {
    await _storeKeyMaterial(command, events);
  }
}