mirror of
https://github.com/ayufan/steam-deck-tools.git
synced 2026-01-07 17:20:19 +01:00
CommonHelpers: Add TimedValue helper
This commit is contained in:
parent
759327c642
commit
afb55e6b16
40
CommonHelpers/TimedValue.cs
Normal file
40
CommonHelpers/TimedValue.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
namespace CommonHelpers
|
||||
{
|
||||
public class TimedValue<T> where T : struct
|
||||
{
|
||||
public T Value { get; }
|
||||
public DateTime ExpiryDate { get; }
|
||||
|
||||
public bool Valid
|
||||
{
|
||||
get => !Expired;
|
||||
}
|
||||
|
||||
public bool Expired
|
||||
{
|
||||
get => ExpiryDate < DateTime.UtcNow;
|
||||
}
|
||||
|
||||
public TimedValue(T value, TimeSpan ts)
|
||||
{
|
||||
this.Value = value;
|
||||
this.ExpiryDate = DateTime.UtcNow.Add(ts);
|
||||
}
|
||||
|
||||
public TimedValue(T value, int timeoutMs)
|
||||
: this(value, TimeSpan.FromMilliseconds(timeoutMs))
|
||||
{
|
||||
}
|
||||
|
||||
public bool TryGetValue(out T value)
|
||||
{
|
||||
value = this.Value;
|
||||
return Valid;
|
||||
}
|
||||
|
||||
public static implicit operator T?(TimedValue<T> tv)
|
||||
{
|
||||
return tv.Valid ? tv.Value : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue