Skip to content

Commit ac8b89a

Browse files
authored
fix #2866 CanExecute Scheduler (#2880)
1 parent 42e2e90 commit ac8b89a

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/ReactiveUI.Tests/Commands/ReactiveCommandTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Reactive.Concurrency;
1010
using System.Reactive.Linq;
1111
using System.Reactive.Subjects;
12+
using System.Threading;
1213
using System.Threading.Tasks;
1314
using System.Windows.Input;
1415
using DynamicData;
@@ -1183,5 +1184,29 @@ public async Task ReactiveCommandCreateFromTaskHandlesTaskExceptionAsync()
11831184
Assert.False(isExecuting);
11841185
Assert.Equal("break execution", fail?.Message);
11851186
}
1187+
1188+
[Fact]
1189+
public async Task ReactiveCommandExecutesFromInvokeCommand()
1190+
{
1191+
var semaphore = new SemaphoreSlim(0);
1192+
var command = ReactiveCommand.Create(() => semaphore.Release());
1193+
1194+
Observable.Return(Unit.Default)
1195+
.InvokeCommand(command);
1196+
1197+
var result = 0;
1198+
var task = semaphore.WaitAsync();
1199+
if (await Task.WhenAny(Task.Delay(TimeSpan.FromMilliseconds(100)), task).ConfigureAwait(true) == task)
1200+
{
1201+
result = 1;
1202+
}
1203+
else
1204+
{
1205+
result = -1;
1206+
}
1207+
1208+
await Task.Delay(200).ConfigureAwait(false);
1209+
Assert.Equal(1, result);
1210+
}
11861211
}
11871212
}

src/ReactiveUI/ReactiveCommand/ReactiveCommand.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,13 +766,14 @@ protected internal ReactiveCommand(
766766
.CombineLatest(_isExecuting, (canEx, isEx) => canEx && !isEx)
767767
.DistinctUntilChanged()
768768
.Replay(1)
769-
.RefCount()
770-
.ObserveOn(_canExecuteScheduler);
769+
.RefCount();
771770

772771
_results = _synchronizedExecutionInfo.Where(x => x.Demarcation == ExecutionDemarcation.Result)
773772
.Select(x => x.Result);
774773

775-
_canExecuteSubscription = _canExecute.Subscribe(OnCanExecuteChanged);
774+
_canExecuteSubscription = _canExecute
775+
.ObserveOn(_canExecuteScheduler)
776+
.Subscribe(OnCanExecuteChanged);
776777
}
777778

778779
private enum ExecutionDemarcation

0 commit comments

Comments
 (0)