Skip to content

APIs to create cancellation commands for AsyncRelayCommand types #118

Closed

Description

@Sergio0694

Overview and motivation

The MVVM Toolkit has the AsyncRelayCommand and AsyncRelayCommand<T> types, which make it easy to work with asynchronous operations through the usual ICommand pattern, while also adding support for cancellation, progress report, etc. They have one limitation though, which is that it's not easy to define a command to cancel another command. Suppose you had this:

partial class MyViewModel : ObservableObject
{
    [ICommand(AllowConcurrentExecutions = false)]
    private async Task DoStuffAsync(CancellationToken token)
    {
    }
}
<Button Content="Start" Command="{x:Bind ViewModel.DoStuffCommand}"/>

This works fine. But now suppose you wanted to also have a button to cancel that operation. You'd need this:

<Button Content="Start" Command="{x:Bind ViewModel.DoStuffCommand}"/>
<Button
    Content="Cancel"
    Click="{x:Bind ViewModel.DoStuffCommand.Cancel}"
    IsEnabled="{x:Bind ViewModel.DoStuffCommand.CanBeCanceled}"

This works, but it's (1) not portable (eg. doesn't work if the framework doesn't allow binding to methods), and (2) it doesn't follow the same usual command pattern: we're binding an event directly to a method in the viewmodel. It would be nice to have a way to create a command to handle this, but this requires a rather clunky setup today, as you need to create a command wrapping the other one, and also subscribe to PropertyChanged on the first command, monitor for CanBeCanceled changing and then update the second command from there. It's not really user friendly and also quite error prone, plus it could be optimized if supported internally.

API proposal

We could add an extension like this:

namespace CommunityToolkit.Mvvm.Input;

public static class IAsyncRelayCommandExtensions
{
    public static ICommand CreateCancelCommand(this IAsyncRelayCommand command);
}

This could then be used as follows:

class MyViewModel : ObservableObject
{
    public MyViewModel()
    {
        DoStuffCommand = new AsyncRelayCommand(DoStuffAsync);
        CancelDoStuffCommand = DoStuffCommand.CreateCancelCommand();
    }

    public IAsyncRelayCommand DoStuffCommand { get; }
    public ICommand CancelDoStuffCommand { get; }
    
    private async Task DoStuffAsync(CancellationToken token)
    {
    }
}
<Button Content="Start" Command="{x:Bind ViewModel.DoStuffCommand}"/>
<Button Content="Cancel" Command="{x:Bind ViewModel.CancelDoStuffCommand}"/>

We could then also add some way to automatically create a cancel command through source generators, like:

partial class MyViewModel : ObservableObject
{
    [ICommand(AllowConcurrentExecutions = false, IncludeCancelCommand = true)]
    private async Task DoStuffAsync(CancellationToken token)
    {
    }
}

Which would generate the pair of command and cancel command.

Thoughts? Other API ideas or alternatives? 😄

cc. @michael-hawker @Arlodotexe

Activity

  1. michael-hawker commented on Feb 24, 2022

    @michael-hawker
    Member

    Seems like a great idea that'll be helpful for developers.

    Think the main concern is to make sure the ICommand generator has analyzers to warn developers if they're trying to set async options on a non-async method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request 📬A request for new changes to improve functionalitymvvm-toolkit 🧰Issues/PRs for the MVVM Toolkitopen discussion ☎️An issue open for active community discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions