using System;
using ICD.Common.Properties;
using ICD.Common.Utils.Extensions;
namespace ICD.Common.Utils.Timers
{
///
/// Simple class for implementing things like volume ramps, where a button has an
/// immediate effect, and then begins ramping after a brief delay.
///
[PublicAPI]
public sealed class Repeater : IDisposable
{
///
/// Raised on the initial repeat.
///
[PublicAPI]
public event EventHandler OnInitialRepeat;
///
/// Raised on each subsequent repeat.
///
[PublicAPI]
public event EventHandler OnRepeat;
private readonly SafeTimer m_RepeatTimer;
private readonly long m_BeforeRepeat;
private readonly long m_BetweenRepeat;
#region Constructor
///
/// Constructor.
///
/// The delay before the second increment
/// The delay between each subsequent repeat
public Repeater(long beforeRepeat, long betweenRepeat)
{
m_RepeatTimer = SafeTimer.Stopped(RepeatCallback);
m_BeforeRepeat = beforeRepeat;
m_BetweenRepeat = betweenRepeat;
}
///
/// Destructor.
///
~Repeater()
{
Dispose();
}
#endregion
#region Methods
///
/// Release resources.
///
public void Dispose()
{
m_RepeatTimer.Dispose();
}
///
/// Begin repeating.
///
[PublicAPI]
public void Start()
{
OnInitialRepeat.Raise(this);
m_RepeatTimer.Reset(m_BeforeRepeat, m_BetweenRepeat);
}
///
/// Stop repeating volume.
///
[PublicAPI]
public void Stop()
{
m_RepeatTimer.Stop();
}
#endregion
#region Private Methods
///
/// Called for every repeat.
///
private void RepeatCallback()
{
OnRepeat.Raise(this);
}
#endregion
}
}