sharplib/time/Time.cs
2026-02-09 14:57:45 -08:00

53 lines
787 B
C#

namespace time;
// This class should be static. It will then only run occasionally, based on passed in TimeSpan
public class EveryState
{
public DateTime Last { get; private set; }
public TimeSpan Interval { get; private set; }
public bool Latch { get; private set; }
public EveryState( TimeSpan interval )
{
Last = DateTime.Now;
Interval = interval;
}
//For first active call, Latch will be true. It will remain true until the next first call to Every
// This allows cohesive between different functions
public void Every( Action fn )
{
var now = DateTime.Now;
if( now - Last >= Interval )
{
Latch = true;
fn();
Last = now;
}
else
{
Latch = false;
}
}
public void Contingent( Action fn )
{
if( Latch )
{
fn();
}
}
}