// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input.Internals;
namespace CommunityToolkit.Mvvm.Input;
///
/// Extensions for the type.
///
public static class IAsyncRelayCommandExtensions
{
///
/// Creates an instance that can be used to cancel execution on the input command.
/// The returned command will also notify when it can be executed based on the state of the wrapped command.
///
/// The input instance to create a cancellation command for.
/// An instance that can be used to monitor and signal cancellation for .
/// The returned instance is not guaranteed to be unique across multiple invocations with the same arguments.
/// Thrown if is .
public static ICommand CreateCancelCommand(this IAsyncRelayCommand command)
{
ArgumentNullException.ThrowIfNull(command);
// If the command is known not to ever allow cancellation, just reuse the same instance
if (command is ICancellationAwareCommand { IsCancellationSupported: false })
{
return DisabledCommand.Instance;
}
// Create a new cancel command wrapping the input one
return new CancelCommand(command);
}
}