Skip to main content

Module transaction

Module transaction 

Source
Expand description

Transaction support for grouping multiple commands atomically.

Transactions allow multiple operations to be grouped together and treated as a single undoable unit. If any operation fails, all previous operations in the transaction are rolled back.

§Usage

use ftui_runtime::undo::{HistoryManager, Transaction};

let mut history = HistoryManager::default();

// Begin a transaction
let mut txn = Transaction::begin("Format Document");

// Add commands to the transaction
txn.push(normalize_whitespace_cmd)?;
txn.push(fix_indentation_cmd)?;
txn.push(sort_imports_cmd)?;

// Commit the transaction to history
history.push(txn.commit());

§Nested Transactions

Transactions can be nested using TransactionScope:

let mut scope = TransactionScope::new(&mut history);

// Outer transaction
scope.begin("Refactor");

// Inner transaction
scope.begin("Rename Variable");
scope.execute(rename_cmd)?;
scope.commit()?;

// More outer work
scope.execute(move_function_cmd)?;
scope.commit()?;

§Invariants

  1. A committed transaction acts as a single command in history
  2. Rollback undoes all executed commands in reverse order
  3. Nested transactions must be committed/rolled back in order
  4. Empty transactions produce no history entry

Structs§

Transaction
Builder for creating a group of commands as a single transaction.
TransactionScope
Scope-based transaction manager for nested transactions.