Skip to content

Commit

Permalink
#214 - Rewrite after upgrade code to await on currency creation event.
Browse files Browse the repository at this point in the history
  • Loading branch information
maraf committed Jan 30, 2019
1 parent 04cce2b commit dadc5d8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
25 changes: 19 additions & 6 deletions src/Money.UI.Universal/Bootstrap/BootstrapTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Money.Services;
using Money.Events;
using Money.Services;
using Money.Services.Globalization;
using Money.Services.Settings;
using Money.Services.Tiles;
Expand All @@ -10,6 +11,7 @@
using Neptuo.Converters;
using Neptuo.Data;
using Neptuo.Events;
using Neptuo.Events.Handlers;
using Neptuo.Exceptions.Handlers;
using Neptuo.Formatters;
using Neptuo.Formatters.Converters;
Expand Down Expand Up @@ -94,15 +96,26 @@ public void Initialize()
readModels.Database.EnsureCreated();

if (ServiceProvider.UpgradeService.IsRequired())
ServiceProvider.UpgradeService.Completed += () => InitializeCache(priceCalculator, currencyCache);
{
ServiceProvider.UpgradeService.Completed += async () =>
{
Task delay = Task.Delay(5000);
Task currencyCreated = eventDispatcher.Handlers.Await<CurrencyCreated>();

await Task.WhenAny(delay, currencyCreated);
await InitializeCacheAsync(priceCalculator, currencyCache);
};
}
else
InitializeCache(priceCalculator, currencyCache);
{
_ = InitializeCacheAsync(priceCalculator, currencyCache);
}
}

private void InitializeCache(PriceCalculator priceCalculator, CurrencyCache currencyCache)
private async Task InitializeCacheAsync(PriceCalculator priceCalculator, CurrencyCache currencyCache)
{
currencyCache.InitializeAsync(ServiceProvider.QueryDispatcher);
priceCalculator.InitializeAsync(ServiceProvider.QueryDispatcher);
await currencyCache.InitializeAsync(ServiceProvider.QueryDispatcher);
await priceCalculator.InitializeAsync(ServiceProvider.QueryDispatcher);
}

private void Domain()
Expand Down
11 changes: 8 additions & 3 deletions src/Money.UI.Universal/Bootstrap/UpgradeServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ internal abstract class UpgradeServiceBase : IUpgradeService
{
private readonly IFactory<ApplicationDataContainer> storageContainerFactory;
private readonly int currentVersion;
private readonly List<Func<Task>> completed = new List<Func<Task>>();

public event Action Completed;
public event Func<Task> Completed
{
add => completed.Add(value);
remove => completed.Remove(value);
}

public UpgradeServiceBase(IFactory<ApplicationDataContainer> storageContainerFactory, int currentVersion)
{
Expand Down Expand Up @@ -53,8 +58,8 @@ public async Task UpgradeAsync(IUpgradeContext context)

SetCurrentVersion(this.currentVersion);

Completed?.Invoke();
Completed = null;
foreach (var item in completed)
await item();

await DelayAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Money.Views.DesignData
{
internal class MockUpgradeService : IUpgradeService
{
public event Action Completed;
public event Func<Task> Completed;

public bool IsRequired()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Money.Views.DesignData
{
internal class TestUpgradeService : IUpgradeService
{
public event Action Completed;
public event Func<Task> Completed;

public bool IsRequired()
{
Expand Down
12 changes: 0 additions & 12 deletions src/Neptuo/Migrations/IDowngradeService.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Neptuo/Migrations/IUpgradeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IUpgradeService
/// <summary>
/// An event raised when upgrade is completed.
/// </summary>
event Action Completed;
event Func<Task> Completed;

/// <summary>
/// Returns a <c>true</c> if upgrade is required.
Expand Down

0 comments on commit dadc5d8

Please sign in to comment.