Skip to content

Commit

Permalink
fix #2866 CanExecute Scheduler (#2880)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Aug 17, 2021
1 parent 42e2e90 commit ac8b89a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/ReactiveUI.Tests/Commands/ReactiveCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using DynamicData;
Expand Down Expand Up @@ -1183,5 +1184,29 @@ public async Task ReactiveCommandCreateFromTaskHandlesTaskExceptionAsync()
Assert.False(isExecuting);
Assert.Equal("break execution", fail?.Message);
}

[Fact]
public async Task ReactiveCommandExecutesFromInvokeCommand()
{
var semaphore = new SemaphoreSlim(0);
var command = ReactiveCommand.Create(() => semaphore.Release());

Observable.Return(Unit.Default)
.InvokeCommand(command);

var result = 0;
var task = semaphore.WaitAsync();
if (await Task.WhenAny(Task.Delay(TimeSpan.FromMilliseconds(100)), task).ConfigureAwait(true) == task)
{
result = 1;
}
else
{
result = -1;
}

await Task.Delay(200).ConfigureAwait(false);
Assert.Equal(1, result);
}
}
}
7 changes: 4 additions & 3 deletions src/ReactiveUI/ReactiveCommand/ReactiveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -766,13 +766,14 @@ public class ReactiveCommand<TParam, TResult> : ReactiveCommandBase<TParam, TRes
.CombineLatest(_isExecuting, (canEx, isEx) => canEx && !isEx)
.DistinctUntilChanged()
.Replay(1)
.RefCount()
.ObserveOn(_canExecuteScheduler);
.RefCount();

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

_canExecuteSubscription = _canExecute.Subscribe(OnCanExecuteChanged);
_canExecuteSubscription = _canExecute
.ObserveOn(_canExecuteScheduler)
.Subscribe(OnCanExecuteChanged);
}

private enum ExecutionDemarcation
Expand Down

0 comments on commit ac8b89a

Please sign in to comment.