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

Add concurrency control to async relay commands #1

Closed
Sergio0694 opened this issue Nov 2, 2021 · 0 comments · Fixed by #30
Closed

Add concurrency control to async relay commands #1

Sergio0694 opened this issue Nov 2, 2021 · 0 comments · Fixed by #30
Assignees
Labels
feature request 📬 A request for new changes to improve functionality improvements ✨ Improvements to an existing functionality mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit need documentation 📃 This change needs a related documentation PR

Comments

@Sergio0694
Copy link
Member

Describe the problem this feature would solve

Right now the two async command types in the MVVM Toolkit (AsyncRelayCommand and AsyncRelayCommand<T>) always allow asynchronous actions to be executed concurrently, if triggered more than once, with no easy way to just restrict only one to be executing at any given time. There should be built-in support for this that is easily configurable.

Describe the solution

This is the API breakdown for the proposed change:

  namespace CommunityToolkit.Mvvm.Input
 {
     public sealed class AsyncRelayCommand : IAsyncRelayCommand
     {
         public event EventHandler? CanExecuteChanged;
 
         public AsyncRelayCommand(Func<Task> execute);
+        public AsyncRelayCommand(Func<Task> execute, bool allowConcurrentExecutions);
         public AsyncRelayCommand(Func<CancellationToken, Task> cancelableExecute);
+        public AsyncRelayCommand(Func<CancellationToken, Task> cancelableExecute, bool allowConcurrentExecutions);
         public AsyncRelayCommand(Func<Task> execute, Func<bool> canExecute);
+        public AsyncRelayCommand(Func<Task> execute, Func<bool> canExecute, bool allowConcurrentExecutions);
         public AsyncRelayCommand(Func<CancellationToken, Task> cancelableExecute, Func<bool> canExecute);
+        public AsyncRelayCommand(Func<CancellationToken, Task> cancelableExecute, Func<bool> canExecute, bool allowConcurrentExecutions);
     }
 
     public sealed class AsyncRelayCommand<T> : IAsyncRelayCommand<T>
     {
         public event EventHandler? CanExecuteChanged;

         public AsyncRelayCommand(Func<T?, Task> execute);
+        public AsyncRelayCommand(Func<T, Task> execute, bool allowConcurrentExecutions);
         public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancelableExecute);
+        public AsyncRelayCommand(Func<T, CancellationToken, Task> cancelableExecute, bool allowConcurrentExecutions);
         public AsyncRelayCommand(Func<T?, Task> execute, Predicate<T?> canExecute);
+        public AsyncRelayCommand(Func<T, Task> execute, Predicate<T> canExecute, bool allowConcurrentExecutions);
         public AsyncRelayCommand(Func<T?, CancellationToken, Task> cancelableExecute, Predicate<T?> canExecute);
+        public AsyncRelayCommand(Func<T, CancellationToken, Task> cancelableExecute, Predicate<T> canExecute, bool allowConcurrentExecutions);
     }
 }

The default execution mode would be the same as today (when no allowConcurrentExecutions parameter is passed).
This ensures that existing consumers won't experience a difference in behavior when upgrading to a new version.

Describe alternatives you've considered

Just letting consumers handle this manually, which is extremely clunky and not really intuitive.

@Sergio0694 Sergio0694 added feature request 📬 A request for new changes to improve functionality improvements ✨ Improvements to an existing functionality need documentation 📃 This change needs a related documentation PR labels Nov 2, 2021
@Sergio0694 Sergio0694 self-assigned this Nov 2, 2021
@Sergio0694 Sergio0694 added the mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit label Nov 11, 2021
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 improvements ✨ Improvements to an existing functionality mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit need documentation 📃 This change needs a related documentation PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant