1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-05 09:42:54 +08:00

Replace event subscription with callback in UserStatisticsWatcher

Also no longer cancels previous API requests as there's no actual need to do it.
This commit is contained in:
Salman Alshamrani 2024-11-24 04:10:01 -05:00
parent 0b52080a52
commit 631bfadd68
2 changed files with 13 additions and 27 deletions

View File

@ -36,7 +36,6 @@ namespace osu.Game.Online
private IAPIProvider api { get; set; } = null!; private IAPIProvider api { get; set; } = null!;
private readonly Dictionary<string, UserStatistics> statisticsCache = new Dictionary<string, UserStatistics>(); private readonly Dictionary<string, UserStatistics> statisticsCache = new Dictionary<string, UserStatistics>();
private readonly Dictionary<string, GetUserRequest> statisticsRequests = new Dictionary<string, GetUserRequest>();
/// <summary> /// <summary>
/// Returns the <see cref="UserStatistics"/> currently available for the given ruleset. /// Returns the <see cref="UserStatistics"/> currently available for the given ruleset.
@ -62,23 +61,21 @@ namespace osu.Game.Online
RefetchStatistics(ruleset); RefetchStatistics(ruleset);
} }
public void RefetchStatistics(RulesetInfo ruleset) public void RefetchStatistics(RulesetInfo ruleset, Action<UserStatisticsUpdate>? callback = null)
{ {
if (statisticsRequests.TryGetValue(ruleset.ShortName, out var previousRequest)) var request = new GetUserRequest(api.LocalUser.Value.Id, ruleset);
previousRequest.Cancel(); request.Success += u => UpdateStatistics(u.Statistics, ruleset, callback);
var request = statisticsRequests[ruleset.ShortName] = new GetUserRequest(api.LocalUser.Value.Id, ruleset);
request.Success += u => UpdateStatistics(u.Statistics, ruleset);
api.Queue(request); api.Queue(request);
} }
protected void UpdateStatistics(UserStatistics newStatistics, RulesetInfo ruleset) protected void UpdateStatistics(UserStatistics newStatistics, RulesetInfo ruleset, Action<UserStatisticsUpdate>? callback = null)
{ {
var oldStatistics = statisticsCache.GetValueOrDefault(ruleset.ShortName); var oldStatistics = statisticsCache.GetValueOrDefault(ruleset.ShortName);
statisticsRequests.Remove(ruleset.ShortName);
statisticsCache[ruleset.ShortName] = newStatistics; statisticsCache[ruleset.ShortName] = newStatistics;
StatisticsUpdated?.Invoke(new UserStatisticsUpdate(ruleset, oldStatistics, newStatistics));
var update = new UserStatisticsUpdate(ruleset, oldStatistics, newStatistics);
callback?.Invoke(update);
StatisticsUpdated?.Invoke(update);
} }
} }

View File

@ -23,8 +23,6 @@ namespace osu.Game.Online
public IBindable<ScoreBasedUserStatisticsUpdate?> LatestUpdate => latestUpdate; public IBindable<ScoreBasedUserStatisticsUpdate?> LatestUpdate => latestUpdate;
private readonly Bindable<ScoreBasedUserStatisticsUpdate?> latestUpdate = new Bindable<ScoreBasedUserStatisticsUpdate?>(); private readonly Bindable<ScoreBasedUserStatisticsUpdate?> latestUpdate = new Bindable<ScoreBasedUserStatisticsUpdate?>();
private ScoreInfo? scorePendingUpdate;
[Resolved] [Resolved]
private SpectatorClient spectatorClient { get; set; } = null!; private SpectatorClient spectatorClient { get; set; } = null!;
@ -43,7 +41,6 @@ namespace osu.Game.Online
base.LoadComplete(); base.LoadComplete();
spectatorClient.OnUserScoreProcessed += userScoreProcessed; spectatorClient.OnUserScoreProcessed += userScoreProcessed;
statisticsProvider.StatisticsUpdated += onStatisticsUpdated;
} }
/// <summary> /// <summary>
@ -72,21 +69,13 @@ namespace osu.Game.Online
if (!watchedScores.Remove(scoreId, out var scoreInfo)) if (!watchedScores.Remove(scoreId, out var scoreInfo))
return; return;
scorePendingUpdate = scoreInfo; statisticsProvider.RefetchStatistics(scoreInfo.Ruleset, u => Schedule(() =>
statisticsProvider.RefetchStatistics(scoreInfo.Ruleset); {
if (u.OldStatistics != null)
latestUpdate.Value = new ScoreBasedUserStatisticsUpdate(scoreInfo, u.OldStatistics, u.NewStatistics);
}));
} }
private void onStatisticsUpdated(UserStatisticsUpdate update) => Schedule(() =>
{
if (scorePendingUpdate == null || !update.Ruleset.Equals(scorePendingUpdate.Ruleset))
return;
if (update.OldStatistics != null)
latestUpdate.Value = new ScoreBasedUserStatisticsUpdate(scorePendingUpdate, update.OldStatistics, update.NewStatistics);
scorePendingUpdate = null;
});
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
if (spectatorClient.IsNotNull()) if (spectatorClient.IsNotNull())