IW4M-Admin/SharedLibraryCore/Helpers/ChangeTracking.cs

36 lines
906 B
C#
Raw Normal View History

2022-01-26 11:32:16 -05:00
using System.Collections.Concurrent;
namespace SharedLibraryCore.Helpers
{
/// <summary>
2022-01-26 11:32:16 -05:00
/// This class provides a way to keep track of changes to an entity
/// </summary>
/// <typeparam name="T">Type of entity to keep track of changes to</typeparam>
public class ChangeTracking<T>
{
2022-01-26 11:32:16 -05:00
private readonly ConcurrentQueue<T> Values;
public ChangeTracking()
{
2018-09-16 18:51:11 -04:00
Values = new ConcurrentQueue<T>();
}
2022-01-26 11:32:16 -05:00
public bool HasChanges => Values.Count > 0;
public void OnChange(T value)
{
2018-09-16 18:51:11 -04:00
if (Values.Count > 30)
2022-01-26 11:32:16 -05:00
{
Values.TryDequeue(out var throwAway);
}
2018-09-16 18:51:11 -04:00
Values.Enqueue(value);
}
2018-09-16 18:51:11 -04:00
public T GetNextChange()
{
2022-01-26 11:32:16 -05:00
var itemDequeued = Values.TryDequeue(out var val);
return itemDequeued ? val : default;
}
}
2022-01-26 11:32:16 -05:00
}