Skip to content

Commit

Permalink
Prevent concurrent loading of catalogs (#2316)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmelBawa-msft authored Feb 27, 2024
1 parent 215deda commit 45b471c
Showing 1 changed file with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
Expand All @@ -17,16 +18,18 @@

namespace DevHome.SetupFlow.ViewModels;

public partial class PackageCatalogListViewModel : ObservableObject
public partial class PackageCatalogListViewModel : ObservableObject, IDisposable
{
private readonly ICatalogDataSourceLoader _catalogDataSourceLoader;
private readonly IExtensionService _extensionService;
private readonly PackageCatalogViewModelFactory _packageCatalogViewModelFactory;
private readonly DispatcherQueue _dispatcher;
private readonly SemaphoreSlim _loadCatalogsSemaphore = new(1, 1);

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(CatalogFullPath))]
private PackageCatalogViewModel _viewAllCatalog;
private bool disposedValue;

public List<string> CatalogFullPath => new()
{
Expand Down Expand Up @@ -61,10 +64,12 @@ public PackageCatalogListViewModel(
/// </summary>
private async Task LoadCatalogsAsync()
{
PackageCatalogs.Clear();
AddShimmers(_catalogDataSourceLoader.CatalogCount);
// Prevent concurrent loading of catalogs
await _loadCatalogsSemaphore.WaitAsync();
try
{
PackageCatalogs.Clear();
AddShimmers(_catalogDataSourceLoader.CatalogCount);
await foreach (var dataSourceCatalogs in _catalogDataSourceLoader.LoadCatalogsAsync())
{
foreach (var catalog in dataSourceCatalogs)
Expand All @@ -75,16 +80,20 @@ private async Task LoadCatalogsAsync()

RemoveShimmers(dataSourceCatalogs.Count);
}

// Remove any remaining shimmers:
// This can happen if for example a catalog was detected but not
// displayed (e.g. catalog with no packages to display)
RemoveShimmers(PackageCatalogShimmers.Count);
}
catch (Exception e)
{
Log.Logger?.ReportError(Log.Component.AppManagement, $"Failed to load catalogs.", e);
}

// Remove any remaining shimmers:
// This can happen if for example a catalog was detected but not
// displayed (e.g. catalog with no packages to display)
RemoveShimmers(PackageCatalogShimmers.Count);
finally
{
_loadCatalogsSemaphore.Release();
}
}

/// <summary>
Expand Down Expand Up @@ -146,4 +155,23 @@ private async void OnExtensionChangedAsync(object sender, EventArgs e)
{
await _dispatcher.EnqueueAsync(() => LoadCatalogsAsync());
}

private void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_loadCatalogsSemaphore.Dispose();
}

disposedValue = true;
}
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}

0 comments on commit 45b471c

Please sign in to comment.