UndoRedoDelegateAction Class
An IUndoRedoAction defined by a pair of delegates, so an action can be recorded without writing a dedicated subclass. The execute and unexecute delegates can each be synchronous (Action) or asynchronous (Func<T, TResult> returning a ValueTask), and the two may be mixed (e.g. an async execute with a sync unexecute).
public sealed class UndoRedoDelegateAction : UndoRedoActionBaseExample
var savedContents = new List<string>();
async ValueTask SaveAsync(CancellationToken cancellationToken)
{
await Task.Delay(1, cancellationToken);
savedContents.Add("content");
}
void Restore() => savedContents.RemoveAt(savedContents.Count - 1);
// Asynchronous execute, synchronous unexecute
var action = new UndoRedoDelegateAction(
execute: ct => SaveAsync(ct),
unexecute: () => Restore());
var manager = new UndoRedoManager();
await manager.RecordActionAsync(action);
Assert.Equal(["content"], savedContents);
await manager.UndoAsync();
Assert.Empty(savedContents);- Inheritance
UndoRedoActionBaseUndoRedoDelegateAction
Constructors
| Name | Description |
|---|---|
| UndoRedoDelegateAction(Action, Action) | Initializes a new instance with a synchronous execute delegate and a synchronous unexecute delegate. |
| UndoRedoDelegateAction(Action, Func<CancellationToken, ValueTask>) | Initializes a new instance with a synchronous execute delegate and an asynchronous unexecute delegate. |
| UndoRedoDelegateAction(Func<CancellationToken, ValueTask>, Action) | Initializes a new instance with an asynchronous execute delegate and a synchronous unexecute delegate. |
| UndoRedoDelegateAction(Func<CancellationToken, ValueTask>, Func<CancellationToken, ValueTask>) | Initializes a new instance with an asynchronous execute delegate and an asynchronous unexecute delegate. |
UndoRedoDelegateAction overloads
UndoRedoDelegateAction(Action, Action)
UndoRedoDelegateAction.csUndoRedoDelegateAction(Action, Func<CancellationToken, ValueTask>)
UndoRedoDelegateAction.csInitializes a new instance with a synchronous execute delegate and an asynchronous unexecute delegate.
public UndoRedoDelegateAction(Action execute, Func<CancellationToken, ValueTask> unexecute)Parameters
executeAction- The delegate invoked to apply the change.
unexecuteFunc<CancellationToken, ValueTask>- The delegate invoked to revert the change.
UndoRedoDelegateAction(Func<CancellationToken, ValueTask>, Action)
UndoRedoDelegateAction.csInitializes a new instance with an asynchronous execute delegate and a synchronous unexecute delegate.
public UndoRedoDelegateAction(Func<CancellationToken, ValueTask> execute, Action unexecute)Parameters
executeFunc<CancellationToken, ValueTask>- The delegate invoked to apply the change.
unexecuteAction- The delegate invoked to revert the change.
UndoRedoDelegateAction(Func<CancellationToken, ValueTask>, Func<CancellationToken, ValueTask>)
UndoRedoDelegateAction.csInitializes a new instance with an asynchronous execute delegate and an asynchronous unexecute delegate.
public UndoRedoDelegateAction(Func<CancellationToken, ValueTask> execute, Func<CancellationToken, ValueTask> unexecute)Parameters
executeFunc<CancellationToken, ValueTask>- The delegate invoked to apply the change.
unexecuteFunc<CancellationToken, ValueTask>- The delegate invoked to revert the change.