# `TCC.Action`
[🔗](https://github.com/vcnovaes/toccata/blob/v0.1.0/lib/tcc/action.ex#L1)

Represents a single TCC action in a distributed transaction.

Each action consists of three phases:
- Try: Reserve resources and validate
- Confirm: Commit the changes
- Cancel: Rollback and release resources

# `callback`
[🔗](https://github.com/vcnovaes/toccata/blob/v0.1.0/lib/tcc/action.ex#L14)

```elixir
@type callback() :: function() | {module(), atom(), list()}
```

Callback type - can be an anonymous function or MFA tuple.

# `t`
[🔗](https://github.com/vcnovaes/toccata/blob/v0.1.0/lib/tcc/action.ex#L16)

```elixir
@type t() :: %TCC.Action{
  async: boolean(),
  cancel_fun: callback(),
  confirm_fun: callback(),
  name: atom(),
  opts: map(),
  try_fun: callback()
}
```

# `async?`
[🔗](https://github.com/vcnovaes/toccata/blob/v0.1.0/lib/tcc/action.ex#L103)

```elixir
@spec async?(t()) :: boolean()
```

Returns true if the action is async.

# `execute_cancel`
[🔗](https://github.com/vcnovaes/toccata/blob/v0.1.0/lib/tcc/action.ex#L143)

```elixir
@spec execute_cancel(t(), map(), any()) :: {:ok, map(), any()} | {:error, any()}
```

Executes the Cancel phase of the action with retry logic.

# `execute_confirm`
[🔗](https://github.com/vcnovaes/toccata/blob/v0.1.0/lib/tcc/action.ex#L126)

```elixir
@spec execute_confirm(t(), map(), any()) :: {:ok, map(), any()} | {:error, any()}
```

Executes the Confirm phase of the action with retry logic.

# `execute_try`
[🔗](https://github.com/vcnovaes/toccata/blob/v0.1.0/lib/tcc/action.ex#L114)

```elixir
@spec execute_try(t(), map(), any()) ::
  {:ok, map(), any()} | {:error, any()} | {:abort, any()}
```

Executes the Try phase of the action.

The try function can return:
- `{:ok, effects, params}` - Success, continue to next action
- `{:error, reason}` - Error, trigger cancel phase (allows retry)
- `{:abort, reason}` - Unrecoverable error, trigger cancel phase (no retry)

# `new`
[🔗](https://github.com/vcnovaes/toccata/blob/v0.1.0/lib/tcc/action.ex#L56)

```elixir
@spec new(atom(), callback(), callback(), callback(), keyword()) :: t()
```

Creates a new TCC action.

Callbacks can be either anonymous functions or MFA tuples `{Module, :function, args}`.
When using MFA tuples, `effects` and `params` are prepended to the args list.

## Examples

    # Using anonymous functions
    iex> TCC.Action.new(:payment, &try_fn/2, &confirm_fn/2, &cancel_fn/2)
    %TCC.Action{name: :payment, ...}

    # Using MFA tuples
    iex> TCC.Action.new(:payment,
    ...>   {PaymentService, :try_payment, []},
    ...>   {PaymentService, :confirm_payment, []},
    ...>   {PaymentService, :cancel_payment, []})
    %TCC.Action{name: :payment, ...}

# `new_async`
[🔗](https://github.com/vcnovaes/toccata/blob/v0.1.0/lib/tcc/action.ex#L87)

```elixir
@spec new_async(atom(), callback(), callback(), callback(), keyword()) :: t()
```

Creates a new async TCC action.

Async actions are executed in parallel during the Try phase. They are awaited
before the next synchronous action or at the end of the Try phase.

## Options

  * `:timeout` - Time in milliseconds to wait for the async action (default: 5000)

## Example

    TCC.new()
    |> TCC.run_async(:payment, &try_fn/2, &confirm_fn/2, &cancel_fn/2, timeout: 10_000)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
