observeReceipt method

CommandApplyStatus observeReceipt(
  1. CausalReceipt receipt
)

Implementation

CommandApplyStatus observeReceipt(CausalReceipt receipt) {
  if (_seenReceiptIds.contains(receipt.receiptId)) {
    return const CommandApplyDuplicate();
  }
  final entry = _entries[receipt.causationId];
  if (entry == null) return const CommandApplyUnknown();
  if (receipt.generation != entry.generation) {
    return CommandApplyStaleGeneration(entry.generation, receipt.generation);
  }
  if (!receipt.outcome.isTerminal) {
    _seenReceiptIds.add(receipt.receiptId);
    if (!entry.terminal &&
        _phaseRank(CommandStatus.accepted) >= _phaseRank(entry.status)) {
      _entries[receipt.causationId] =
          entry.copyWith(status: CommandStatus.accepted);
    }
    return const CommandApplyRecorded();
  }
  final incoming = _terminalStatusOf(receipt.outcome, receipt.reason);
  if (entry.terminal) {
    if (entry.status == incoming) {
      _seenReceiptIds.add(receipt.receiptId);
      return const CommandApplyRecorded();
    }
    _conflicts.add(receipt.causationId);
    return CommandApplyTerminalConflict(
        receipt.causationId, entry.status, incoming);
  }
  _seenReceiptIds.add(receipt.receiptId);
  _entries[receipt.causationId] = entry.copyWith(
    terminal: true,
    status: incoming,
    reason: receipt.reason,
    terminalReceiptId: receipt.receiptId,
  );
  return const CommandApplyRecorded();
}