Selector/Selector.MAUI/Services/HubManager.cs

59 lines
1.6 KiB
C#
Raw Normal View History

using System;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Logging;
using Selector.SignalR;
namespace Selector.MAUI.Services;
public class HubManager
{
private readonly NowHubClient nowClient;
private readonly NowHubCache nowCache;
private readonly PastHubClient pastClient;
private readonly ILogger<HubManager> logger;
public HubManager(NowHubClient nowClient, NowHubCache nowCache, PastHubClient pastClient, ILogger<HubManager> logger)
{
this.nowClient = nowClient;
this.nowCache = nowCache;
this.pastClient = pastClient;
this.logger = logger;
}
public async Task EnsureConnected()
{
2023-01-29 21:26:39 +00:00
var nowTask = Task.CompletedTask;
var pastTask = Task.CompletedTask;
if (nowClient.State == HubConnectionState.Disconnected)
{
logger.LogInformation("Starting now hub connection");
2023-01-29 21:26:39 +00:00
nowTask = nowClient.StartAsync().ContinueWith(async x =>
{
if (x.IsCompletedSuccessfully)
{
nowCache.BindClient();
await nowClient.OnConnected();
}
});
}
if (pastClient.State == HubConnectionState.Disconnected)
{
logger.LogInformation("Starting past hub connection");
2023-01-29 21:26:39 +00:00
pastTask = pastClient.StartAsync().ContinueWith(async x =>
{
if (x.IsCompletedSuccessfully)
{
await pastClient.OnConnected();
}
});
}
2023-01-29 21:26:39 +00:00
await Task.WhenAll(nowTask, pastTask);
}
}