Reorganise methods in `PollingComponent`

This commit is contained in:
Dean Herbert 2021-08-17 17:04:32 +09:00
parent a901fca54a
commit 8a1651e830
1 changed files with 36 additions and 37 deletions

View File

@ -47,39 +47,13 @@ protected override void LoadComplete()
pollIfNecessary();
}
private bool pollIfNecessary()
/// <summary>
/// Immediately performs a <see cref="Poll"/>.
/// </summary>
public void PollImmediately()
{
// we must be loaded so we have access to clock.
if (!IsLoaded) return false;
// there's already a poll process running.
if (pollingActive) return false;
// don't try polling if the time between polls hasn't been set.
if (TimeBetweenPolls.Value == 0) return false;
if (!lastTimePolled.HasValue)
{
doPoll();
return true;
}
if (Time.Current - lastTimePolled.Value > TimeBetweenPolls.Value)
{
doPoll();
return true;
}
// not enough time has passed since the last poll. we do want to schedule a poll to happen, though.
lastTimePolled = Time.Current - TimeBetweenPolls.Value;
scheduleNextPoll();
return false;
}
private void doPoll()
{
scheduledPoll = null;
pollingActive = true;
Poll().ContinueWith(_ => pollComplete());
}
/// <summary>
@ -90,13 +64,11 @@ protected virtual Task Poll()
return Task.CompletedTask;
}
/// <summary>
/// Immediately performs a <see cref="Poll"/>.
/// </summary>
public void PollImmediately()
private void doPoll()
{
lastTimePolled = Time.Current - TimeBetweenPolls.Value;
scheduleNextPoll();
scheduledPoll = null;
pollingActive = true;
Poll().ContinueWith(_ => pollComplete());
}
/// <summary>
@ -111,6 +83,33 @@ private void pollComplete()
pollIfNecessary();
}
private void pollIfNecessary()
{
// we must be loaded so we have access to clock.
if (!IsLoaded) return;
// there's already a poll process running.
if (pollingActive) return;
// don't try polling if the time between polls hasn't been set.
if (TimeBetweenPolls.Value == 0) return;
if (!lastTimePolled.HasValue)
{
doPoll();
return;
}
if (Time.Current - lastTimePolled.Value > TimeBetweenPolls.Value)
{
doPoll();
return;
}
// not enough time has passed since the last poll. we do want to schedule a poll to happen, though.
scheduleNextPoll();
}
private void scheduleNextPoll()
{
scheduledPoll?.Cancel();