Make TimeBetweenPolls into a bindable

This commit is contained in:
smoogipoo
2020-12-20 18:34:54 +09:00
parent d74485704a
commit 45107280a0
4 changed files with 19 additions and 34 deletions

View File

@ -57,7 +57,7 @@ namespace osu.Game.Online.Chat
{
CurrentChannel.ValueChanged += currentChannelChanged;
HighPollRate.BindValueChanged(enabled => TimeBetweenPolls = enabled.NewValue ? 1000 : 6000, true);
HighPollRate.BindValueChanged(enabled => TimeBetweenPolls.Value = enabled.NewValue ? 1000 : 6000, true);
}
/// <summary>

View File

@ -3,6 +3,7 @@
using System;
using System.Threading.Tasks;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Framework.Threading;
@ -19,22 +20,11 @@ namespace osu.Game.Online
private bool pollingActive;
private double timeBetweenPolls;
/// <summary>
/// The time in milliseconds to wait between polls.
/// Setting to zero stops all polling.
/// </summary>
public double TimeBetweenPolls
{
get => timeBetweenPolls;
set
{
timeBetweenPolls = value;
scheduledPoll?.Cancel();
pollIfNecessary();
}
}
public readonly Bindable<double> TimeBetweenPolls = new Bindable<double>();
/// <summary>
///
@ -42,7 +32,13 @@ namespace osu.Game.Online
/// <param name="timeBetweenPolls">The initial time in milliseconds to wait between polls. Setting to zero stops all polling.</param>
protected PollingComponent(double timeBetweenPolls = 0)
{
TimeBetweenPolls = timeBetweenPolls;
TimeBetweenPolls.BindValueChanged(_ =>
{
scheduledPoll?.Cancel();
pollIfNecessary();
});
TimeBetweenPolls.Value = timeBetweenPolls;
}
protected override void LoadComplete()
@ -60,7 +56,7 @@ namespace osu.Game.Online
if (pollingActive) return false;
// don't try polling if the time between polls hasn't been set.
if (timeBetweenPolls == 0) return false;
if (TimeBetweenPolls.Value == 0) return false;
if (!lastTimePolled.HasValue)
{
@ -68,7 +64,7 @@ namespace osu.Game.Online
return true;
}
if (Time.Current - lastTimePolled.Value > timeBetweenPolls)
if (Time.Current - lastTimePolled.Value > TimeBetweenPolls.Value)
{
doPoll();
return true;
@ -99,7 +95,7 @@ namespace osu.Game.Online
/// </summary>
public void PollImmediately()
{
lastTimePolled = Time.Current - timeBetweenPolls;
lastTimePolled = Time.Current - TimeBetweenPolls.Value;
scheduleNextPoll();
}
@ -121,7 +117,7 @@ namespace osu.Game.Online
double lastPollDuration = lastTimePolled.HasValue ? Time.Current - lastTimePolled.Value : 0;
scheduledPoll = Scheduler.AddDelayed(doPoll, Math.Max(0, timeBetweenPolls - lastPollDuration));
scheduledPoll = Scheduler.AddDelayed(doPoll, Math.Max(0, TimeBetweenPolls.Value - lastPollDuration));
}
}
}