refactor : refactored Repeater for easier implementation in other classes.

This commit is contained in:
Laura Gomez
2020-06-15 17:56:24 -04:00
committed by Chris Cameron
parent c6b2cd78cc
commit 878784586f

View File

@@ -1,6 +1,5 @@
using System; using System;
using ICD.Common.Properties; using ICD.Common.Properties;
using ICD.Common.Utils.Extensions;
namespace ICD.Common.Utils.Timers namespace ICD.Common.Utils.Timers
{ {
@@ -11,36 +10,20 @@ namespace ICD.Common.Utils.Timers
[PublicAPI] [PublicAPI]
public sealed class Repeater : IDisposable public sealed class Repeater : IDisposable
{ {
/// <summary> public delegate void RepeatCallback(bool isInitial);
/// Raised on the initial repeat.
/// </summary>
[PublicAPI]
public event EventHandler OnInitialRepeat;
/// <summary>
/// Raised on each subsequent repeat.
/// </summary>
[PublicAPI]
public event EventHandler OnRepeat;
private readonly SafeTimer m_RepeatTimer; private readonly SafeTimer m_RepeatTimer;
private readonly long m_BeforeRepeat; private RepeatCallback m_RepeatCallback;
private readonly long m_BetweenRepeat;
#region Constructor #region Constructor
/// <summary> /// <summary>
/// Constructor. /// Constructor.
/// </summary> /// </summary>
/// <param name="beforeRepeat">The delay before the second increment</param> public Repeater()
/// <param name="betweenRepeat">The delay between each subsequent repeat</param>
public Repeater(long beforeRepeat, long betweenRepeat)
{ {
m_RepeatTimer = SafeTimer.Stopped(RepeatCallback); m_RepeatTimer = SafeTimer.Stopped(TimerElapsed);
m_BeforeRepeat = beforeRepeat;
m_BetweenRepeat = betweenRepeat;
} }
/// <summary> /// <summary>
@@ -66,12 +49,21 @@ namespace ICD.Common.Utils.Timers
/// <summary> /// <summary>
/// Begin repeating. /// Begin repeating.
/// </summary> /// </summary>
/// <param name="repeatCallback"></param>
/// <param name="beforeRepeat">The delay before the second increment</param>
/// <param name="betweenRepeat">The delay between each subsequent repeat</param>
[PublicAPI] [PublicAPI]
public void Start() public void Start([NotNull] RepeatCallback repeatCallback, long beforeRepeat, long betweenRepeat)
{ {
OnInitialRepeat.Raise(this); if (repeatCallback == null)
throw new ArgumentNullException("repeatCallback");
m_RepeatTimer.Reset(m_BeforeRepeat, m_BetweenRepeat); Stop();
m_RepeatCallback = repeatCallback;
m_RepeatCallback(true);
m_RepeatTimer.Reset(beforeRepeat, betweenRepeat);
} }
/// <summary> /// <summary>
@@ -81,6 +73,7 @@ namespace ICD.Common.Utils.Timers
public void Stop() public void Stop()
{ {
m_RepeatTimer.Stop(); m_RepeatTimer.Stop();
m_RepeatCallback = null;
} }
#endregion #endregion
@@ -90,9 +83,9 @@ namespace ICD.Common.Utils.Timers
/// <summary> /// <summary>
/// Called for every repeat. /// Called for every repeat.
/// </summary> /// </summary>
private void RepeatCallback() private void TimerElapsed()
{ {
OnRepeat.Raise(this); m_RepeatCallback(false);
} }
#endregion #endregion