Skip to content

Commit 87d29d3

Browse files
committed
Add AsyncLazyCommand.
1 parent e419e7f commit 87d29d3

File tree

6 files changed

+79
-24
lines changed

6 files changed

+79
-24
lines changed

samples/Unity.Mvvm.ToDoList/Assets/Scripts/ViewModels/MainViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public MainViewModel(IAppContext appContext)
2323
TaskItems = new ObservableCollection<TaskItemData>();
2424
TaskItems.CollectionChanged += OnTaskItemsCollectionChanged;
2525

26-
ShowAddTaskDialogCommand = new AsyncCommand(SetAddTaskDialogActive);
27-
HideAddTaskDialogCommand = new AsyncCommand(HideAddTaskDialogAsync, IsAddTaskDialogActive);
26+
ShowAddTaskDialogCommand = new AsyncLazyCommand(SetAddTaskDialogActive);
27+
HideAddTaskDialogCommand = new AsyncLazyCommand(HideAddTaskDialogAsync, IsAddTaskDialogActive);
2828

2929
SubscribeOnTaskAddMessage(appContext.Resolve<TaskBroker>()).Forget();
3030
}

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/External/UniTask/AsyncCommand.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,23 @@ namespace UnityMvvmToolkit.UniTask
99

1010
public class AsyncCommand : BaseAsyncCommand, IAsyncCommand
1111
{
12-
private readonly Func<CancellationToken, UniTask> _action;
13-
1412
public AsyncCommand(Func<CancellationToken, UniTask> action, Func<bool> canExecute = null) : base(canExecute)
1513
{
16-
_action = action;
14+
Action = action;
1715
}
1816

17+
protected Func<CancellationToken, UniTask> Action { get; }
18+
1919
public void Execute()
2020
{
2121
ExecuteAsync().Forget();
2222
}
2323

24-
public async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
24+
public virtual async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
2525
{
26-
if (ExecuteTask?.Task.Status.IsCompleted() ?? true)
27-
{
28-
ExecuteTask = _action.Invoke(cancellationToken).ToAsyncLazy();
29-
}
30-
3126
try
3227
{
28+
ExecuteTask = Action.Invoke(cancellationToken).ToAsyncLazy();
3329
await ExecuteTask;
3430
}
3531
finally
@@ -41,27 +37,23 @@ public async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
4137

4238
public class AsyncCommand<T> : BaseAsyncCommand, IAsyncCommand<T>
4339
{
44-
private readonly Func<T, CancellationToken, UniTask> _action;
45-
4640
public AsyncCommand(Func<T, CancellationToken, UniTask> action, Func<bool> canExecute = null) : base(canExecute)
4741
{
48-
_action = action;
42+
Action = action;
4943
}
5044

45+
protected Func<T, CancellationToken, UniTask> Action { get; }
46+
5147
public void Execute(T parameter)
5248
{
5349
ExecuteAsync(parameter).Forget();
5450
}
5551

56-
public async UniTask ExecuteAsync(T parameter, CancellationToken cancellationToken = default)
52+
public virtual async UniTask ExecuteAsync(T parameter, CancellationToken cancellationToken = default)
5753
{
58-
if (ExecuteTask?.Task.Status.IsCompleted() ?? true)
59-
{
60-
ExecuteTask = _action.Invoke(parameter, cancellationToken).ToAsyncLazy();
61-
}
62-
6354
try
6455
{
56+
ExecuteTask = Action.Invoke(parameter, cancellationToken).ToAsyncLazy();
6557
await ExecuteTask;
6658
}
6759
finally

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/External/UniTask/AsyncCommand.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#if UNITYMVVMTOOLKIT_UNITASK_SUPPORT
2+
3+
namespace UnityMvvmToolkit.UniTask
4+
{
5+
using System;
6+
using System.Threading;
7+
using Cysharp.Threading.Tasks;
8+
9+
public class AsyncLazyCommand : AsyncCommand
10+
{
11+
public AsyncLazyCommand(Func<CancellationToken, UniTask> action, Func<bool> canExecute = null)
12+
: base(action, canExecute)
13+
{
14+
}
15+
16+
public override async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
17+
{
18+
try
19+
{
20+
if (ExecuteTask?.Task.Status.IsCompleted() ?? true)
21+
{
22+
ExecuteTask = Action.Invoke(cancellationToken).ToAsyncLazy();
23+
}
24+
25+
await ExecuteTask;
26+
}
27+
finally
28+
{
29+
ExecuteTask = null;
30+
}
31+
}
32+
}
33+
34+
public class AsyncLazyCommand<T> : AsyncCommand<T>
35+
{
36+
public AsyncLazyCommand(Func<T, CancellationToken, UniTask> action, Func<bool> canExecute = null)
37+
: base(action, canExecute)
38+
{
39+
}
40+
41+
public override async UniTask ExecuteAsync(T parameter, CancellationToken cancellationToken = default)
42+
{
43+
try
44+
{
45+
if (ExecuteTask?.Task.Status.IsCompleted() ?? true)
46+
{
47+
ExecuteTask = Action.Invoke(parameter, cancellationToken).ToAsyncLazy();
48+
}
49+
50+
await ExecuteTask;
51+
}
52+
finally
53+
{
54+
ExecuteTask = null;
55+
}
56+
}
57+
}
58+
}
59+
60+
#endif

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/External/UniTask/AsyncLazyCommand.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/External/UniTask/BaseAsyncCommand.cs.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)