Fix possible threading issues

Not really sure of the best way to handle this in general. It could be
argued that this should be a `Component` type and the bindable bound in
`LoadComplete()`...
This commit is contained in:
Dan Balasescu 2022-11-02 10:04:24 +09:00
parent 5b25ef5f2f
commit 8ac2075c61
4 changed files with 10 additions and 13 deletions

View File

@ -21,14 +21,14 @@ namespace osu.Game.Online.Notifications
public Action<List<Message>>? NewMessages;
public Action? PresenceReceived;
private readonly IAPIProvider api;
protected readonly IAPIProvider API;
private bool enableChat;
private long lastMessageId;
protected NotificationsClient(IAPIProvider api)
{
this.api = api;
API = api;
}
public bool EnableChat
@ -54,7 +54,7 @@ namespace osu.Game.Online.Notifications
protected virtual Task StartChatAsync()
{
api.Queue(CreateFetchMessagesRequest(0));
API.Queue(CreateFetchMessagesRequest(0));
return Task.CompletedTask;
}

View File

@ -12,12 +12,9 @@ namespace osu.Game.Online.Notifications.Polling
/// </summary>
public class PollingNotificationsClient : NotificationsClient
{
private readonly IAPIProvider api;
public PollingNotificationsClient(IAPIProvider api)
: base(api)
{
this.api = api;
}
public override Task ConnectAsync(CancellationToken cancellationToken)
@ -26,7 +23,7 @@ namespace osu.Game.Online.Notifications.Polling
{
while (!cancellationToken.IsCancellationRequested)
{
await api.PerformAsync(CreateFetchMessagesRequest());
await API.PerformAsync(CreateFetchMessagesRequest());
await Task.Delay(1000, cancellationToken);
}
}, cancellationToken);

View File

@ -12,15 +12,12 @@ namespace osu.Game.Online.Notifications.Polling
/// </summary>
public class PollingNotificationsClientConnector : NotificationsClientConnector
{
private readonly IAPIProvider api;
public PollingNotificationsClientConnector(IAPIProvider api)
: base(api)
{
this.api = api;
}
protected override Task<NotificationsClient> BuildNotificationClientAsync(CancellationToken cancellationToken)
=> Task.FromResult((NotificationsClient)new PollingNotificationsClient(api));
=> Task.FromResult((NotificationsClient)new PollingNotificationsClient(API));
}
}

View File

@ -23,18 +23,21 @@ namespace osu.Game.Online
/// </summary>
public SocketClient? CurrentConnection { get; private set; }
protected readonly IAPIProvider API;
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
private readonly Bindable<bool> isConnected = new Bindable<bool>();
private readonly SemaphoreSlim connectionLock = new SemaphoreSlim(1);
private CancellationTokenSource connectCancelSource = new CancellationTokenSource();
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
/// <summary>
/// Constructs a new <see cref="HubClientConnector"/>.
/// </summary>
/// <param name="api"> An API provider used to react to connection state changes.</param>
protected SocketClientConnector(IAPIProvider api)
{
API = api;
apiState.BindTo(api.State);
apiState.BindValueChanged(_ => Task.Run(connectIfPossible), true);
}