Skip to content

Commit

Permalink
(#423) Retrieve thumbnails is done a a separate thread (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
jibedoubleve authored Jan 1, 2024
1 parent b94656f commit a2d4b34
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Lanceur.Core/Managers/IThumbnailManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IThumbnailManager
/// when thread has done its work
/// </summary>
/// <param name="results">The <see cref="QueryResult"/> to refresh</param>
void RefreshThumbnails(IEnumerable<QueryResult> results);
Task RefreshThumbnails(IEnumerable<QueryResult> results);

#endregion Methods
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lanceur.Infra.Win32/Thumbnails/ThumbnailManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public ThumbnailManager(
/// All the alias are updated at once to avoid concurrency issues.Thumbnail
/// </remarks>
/// <param name="results">The list a queries that need to have an updated thumbnail.</param>
public void RefreshThumbnails(IEnumerable<QueryResult> results)
public async Task RefreshThumbnails(IEnumerable<QueryResult> results)
{
var queries = EntityDecorator<QueryResult>.FromEnumerable(results)
.ToArray();

using var m = TimePiece.Measure(this, m => _log.Info(m));
try
{
Parallel.ForEach(queries, _thumbnailRefresher.RefreshCurrentThumbnail);
await Task.Run(() => Parallel.ForEach(queries, _thumbnailRefresher.RefreshCurrentThumbnail));

var aliases = queries.Where(x => x.IsDirty)
.Select(x => x.Entity)
Expand Down
2 changes: 0 additions & 2 deletions src/Lanceur/Views/DoubloonsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public DoubloonsView()
var result = await Dialogs.YesNoQuestion($"Do you want to delete {interaction.Input} {(value > 1 ? "aliases" : "alias")}?");
interaction.SetOutput(result.ToBool());
});

ViewModel.Activate.Execute().Subscribe();
});
}

Expand Down
49 changes: 36 additions & 13 deletions src/Lanceur/Views/DoubloonsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;

namespace Lanceur.Views
{
public class DoubloonsViewModel : RoutableViewModel
public class DoubloonsViewModel : RoutableViewModel, IActivatableViewModel
{
#region Fields

Expand Down Expand Up @@ -44,29 +45,51 @@ public DoubloonsViewModel(
_notification = notification ?? l.GetService<INotification>();
_confirmRemove = Interactions.YesNoQuestion(schedulers.MainThreadScheduler);

Activate = ReactiveCommand.Create(OnActivate, outputScheduler: schedulers.MainThreadScheduler);
Activate.ThrownExceptions.Subscribe(ex => notify.Error(ex.Message, ex));

RemoveSelected = ReactiveCommand.CreateFromTask(OnRemoveSelected, outputScheduler: schedulers.MainThreadScheduler);
RemoveSelected.ThrownExceptions.Subscribe(ex => notify.Error(ex.Message, ex));

this.WhenAnyObservable(vm => vm.Activate)
.BindTo(this, vm => vm.Doubloons);
this.WhenActivated(d =>
{
Activate = ReactiveCommand.Create(OnActivate, outputScheduler: schedulers.TaskpoolScheduler);
Activate.ThrownExceptions.Subscribe(ex => notify.Error(ex.Message, ex));
Activate.DisposeWith(d);

RemoveSelected =
ReactiveCommand.CreateFromTask(OnRemoveSelected, outputScheduler: schedulers.MainThreadScheduler);
RemoveSelected.ThrownExceptions.Subscribe(ex => notify.Error(ex.Message, ex));
RemoveSelected.DisposeWith(d);

this.WhenAnyObservable(vm => vm.Activate)
.BindTo(this, vm => vm.Doubloons)
.DisposeWith(d);

this.WhenAnyValue(vm => vm.IsActivating)
.Where(x => x)
.Select(_ => Unit.Default)
.InvokeCommand(Activate)
.DisposeWith(d);

Activate.WhenAnyObservable(x => x.IsExecuting)
.Where(x => !x)
.BindTo(this, vm => vm.IsActivating)
.DisposeWith(d);

IsActivating = true;
// Activate.Execute().Subscribe().DisposeWith(d);
});
}

#endregion Constructors

#region Properties

public ReactiveCommand<Unit, ObservableCollection<SelectableAliasQueryResult>> Activate { get; }
[Reactive] private bool IsActivating { get; set; }
public ReactiveCommand<Unit, ObservableCollection<SelectableAliasQueryResult>> Activate { get; private set; }

public ViewModelActivator Activator { get; } = new();
public Interaction<string, bool> ConfirmRemove => _confirmRemove;
[Reactive] public ObservableCollection<SelectableAliasQueryResult> Doubloons { get; set; }
public ReactiveCommand<Unit, Unit> RemoveSelected { get; }
public ReactiveCommand<Unit, Unit> RemoveSelected { get; private set;}

#endregion Properties

#region Methods

private ObservableCollection<SelectableAliasQueryResult> OnActivate()
{
var doubloons = _service.GetDoubloons().ToArray();
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/Lanceur.Tests/ViewModels/KeywordViewModelShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public KeywordViewModelShould(ITestOutputHelper output)
});

[Fact]
public void RefreshThumbnailsWithoutRemovingAdditionalParameters()
public async Task RefreshThumbnailsWithoutRemovingAdditionalParameters()
{
// ARRANGE
var sql = new SqlBuilder().AppendAlias(1, "fileName1", "arguments1")
Expand Down Expand Up @@ -227,7 +227,7 @@ public void RefreshThumbnailsWithoutRemovingAdditionalParameters()
var aliases = dbRepository.Search("a");

// ACT
thumbnailManager.RefreshThumbnails(aliases);
await thumbnailManager.RefreshThumbnails(aliases);

// ASSERT
connectionMgr.WithinTransaction(tx => (long)tx.Connection.ExecuteScalar("select count(*) from alias_argument"))
Expand Down

0 comments on commit a2d4b34

Please sign in to comment.