Skip to content

Commit 3bdc61e

Browse files
committed
Optimize async command memory usage and add IsRunning property.
1 parent 473bd4c commit 3bdc61e

File tree

6 files changed

+87
-37
lines changed

6 files changed

+87
-37
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,56 @@ namespace UnityMvvmToolkit.UniTask
99

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

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

24-
public virtual async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
24+
public async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
2525
{
2626
try
2727
{
28-
ExecuteTask = Action.Invoke(cancellationToken).ToAsyncLazy();
29-
await ExecuteTask;
28+
IsRunning = true;
29+
await _action.Invoke(cancellationToken).ToAsyncLazy();
3030
}
3131
finally
3232
{
33-
ExecuteTask = null;
33+
IsRunning = false;
3434
}
3535
}
3636
}
3737

3838
public class AsyncCommand<T> : BaseAsyncCommand, IAsyncCommand<T>
3939
{
40+
private readonly Func<T, CancellationToken, UniTask> _action;
41+
4042
public AsyncCommand(Func<T, CancellationToken, UniTask> action, Func<bool> canExecute = null) : base(canExecute)
4143
{
42-
Action = action;
44+
_action = action;
4345
}
4446

45-
protected Func<T, CancellationToken, UniTask> Action { get; }
46-
4747
public void Execute(T parameter)
4848
{
4949
ExecuteAsync(parameter).Forget();
5050
}
5151

52-
public virtual async UniTask ExecuteAsync(T parameter, CancellationToken cancellationToken = default)
52+
public async UniTask ExecuteAsync(T parameter, CancellationToken cancellationToken = default)
5353
{
5454
try
5555
{
56-
ExecuteTask = Action.Invoke(parameter, cancellationToken).ToAsyncLazy();
57-
await ExecuteTask;
56+
IsRunning = true;
57+
await _action.Invoke(parameter, cancellationToken);
5858
}
5959
finally
6060
{
61-
ExecuteTask = null;
61+
IsRunning = false;
6262
}
6363
}
6464
}

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,72 @@
33
namespace UnityMvvmToolkit.UniTask
44
{
55
using System;
6+
using Interfaces;
67
using System.Threading;
78
using Cysharp.Threading.Tasks;
89

9-
public class AsyncLazyCommand : AsyncCommand
10+
public class AsyncLazyCommand : BaseAsyncLazyCommand, IAsyncCommand
1011
{
11-
public AsyncLazyCommand(Func<CancellationToken, UniTask> action, Func<bool> canExecute = null)
12-
: base(action, canExecute)
12+
private readonly Func<CancellationToken, UniTask> _action;
13+
14+
public AsyncLazyCommand(Func<CancellationToken, UniTask> action, Func<bool> canExecute = null)
15+
: base(canExecute)
16+
{
17+
_action = action;
18+
}
19+
20+
public void Execute()
1321
{
22+
ExecuteAsync().Forget();
1423
}
1524

16-
public override async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
25+
public async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
1726
{
1827
try
1928
{
20-
if (ExecuteTask?.Task.Status.IsCompleted() ?? true)
29+
if (IsRunning == false)
2130
{
22-
ExecuteTask = Action.Invoke(cancellationToken).ToAsyncLazy();
31+
ExecutionTask = _action.Invoke(cancellationToken).ToAsyncLazy();
2332
}
2433

25-
await ExecuteTask;
34+
await ExecutionTask;
2635
}
2736
finally
2837
{
29-
ExecuteTask = null;
38+
ExecutionTask = null;
3039
}
3140
}
3241
}
3342

34-
public class AsyncLazyCommand<T> : AsyncCommand<T>
43+
public class AsyncLazyCommand<T> : BaseAsyncLazyCommand, IAsyncCommand<T>
3544
{
36-
public AsyncLazyCommand(Func<T, CancellationToken, UniTask> action, Func<bool> canExecute = null)
37-
: base(action, canExecute)
45+
private readonly Func<T, CancellationToken, UniTask> _action;
46+
47+
public AsyncLazyCommand(Func<T, CancellationToken, UniTask> action, Func<bool> canExecute = null)
48+
: base(canExecute)
49+
{
50+
_action = action;
51+
}
52+
53+
public void Execute(T parameter)
3854
{
55+
ExecuteAsync(parameter).Forget();
3956
}
4057

41-
public override async UniTask ExecuteAsync(T parameter, CancellationToken cancellationToken = default)
58+
public async UniTask ExecuteAsync(T parameter, CancellationToken cancellationToken = default)
4259
{
4360
try
4461
{
45-
if (ExecuteTask?.Task.Status.IsCompleted() ?? true)
62+
if (IsRunning == false)
4663
{
47-
ExecuteTask = Action.Invoke(parameter, cancellationToken).ToAsyncLazy();
64+
ExecutionTask = _action.Invoke(parameter, cancellationToken).ToAsyncLazy();
4865
}
4966

50-
await ExecuteTask;
67+
await ExecutionTask;
5168
}
5269
finally
5370
{
54-
ExecuteTask = null;
71+
ExecutionTask = null;
5572
}
5673
}
5774
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@ namespace UnityMvvmToolkit.UniTask
55
using Core;
66
using System;
77
using Interfaces;
8-
using Cysharp.Threading.Tasks;
98
using System.Runtime.CompilerServices;
109

1110
public abstract class BaseAsyncCommand : BaseCommand, IBaseAsyncCommand
1211
{
13-
private AsyncLazy _executeTask;
12+
private bool _isRunning;
1413

1514
protected BaseAsyncCommand(Func<bool> canExecute) : base(canExecute)
1615
{
1716
}
1817

1918
public bool DisableOnExecution { get; set; }
2019

21-
protected AsyncLazy ExecuteTask
20+
public virtual bool IsRunning
2221
{
23-
get => _executeTask;
24-
set
22+
get => _isRunning;
23+
protected set
2524
{
26-
_executeTask = value;
25+
_isRunning = value;
2726
RaiseCanExecuteChanged();
2827
}
2928
}
@@ -36,7 +35,7 @@ public override bool CanExecute()
3635
return base.CanExecute();
3736
}
3837

39-
return base.CanExecute() && (ExecuteTask?.Task.Status.IsCompleted() ?? true);
38+
return IsRunning == false && base.CanExecute();
4039
}
4140
}
4241
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#if UNITYMVVMTOOLKIT_UNITASK_SUPPORT
2+
3+
namespace UnityMvvmToolkit.UniTask
4+
{
5+
using System;
6+
using Cysharp.Threading.Tasks;
7+
8+
public abstract class BaseAsyncLazyCommand : BaseAsyncCommand
9+
{
10+
private AsyncLazy _executionTask;
11+
12+
protected BaseAsyncLazyCommand(Func<bool> canExecute) : base(canExecute)
13+
{
14+
}
15+
16+
public override bool IsRunning => ExecutionTask is { Task: { Status: UniTaskStatus.Pending } };
17+
18+
protected AsyncLazy ExecutionTask
19+
{
20+
get => _executionTask;
21+
set
22+
{
23+
_executionTask = value;
24+
RaiseCanExecuteChanged();
25+
}
26+
}
27+
}
28+
}
29+
30+
#endif

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/External/UniTask/BaseAsyncLazyCommand.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/Interfaces/IBaseAsyncCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace UnityMvvmToolkit.UniTask.Interfaces
44
{
55
public interface IBaseAsyncCommand
66
{
7+
bool IsRunning { get; }
78
bool DisableOnExecution { get; set; }
89
}
910
}

0 commit comments

Comments
 (0)