Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIs to create cancellation commands for AsyncRelayCommand types #118

Closed
Sergio0694 opened this issue Feb 21, 2022 · 1 comment · Fixed by #128
Closed

APIs to create cancellation commands for AsyncRelayCommand types #118

Sergio0694 opened this issue Feb 21, 2022 · 1 comment · Fixed by #128
Labels
feature request 📬 A request for new changes to improve functionality mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit open discussion ☎️ An issue open for active community discussion
Projects

Comments

@Sergio0694
Copy link
Member

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

@Sergio0694 Sergio0694 added feature request 📬 A request for new changes to improve functionality open discussion ☎️ An issue open for active community discussion mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit labels Feb 21, 2022
@michael-hawker michael-hawker added this to To do in 8.0 via automation Feb 24, 2022
@michael-hawker
Copy link
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.

8.0 automation moved this from To do to Done Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request 📬 A request for new changes to improve functionality mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit open discussion ☎️ An issue open for active community discussion
Projects
No open projects
8.0
Done
Development

Successfully merging a pull request may close this issue.

2 participants