Skip to content

Commit f0c575b

Browse files
committed
Close #5. Make async command IsRunning observable.
1 parent a808c0c commit f0c575b

File tree

8 files changed

+50
-36
lines changed

8 files changed

+50
-36
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public AsyncCommand(Func<T, CancellationToken, UniTask> action, Func<bool> canEx
1818

1919
public void Execute(T parameter)
2020
{
21-
if (IsRunning && AllowConcurrency == false)
21+
if (IsCommandRunning && AllowConcurrency == false)
2222
{
2323
return;
2424
}
@@ -30,12 +30,13 @@ public async UniTask ExecuteAsync(T parameter, CancellationToken cancellationTok
3030
{
3131
try
3232
{
33-
IsRunning = true;
33+
SetCommandRunning(true);
34+
3435
await _action(parameter, cancellationToken);
3536
}
3637
finally
3738
{
38-
IsRunning = false;
39+
SetCommandRunning(false);
3940
}
4041
}
4142
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public AsyncCommand(Func<CancellationToken, UniTask> action, Func<bool> canExecu
1818

1919
public void Execute()
2020
{
21-
if (IsRunning && AllowConcurrency == false)
21+
if (IsCommandRunning && AllowConcurrency == false)
2222
{
2323
return;
2424
}
@@ -30,12 +30,13 @@ public async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
3030
{
3131
try
3232
{
33-
IsRunning = true;
33+
SetCommandRunning(true);
34+
3435
await _action(cancellationToken);
3536
}
3637
finally
3738
{
38-
IsRunning = false;
39+
SetCommandRunning(false);
3940
}
4041
}
4142
}

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,24 @@ namespace UnityMvvmToolkit.UniTask
77
using Interfaces;
88
using Extensions;
99
using System.Runtime.CompilerServices;
10+
using UnityMvvmToolkit.Core.Interfaces;
1011

1112
public abstract class BaseAsyncCommand : BaseCommand, IBaseAsyncCommand
1213
{
13-
private bool _isRunning;
14+
private readonly IProperty<bool> _isRunning;
1415

1516
protected BaseAsyncCommand(Func<bool> canExecute) : base(canExecute)
1617
{
17-
}
18-
19-
public virtual bool IsRunning
20-
{
21-
get => _isRunning;
22-
protected set
23-
{
24-
_isRunning = value;
25-
RaiseCanExecuteChanged();
26-
}
18+
_isRunning = new Property<bool>();
2719
}
2820

2921
public bool AllowConcurrency { get; set; }
30-
3122
public virtual bool DisableOnExecution { get; set; }
3223

24+
public IReadOnlyProperty<bool> IsRunning => _isRunning;
25+
26+
protected bool IsCommandRunning { get; private set; }
27+
3328
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3429
public override bool CanExecute()
3530
{
@@ -38,14 +33,28 @@ public override bool CanExecute()
3833
return base.CanExecute();
3934
}
4035

41-
return IsRunning == false && base.CanExecute();
36+
return IsCommandRunning == false && base.CanExecute();
4237
}
4338

4439
public virtual void Cancel()
4540
{
4641
throw new InvalidOperationException(
4742
$"To make the 'AsyncCommand' cancelable, use '{nameof(AsyncCommandExtensions.WithCancellation)}' extension.");
4843
}
44+
45+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
46+
protected void SetCommandRunning(bool isRunning)
47+
{
48+
_isRunning.Value = isRunning;
49+
50+
if (IsCommandRunning == isRunning)
51+
{
52+
return;
53+
}
54+
55+
IsCommandRunning = isRunning;
56+
RaiseCanExecuteChanged();
57+
}
4958
}
5059
}
5160

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/External/UniTask/Interfaces/IBaseAsyncCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace UnityMvvmToolkit.UniTask.Interfaces
44
{
5+
using UnityMvvmToolkit.Core.Interfaces;
6+
57
public interface IBaseAsyncCommand
68
{
7-
bool IsRunning { get; }
89
bool AllowConcurrency { get; set; }
910
bool DisableOnExecution { get; set; }
1011

12+
IReadOnlyProperty<bool> IsRunning { get; }
13+
1114
void Cancel();
1215
}
1316
}

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/External/UniTask/Internal/AsyncCommandWithCancellation.T.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public AsyncCommandWithCancellation(IAsyncCommand<T> asyncCommand) : base(null)
2222
_runningCommands = new ConcurrentQueue<UniTask>();
2323
}
2424

25-
public override bool IsRunning { get; protected set; }
26-
2725
public override bool DisableOnExecution
2826
{
2927
get => _asyncCommand.DisableOnExecution;
@@ -38,7 +36,7 @@ public override event EventHandler<bool> CanExecuteChanged
3836

3937
public void Execute(T parameter)
4038
{
41-
if (IsRunning)
39+
if (IsCommandRunning)
4240
{
4341
TryEnqueueAsyncCommand(parameter, _cancellationTokenSource.Token);
4442
}
@@ -54,7 +52,7 @@ public async UniTask ExecuteAsync(T parameter, CancellationToken cancellationTok
5452

5553
try
5654
{
57-
IsRunning = true;
55+
SetCommandRunning(true);
5856

5957
TryEnqueueAsyncCommand(parameter, _cancellationTokenSource.Token);
6058

@@ -65,7 +63,7 @@ public async UniTask ExecuteAsync(T parameter, CancellationToken cancellationTok
6563
}
6664
finally
6765
{
68-
IsRunning = false;
66+
SetCommandRunning(false);
6967

7068
_cancellationTokenSource?.Dispose();
7169
_cancellationTokenSource = null;

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/External/UniTask/Internal/AsyncCommandWithCancellation.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public AsyncCommandWithCancellation(IAsyncCommand asyncCommand) : base(null)
2222
_runningCommands = new ConcurrentQueue<UniTask>();
2323
}
2424

25-
public override bool IsRunning { get; protected set; }
26-
2725
public override bool DisableOnExecution
2826
{
2927
get => _asyncCommand.DisableOnExecution;
@@ -38,7 +36,7 @@ public override event EventHandler<bool> CanExecuteChanged
3836

3937
public void Execute()
4038
{
41-
if (IsRunning)
39+
if (IsCommandRunning)
4240
{
4341
TryEnqueueAsyncCommand(_cancellationTokenSource.Token);
4442
}
@@ -54,7 +52,7 @@ public async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
5452

5553
try
5654
{
57-
IsRunning = true;
55+
SetCommandRunning(true);
5856

5957
TryEnqueueAsyncCommand(_cancellationTokenSource.Token);
6058

@@ -65,7 +63,7 @@ public async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
6563
}
6664
finally
6765
{
68-
IsRunning = false;
66+
SetCommandRunning(false);
6967

7068
_cancellationTokenSource?.Dispose();
7169
_cancellationTokenSource = null;

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/External/UniTask/Internal/AsyncLazyCommandWithCancellation.T.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public AsyncLazyCommandWithCancellation(IAsyncCommand<T> asyncCommand) : base(nu
1717
_asyncCommand = asyncCommand;
1818
}
1919

20-
public override bool IsRunning => _asyncCommand.IsRunning;
21-
2220
public override bool DisableOnExecution
2321
{
2422
get => _asyncCommand.DisableOnExecution;
@@ -33,7 +31,7 @@ public override event EventHandler<bool> CanExecuteChanged
3331

3432
public void Execute(T parameter)
3533
{
36-
if (IsRunning)
34+
if (IsCommandRunning)
3735
{
3836
return;
3937
}
@@ -47,10 +45,14 @@ public async UniTask ExecuteAsync(T parameter, CancellationToken cancellationTok
4745

4846
try
4947
{
48+
SetCommandRunning(true);
49+
5050
await _asyncCommand.ExecuteAsync(parameter, _cancellationTokenSource.Token);
5151
}
5252
finally
5353
{
54+
SetCommandRunning(false);
55+
5456
_cancellationTokenSource?.Dispose();
5557
_cancellationTokenSource = null;
5658
}

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/External/UniTask/Internal/AsyncLazyCommandWithCancellation.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public AsyncLazyCommandWithCancellation(IAsyncCommand asyncCommand) : base(null)
1717
_asyncCommand = asyncCommand;
1818
}
1919

20-
public override bool IsRunning => _asyncCommand.IsRunning;
21-
2220
public override bool DisableOnExecution
2321
{
2422
get => _asyncCommand.DisableOnExecution;
@@ -33,7 +31,7 @@ public override event EventHandler<bool> CanExecuteChanged
3331

3432
public void Execute()
3533
{
36-
if (IsRunning)
34+
if (IsCommandRunning)
3735
{
3836
return;
3937
}
@@ -47,10 +45,14 @@ public async UniTask ExecuteAsync(CancellationToken cancellationToken = default)
4745

4846
try
4947
{
48+
SetCommandRunning(true);
49+
5050
await _asyncCommand.ExecuteAsync(_cancellationTokenSource.Token);
5151
}
5252
finally
5353
{
54+
SetCommandRunning(false);
55+
5456
_cancellationTokenSource?.Dispose();
5557
_cancellationTokenSource = null;
5658
}

0 commit comments

Comments
 (0)