From 848b81e952934ee90f3cc4e86428bb385c0fdde6 Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Tue, 9 Feb 2021 07:53:22 +0300 Subject: [PATCH] Remove necessity of making hub client connector a component --- osu.Game/Online/HubClientConnector.cs | 51 ++++++++++++++------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/osu.Game/Online/HubClientConnector.cs b/osu.Game/Online/HubClientConnector.cs index 49b1ab639a..b740aabb92 100644 --- a/osu.Game/Online/HubClientConnector.cs +++ b/osu.Game/Online/HubClientConnector.cs @@ -4,15 +4,14 @@ #nullable enable using System; +using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR.Client; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using osu.Framework; -using osu.Framework.Allocation; using osu.Framework.Bindables; -using osu.Framework.Graphics; using osu.Framework.Logging; using osu.Game.Online.API; @@ -21,7 +20,7 @@ namespace osu.Game.Online /// /// A component that maintains over a hub connection between client and server. /// - public class HubClientConnector : Component + public class HubClientConnector : IDisposable { /// /// Invoked whenever a new hub connection is built. @@ -30,6 +29,7 @@ public class HubClientConnector : Component private readonly string clientName; private readonly string endpoint; + private readonly IAPIProvider? api; /// /// The current connection opened by this connector. @@ -45,9 +45,6 @@ public class HubClientConnector : Component private readonly SemaphoreSlim connectionLock = new SemaphoreSlim(1); private CancellationTokenSource connectCancelSource = new CancellationTokenSource(); - [Resolved] - private IAPIProvider api { get; set; } = null!; - private readonly IBindable apiState = new Bindable(); /// @@ -55,30 +52,32 @@ public class HubClientConnector : Component /// /// The name of the client this connector connects for, used for logging. /// The endpoint to the hub. - public HubClientConnector(string clientName, string endpoint) + /// The API provider for listening to state changes, or null to not listen. + public HubClientConnector(string clientName, string endpoint, IAPIProvider? api) { this.clientName = clientName; this.endpoint = endpoint; - } - [BackgroundDependencyLoader] - private void load() - { - apiState.BindTo(api.State); - apiState.BindValueChanged(state => + this.api = api; + + if (api != null) { - switch (state.NewValue) + apiState.BindTo(api.State); + apiState.BindValueChanged(state => { - case APIState.Failing: - case APIState.Offline: - Task.Run(() => disconnect(true)); - break; + switch (state.NewValue) + { + case APIState.Failing: + case APIState.Offline: + Task.Run(() => disconnect(true)); + break; - case APIState.Online: - Task.Run(connect); - break; - } - }); + case APIState.Online: + Task.Run(connect); + break; + } + }, true); + } } private async Task connect() @@ -137,6 +136,8 @@ private async Task connect() private HubConnection createConnection(CancellationToken cancellationToken) { + Debug.Assert(api != null); + var builder = new HubConnectionBuilder() .WithUrl(endpoint, options => { options.Headers.Add("Authorization", $"Bearer {api.AccessToken}"); }); @@ -200,9 +201,9 @@ private void cancelExistingConnect() public override string ToString() => $"Connector for {clientName} ({(IsConnected.Value ? "connected" : "not connected")}"; - protected override void Dispose(bool isDisposing) + public void Dispose() { - base.Dispose(isDisposing); + apiState.UnbindAll(); cancelExistingConnect(); } }