CompensationScope

class CompensationScope<R, S>(val result: R, val state: S)

Scope for compensation execution, providing access to both step result and saga state.

This scope is the receiver for compensation lambdas, enabling "smart compensation" where the compensation logic can make decisions based on:

  • result: The result produced by this specific step's forward action

  • state: The current saga state, including updates from later steps

Example: Smart Compensation

// Step 1: Take payment
first `do` "take-payment" with { cart ->
updateState { it.copy(paymentAmount = payment.amount) }
createTransaction(cart)
} and undo {
// Smart compensation: behavior depends on later steps
when {
state.escrowCaptured -> {
// Step 3 captured escrow - need to dispense physical refund
cashManager.dispenseCashRefund(state.paymentAmount)
}
state.paymentAmount > 0 -> {
// Escrow exists but wasn't captured - just return it
cashManager.returnEscrow()
}
else -> {
// No payment received
cashManager.disableAcceptors()
}
}
}

// Step 3: Capture escrow (sets escrowCaptured = true)
then `do` "capture-payment" with { cart ->
cashManager.captureEscrow()
updateState { it.copy(escrowCaptured = true) }
result
}

Parameters

R

The type of result produced by this step

S

The type of shared saga state

Constructors

Link copied to clipboard
constructor(result: R, state: S)

Properties

Link copied to clipboard
val result: R

Result produced by this step's forward action.

Link copied to clipboard
val state: S

Current saga state at the time of compensation.