Meziantou.Framework.UndoRedo 3.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Meziantou.Framework.UndoRedo --version 3.0.0
                    
NuGet\Install-Package Meziantou.Framework.UndoRedo -Version 3.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Meziantou.Framework.UndoRedo" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Meziantou.Framework.UndoRedo" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Meziantou.Framework.UndoRedo" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Meziantou.Framework.UndoRedo --version 3.0.0
                    
#r "nuget: Meziantou.Framework.UndoRedo, 3.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Meziantou.Framework.UndoRedo@3.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Meziantou.Framework.UndoRedo&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Meziantou.Framework.UndoRedo&version=3.0.0
                    
Install as a Cake Tool

Meziantou.Framework.UndoRedo

Meziantou.Framework.UndoRedo provides an async-first undo/redo framework based on the command pattern. Every action exposes asynchronous ExecuteAsync / UnExecuteAsync methods (returning ValueTask), supports cancellation, and can be grouped into transactions or merged into a single undo step.

Record, undo and redo an action

The simplest way to record an action is to provide the execute and unexecute delegates:

var manager = new UndoRedoManager();
var list = new List<int>();

await manager.RecordActionAsync(
    execute: () => list.Add(1),
    unexecute: () => list.RemoveAt(list.Count - 1));

// list = [1]
await manager.UndoAsync(); // list = []
await manager.RedoAsync(); // list = [1]

CanUndo and CanRedo indicate whether an operation is available, and CollectionChanged is raised whenever the history changes.

The execute and unexecute delegates can each be synchronous or asynchronous, and the two may be mixed. Build the action with UndoRedoDelegateAction and record it:

// Asynchronous execute, synchronous unexecute
var action = new UndoRedoDelegateAction(
    execute: ct => SaveAsync(ct),
    unexecute: () => Restore());

await manager.RecordActionAsync(action);

Custom action

For reusable actions, derive from UndoRedoActionBase. The base class guards against executing or reverting twice in a row.

sealed class AddItemAction(IList<int> list, int value) : UndoRedoActionBase
{
    protected override ValueTask ExecuteCoreAsync(CancellationToken cancellationToken)
    {
        list.Add(value);
        return ValueTask.CompletedTask;
    }

    protected override ValueTask UnExecuteCoreAsync(CancellationToken cancellationToken)
    {
        list.Remove(value);
        return ValueTask.CompletedTask;
    }
}

await manager.RecordActionAsync(new AddItemAction(list, 42));

Group actions in a transaction

A transaction groups several actions into a single undo step. Disposing the transaction commits it (use await using), or call RollbackAsync to revert it.

await using (manager.CreateTransaction())
{
    await manager.RecordActionAsync(addFirst, removeFirst);
    await manager.RecordActionAsync(addSecond, removeSecond);
}

// A single UndoAsync reverts both actions
await manager.UndoAsync();

Disposing always commits, including when the scope is left because of an exception. To discard the actions recorded so far, call RollbackAsync explicitly:

var transaction = manager.CreateTransaction();
try
{
    await manager.RecordActionAsync(addFirst, removeFirst);
    await manager.RecordActionAsync(addSecond, removeSecond);
    await transaction.CommitAsync();
}
catch
{
    await transaction.RollbackAsync();
    throw;
}

Nested transactions must be completed from the innermost out; committing or rolling back a transaction while one of its nested transactions is still open throws an InvalidOperationException.

Committing a transaction is all-or-nothing: if one of its actions fails, the actions that already ran are reverted before the exception is propagated.

Merge consecutive actions

When an action sets AllowToMergeWithPrevious and the previous action's TryToMergeAsync returns true, both collapse into a single undo step. This is useful for chains of similar operations such as typing or dragging.

Note: UndoRedoManager is not thread-safe; operate on it from a single logical flow. An action must not record, undo, or redo anything while it is executing: those operations throw an InvalidOperationException as long as ActionIsExecuting is true.

Migrating from 2.x

2.x 3.0
IUndoRedoAction.CanExecute() / CanUnExecute() Removed. UndoRedoManager never called them; use CanUndo / CanRedo to know whether an operation is available.
TryToMergeAsync(IUndoRedoAction) TryToMergeAsync(IUndoRedoAction, CancellationToken)
UndoRedoTransaction.ExecuteAsync / UnExecuteAsync / TryToMergeAsync Explicit interface implementations; cast to IUndoRedoAction to reach them.
UndoableActions / RedoableActions returning IEnumerable<IUndoRedoAction> IReadOnlyList<IUndoRedoAction>, a snapshot taken on each access.
Recording, undoing or redoing from inside a running action Throws InvalidOperationException.
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.
  • net11.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.1 92 9/6/2026
4.0.0 92 9/4/2026
3.0.1 93 8/23/2026
3.0.0 96 8/21/2026
2.0.0 125 7/5/2026
1.0.1 118 6/13/2026
1.0.0 114 6/2/2026