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 : UndoRedoActionBase

Example

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
UndoRedoActionBase UndoRedoDelegateAction

Constructors

NameDescription
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.cs

Initializes a new instance with a synchronous execute delegate and a synchronous unexecute delegate.

public UndoRedoDelegateAction(Action execute, Action unexecute)
Parameters
execute Action
The delegate invoked to apply the change.
unexecute Action
The delegate invoked to revert the change.

UndoRedoDelegateAction(Action, Func<CancellationToken, ValueTask>)

UndoRedoDelegateAction.cs

Initializes a new instance with a synchronous execute delegate and an asynchronous unexecute delegate.

public UndoRedoDelegateAction(Action execute, Func<CancellationToken, ValueTask> unexecute)
Parameters
execute Action
The delegate invoked to apply the change.
unexecute Func<CancellationToken, ValueTask>
The delegate invoked to revert the change.

UndoRedoDelegateAction(Func<CancellationToken, ValueTask>, Action)

UndoRedoDelegateAction.cs

Initializes a new instance with an asynchronous execute delegate and a synchronous unexecute delegate.

public UndoRedoDelegateAction(Func<CancellationToken, ValueTask> execute, Action unexecute)
Parameters
execute Func<CancellationToken, ValueTask>
The delegate invoked to apply the change.
unexecute Action
The delegate invoked to revert the change.

UndoRedoDelegateAction(Func<CancellationToken, ValueTask>, Func<CancellationToken, ValueTask>)

UndoRedoDelegateAction.cs

Initializes a new instance with an asynchronous execute delegate and an asynchronous unexecute delegate.

public UndoRedoDelegateAction(Func<CancellationToken, ValueTask> execute, Func<CancellationToken, ValueTask> unexecute)
Parameters
execute Func<CancellationToken, ValueTask>
The delegate invoked to apply the change.
unexecute Func<CancellationToken, ValueTask>
The delegate invoked to revert the change.