StatefulSagaStep

data class StatefulSagaStep<C, R, S>(val step: TypedValue, val forward: suspend StatefulStepScope<R, S>.(C) -> StepOutcome<R>, val compensation: suspend CompensationScope<R, S>.() -> Unit? = null, val idempotent: Boolean = false, val timeout: Duration? = null, val retryPolicy: RetryPolicy? = null)

Represents a single step in a stateful saga with typed state management.

Unlike SagaStep, this step provides access to shared saga state that can be updated during forward execution and read during compensation. This enables "smart compensation" where compensation logic can make decisions based on the execution progress of later steps.

Example

data class PaymentState(val escrowCaptured: Boolean = false)

StatefulSagaStep<Cart, Order, PaymentState>(
step = TypedValue.fromEnum(OrderSteps.TAKE_PAYMENT),
forward = { cart ->
updateState { it.copy(amount = payment.amount) }
completes with createOrder(cart)
},
compensation = {
// `state` contains latest state including updates from later steps
if (state.escrowCaptured) refund() else returnEscrow()
}
)

Parameters

C

The type of context passed to saga steps

R

The type of result produced by saga steps

S

The type of shared saga state

Constructors

Link copied to clipboard
constructor(step: TypedValue, forward: suspend StatefulStepScope<R, S>.(C) -> StepOutcome<R>, compensation: suspend CompensationScope<R, S>.() -> Unit? = null, idempotent: Boolean = false, timeout: Duration? = null, retryPolicy: RetryPolicy? = null)

Properties

Link copied to clipboard
val compensation: suspend CompensationScope<R, S>.() -> Unit?

Optional compensation logic executed on saga failure

Link copied to clipboard
val forward: suspend StatefulStepScope<R, S>.(C) -> StepOutcome<R>

Forward action that executes the step's business logic with explicit outcome control

Link copied to clipboard

Whether this step is idempotent (safe to retry)

Link copied to clipboard

String representation of the step name. Provided for backward compatibility.

Link copied to clipboard

Retry policy for handling transient failures

Link copied to clipboard

Step identifier wrapped in TypedValue (can be enum or string)

Link copied to clipboard

Maximum execution time for this step

Functions

Link copied to clipboard
inline fun <E : Enum<E>> stepAs(): E?

Retrieves the step identifier as the specified enum type.