Meziantou.Framework.UndoRedo
3.0.0
Prefix Reserved
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
<PackageReference Include="Meziantou.Framework.UndoRedo" Version="3.0.0" />
<PackageVersion Include="Meziantou.Framework.UndoRedo" Version="3.0.0" />
<PackageReference Include="Meziantou.Framework.UndoRedo" />
paket add Meziantou.Framework.UndoRedo --version 3.0.0
#r "nuget: Meziantou.Framework.UndoRedo, 3.0.0"
#:package Meziantou.Framework.UndoRedo@3.0.0
#addin nuget:?package=Meziantou.Framework.UndoRedo&version=3.0.0
#tool nuget:?package=Meziantou.Framework.UndoRedo&version=3.0.0
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
RollbackAsyncexplicitly: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:
UndoRedoManageris 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 anInvalidOperationExceptionas long asActionIsExecutingistrue.
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 | Versions 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. |
-
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.