Skip to content

Commit

Permalink
(#421) Refactor TimeSpan with Humanizer (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
jibedoubleve authored Jan 3, 2024
1 parent 7d782f8 commit bf7f603
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/Lanceur/Views/AppSettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using Humanizer;

namespace Lanceur.Views
{
Expand Down Expand Up @@ -105,7 +106,7 @@ public AppSettingsViewModel(
private TimeSpan GetDelay()
{
var delay = _settingsFacade.Application.RestartDelay;
var time = TimeSpan.FromMilliseconds(delay);
var time = delay.Milliseconds();
return time;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Lanceur/Views/KeywordsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Humanizer;

namespace Lanceur.Views
{
Expand Down Expand Up @@ -236,7 +237,7 @@ private void SetupBindings(IScheduler uiThread, CompositeDisposable d)

this.WhenAnyValue(vm => vm.SearchQuery)
.DistinctUntilChanged()
.Throttle(TimeSpan.FromMilliseconds(10), scheduler: uiThread)
.Throttle(10.Milliseconds(), scheduler: uiThread)
.Select(x => new SearchRequest(x?.Trim(), AliasToCreate))
.Log(this, "Invoking search.", c => $"With criterion '{c.Query}' and alias to create '{c.AliasToCreate?.Name ?? "<EMPTY>"}'")
.InvokeCommand(Search)
Expand Down
3 changes: 2 additions & 1 deletion src/Lanceur/Views/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Humanizer;

namespace Lanceur.Views
{
Expand Down Expand Up @@ -138,7 +139,7 @@ public MainViewModel(
#region Query

this.WhenAnyValue(vm => vm.Query.Value)
.Throttle(TimeSpan.FromMilliseconds(100), schedulerProvider.TaskpoolScheduler)
.Throttle(100.Milliseconds(), schedulerProvider.TaskpoolScheduler)
.Select(x => x.Trim())
.Where(x => !x.IsNullOrWhiteSpace())
.Log(this, "Query changed.", x => $"'{x}'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using NSubstitute;
using ReactiveUI.Testing;
using System.Reactive.Concurrency;
using FluentAssertions.Extensions;
using Xunit;

namespace Lanceur.Tests.ViewModels
Expand All @@ -30,7 +31,7 @@ public void ExecuteA_NOT_ExecutableQueryResult()
.With(scheduler)
.Build();

scheduler.Schedule(TimeSpan.FromTicks(00), () => vm.CurrentAlias = new NotExecutableTestAlias());
scheduler.Schedule(0.Ticks(), () => vm.CurrentAlias = new NotExecutableTestAlias());

var results = scheduler.Start(
() => vm.ExecuteAlias.CanExecute,
Expand All @@ -55,7 +56,7 @@ public void ExecuteAnExecutableQueryResult()
.With(scheduler)
.Build();

scheduler.Schedule(TimeSpan.FromTicks(00), () => vm.CurrentAlias = new ExecutableTestAlias());
scheduler.Schedule(0.Ticks(), () => vm.CurrentAlias = new ExecutableTestAlias());

var results = scheduler.Start(
() => vm.ExecuteAlias.CanExecute,
Expand Down
13 changes: 7 additions & 6 deletions src/Tests/Lanceur.Tests/ViewModels/MainViewModelShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using ReactiveUI.Testing;
using Splat;
using System.Reactive.Concurrency;
using FluentAssertions.Extensions;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -90,20 +91,20 @@ public void NotifyWhenCriterionChanges()
.Build();

scheduler.Schedule(() => vm.Query.Value = "a");
scheduler.Schedule(TimeSpan.FromTicks(200), () => vm.Query.Value += "b");
scheduler.Schedule(TimeSpan.FromTicks(300), () => vm.Query.Value += "c");
scheduler.Schedule(TimeSpan.FromTicks(400), () => vm.Query.Value += "d");
scheduler.Schedule(200.Ticks(), () => vm.Query.Value += "b");
scheduler.Schedule(300.Ticks(), () => vm.Query.Value += "c");
scheduler.Schedule(400.Ticks(), () => vm.Query.Value += "d");

var results = scheduler.Start(
() => vm.SearchAlias.IsExecuting,
created: 0,
subscribed: 100,
disposed: TimeSpan.FromMilliseconds(1_000).Ticks);
disposed: 1_000.Milliseconds().Ticks);

results.Messages.AssertEqual(
OnNext(100, false),
OnNext(TimeSpan.FromMilliseconds(100).Ticks + 402, true),
OnNext(TimeSpan.FromMilliseconds(100).Ticks + 404, false)
OnNext(100.Milliseconds().Ticks + 402, true),
OnNext(100.Milliseconds().Ticks + 404, false)
);
});
}
Expand Down

0 comments on commit bf7f603

Please sign in to comment.