mirror of
https://github.com/osukey/osukey.git
synced 2025-08-04 15:16:38 +09:00
Remove IOnlineComponent and change existing components to use bindable flow
This commit is contained in:
@ -78,26 +78,8 @@ namespace osu.Game.Online.API
|
||||
|
||||
private void onTokenChanged(ValueChangedEvent<OAuthToken> e) => config.Set(OsuSetting.Token, config.Get<bool>(OsuSetting.SavePassword) ? authentication.TokenString : string.Empty);
|
||||
|
||||
private readonly List<IOnlineComponent> components = new List<IOnlineComponent>();
|
||||
|
||||
internal new void Schedule(Action action) => base.Schedule(action);
|
||||
|
||||
/// <summary>
|
||||
/// Register a component to receive API events.
|
||||
/// Fires <see cref="IOnlineComponent.APIStateChanged"/> once immediately to ensure a correct state.
|
||||
/// </summary>
|
||||
/// <param name="component"></param>
|
||||
public void Register(IOnlineComponent component)
|
||||
{
|
||||
Schedule(() => components.Add(component));
|
||||
component.APIStateChanged(this, state);
|
||||
}
|
||||
|
||||
public void Unregister(IOnlineComponent component)
|
||||
{
|
||||
Schedule(() => components.Remove(component));
|
||||
}
|
||||
|
||||
public string AccessToken => authentication.RequestAccessToken();
|
||||
|
||||
/// <summary>
|
||||
@ -109,7 +91,7 @@ namespace osu.Game.Online.API
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
switch (State)
|
||||
switch (State.Value)
|
||||
{
|
||||
case APIState.Failing:
|
||||
//todo: replace this with a ping request.
|
||||
@ -131,12 +113,12 @@ namespace osu.Game.Online.API
|
||||
// work to restore a connection...
|
||||
if (!HasLogin)
|
||||
{
|
||||
State = APIState.Offline;
|
||||
state.Value = APIState.Offline;
|
||||
Thread.Sleep(50);
|
||||
continue;
|
||||
}
|
||||
|
||||
State = APIState.Connecting;
|
||||
state.Value = APIState.Connecting;
|
||||
|
||||
// save the username at this point, if the user requested for it to be.
|
||||
config.Set(OsuSetting.Username, config.Get<bool>(OsuSetting.SaveUsername) ? ProvidedUsername : string.Empty);
|
||||
@ -162,20 +144,20 @@ namespace osu.Game.Online.API
|
||||
failureCount = 0;
|
||||
|
||||
//we're connected!
|
||||
State = APIState.Online;
|
||||
state.Value = APIState.Online;
|
||||
};
|
||||
|
||||
if (!handleRequest(userReq))
|
||||
{
|
||||
if (State == APIState.Connecting)
|
||||
State = APIState.Failing;
|
||||
if (State.Value == APIState.Connecting)
|
||||
state.Value = APIState.Failing;
|
||||
continue;
|
||||
}
|
||||
|
||||
// The Success callback event is fired on the main thread, so we should wait for that to run before proceeding.
|
||||
// Without this, we will end up circulating this Connecting loop multiple times and queueing up many web requests
|
||||
// before actually going online.
|
||||
while (State > APIState.Offline && State < APIState.Online)
|
||||
while (State.Value > APIState.Offline && State.Value < APIState.Online)
|
||||
Thread.Sleep(500);
|
||||
|
||||
break;
|
||||
@ -224,7 +206,7 @@ namespace osu.Game.Online.API
|
||||
|
||||
public void Login(string username, string password)
|
||||
{
|
||||
Debug.Assert(State == APIState.Offline);
|
||||
Debug.Assert(State.Value == APIState.Offline);
|
||||
|
||||
ProvidedUsername = username;
|
||||
this.password = password;
|
||||
@ -232,7 +214,7 @@ namespace osu.Game.Online.API
|
||||
|
||||
public RegistrationRequest.RegistrationRequestErrors CreateAccount(string email, string username, string password)
|
||||
{
|
||||
Debug.Assert(State == APIState.Offline);
|
||||
Debug.Assert(State.Value == APIState.Offline);
|
||||
|
||||
var req = new RegistrationRequest
|
||||
{
|
||||
@ -276,7 +258,7 @@ namespace osu.Game.Online.API
|
||||
req.Perform(this);
|
||||
|
||||
// we could still be in initialisation, at which point we don't want to say we're Online yet.
|
||||
if (IsLoggedIn) State = APIState.Online;
|
||||
if (IsLoggedIn) state.Value = APIState.Online;
|
||||
|
||||
failureCount = 0;
|
||||
return true;
|
||||
@ -293,27 +275,12 @@ namespace osu.Game.Online.API
|
||||
}
|
||||
}
|
||||
|
||||
private APIState state;
|
||||
private readonly Bindable<APIState> state = new Bindable<APIState>();
|
||||
|
||||
public APIState State
|
||||
{
|
||||
get => state;
|
||||
private set
|
||||
{
|
||||
if (state == value)
|
||||
return;
|
||||
|
||||
APIState oldState = state;
|
||||
state = value;
|
||||
|
||||
log.Add($@"We just went {state}!");
|
||||
Schedule(() =>
|
||||
{
|
||||
components.ForEach(c => c.APIStateChanged(this, state));
|
||||
OnStateChange?.Invoke(oldState, state);
|
||||
});
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// The current connectivity state of the API.
|
||||
/// </summary>
|
||||
public IBindable<APIState> State => state;
|
||||
|
||||
private bool handleWebException(WebException we)
|
||||
{
|
||||
@ -343,9 +310,9 @@ namespace osu.Game.Online.API
|
||||
// we might try again at an api level.
|
||||
return false;
|
||||
|
||||
if (State == APIState.Online)
|
||||
if (State.Value == APIState.Online)
|
||||
{
|
||||
State = APIState.Failing;
|
||||
state.Value = APIState.Failing;
|
||||
flushQueue();
|
||||
}
|
||||
|
||||
@ -362,10 +329,6 @@ namespace osu.Game.Online.API
|
||||
lock (queue) queue.Enqueue(request);
|
||||
}
|
||||
|
||||
public event StateChangeDelegate OnStateChange;
|
||||
|
||||
public delegate void StateChangeDelegate(APIState oldState, APIState newState);
|
||||
|
||||
private void flushQueue(bool failOldRequests = true)
|
||||
{
|
||||
lock (queue)
|
||||
@ -392,7 +355,7 @@ namespace osu.Game.Online.API
|
||||
// Scheduled prior to state change such that the state changed event is invoked with the correct user present
|
||||
Schedule(() => LocalUser.Value = createGuestUser());
|
||||
|
||||
State = APIState.Offline;
|
||||
state.Value = APIState.Offline;
|
||||
}
|
||||
|
||||
private static User createGuestUser() => new GuestUser();
|
||||
|
@ -21,34 +21,23 @@ namespace osu.Game.Online.API
|
||||
|
||||
public Bindable<UserActivity> Activity { get; } = new Bindable<UserActivity>();
|
||||
|
||||
public bool IsLoggedIn => State == APIState.Online;
|
||||
public bool IsLoggedIn => State.Value == APIState.Online;
|
||||
|
||||
public string ProvidedUsername => LocalUser.Value.Username;
|
||||
|
||||
public string Endpoint => "http://localhost";
|
||||
|
||||
private APIState state = APIState.Online;
|
||||
|
||||
private readonly List<IOnlineComponent> components = new List<IOnlineComponent>();
|
||||
|
||||
/// <summary>
|
||||
/// Provide handling logic for an arbitrary API request.
|
||||
/// </summary>
|
||||
public Action<APIRequest> HandleRequest;
|
||||
|
||||
public APIState State
|
||||
{
|
||||
get => state;
|
||||
set
|
||||
{
|
||||
if (state == value)
|
||||
return;
|
||||
private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online);
|
||||
|
||||
state = value;
|
||||
|
||||
Scheduler.Add(() => components.ForEach(c => c.APIStateChanged(this, value)));
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// The current connectivity state of the API.
|
||||
/// </summary>
|
||||
public IBindable<APIState> State => state;
|
||||
|
||||
public DummyAPIAccess()
|
||||
{
|
||||
@ -72,17 +61,6 @@ namespace osu.Game.Online.API
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Register(IOnlineComponent component)
|
||||
{
|
||||
Scheduler.Add(delegate { components.Add(component); });
|
||||
component.APIStateChanged(this, state);
|
||||
}
|
||||
|
||||
public void Unregister(IOnlineComponent component)
|
||||
{
|
||||
Scheduler.Add(delegate { components.Remove(component); });
|
||||
}
|
||||
|
||||
public void Login(string username, string password)
|
||||
{
|
||||
LocalUser.Value = new User
|
||||
@ -91,13 +69,13 @@ namespace osu.Game.Online.API
|
||||
Id = 1001,
|
||||
};
|
||||
|
||||
State = APIState.Online;
|
||||
state.Value = APIState.Online;
|
||||
}
|
||||
|
||||
public void Logout()
|
||||
{
|
||||
LocalUser.Value = new GuestUser();
|
||||
State = APIState.Offline;
|
||||
state.Value = APIState.Offline;
|
||||
}
|
||||
|
||||
public RegistrationRequest.RegistrationRequestErrors CreateAccount(string email, string username, string password)
|
||||
@ -105,5 +83,7 @@ namespace osu.Game.Online.API
|
||||
Thread.Sleep(200);
|
||||
return null;
|
||||
}
|
||||
|
||||
public void SetState(APIState newState) => state.Value = newState;
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ namespace osu.Game.Online.API
|
||||
/// </summary>
|
||||
string Endpoint { get; }
|
||||
|
||||
APIState State { get; }
|
||||
IBindable<APIState> State { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Queue a new request.
|
||||
@ -61,18 +61,6 @@ namespace osu.Game.Online.API
|
||||
/// <param name="request">The request to perform.</param>
|
||||
Task PerformAsync(APIRequest request);
|
||||
|
||||
/// <summary>
|
||||
/// Register a component to receive state changes.
|
||||
/// </summary>
|
||||
/// <param name="component">The component to register.</param>
|
||||
void Register(IOnlineComponent component);
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters a component to receive state changes.
|
||||
/// </summary>
|
||||
/// <param name="component">The component to unregister.</param>
|
||||
void Unregister(IOnlineComponent component);
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to login using the provided credentials. This is a non-blocking operation.
|
||||
/// </summary>
|
||||
|
@ -1,10 +0,0 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
namespace osu.Game.Online.API
|
||||
{
|
||||
public interface IOnlineComponent
|
||||
{
|
||||
void APIStateChanged(IAPIProvider api, APIState state);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user