mirror of
https://github.com/osukey/osukey.git
synced 2025-07-03 01:09:57 +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();
|
||||
|
Reference in New Issue
Block a user