IW4M-Admin/SharedLibraryCore/Helpers/ChangeTracking.cs

38 lines
960 B
C#
Raw Normal View History

using SharedLibraryCore.Interfaces;
using System;
2018-09-16 18:51:11 -04:00
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;
namespace SharedLibraryCore.Helpers
{
/// <summary>
/// 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>
{
2018-09-16 18:51:11 -04:00
ConcurrentQueue<T> Values;
public ChangeTracking()
{
2018-09-16 18:51:11 -04:00
Values = new ConcurrentQueue<T>();
}
public void OnChange(T value)
{
2018-09-16 18:51:11 -04:00
if (Values.Count > 30)
Values.TryDequeue(out T throwAway);
Values.Enqueue(value);
}
2018-09-16 18:51:11 -04:00
public T GetNextChange()
{
2018-09-16 18:51:11 -04:00
bool itemDequeued = Values.TryDequeue(out T val);
return itemDequeued ? val : default(T);
}
2018-09-16 18:51:11 -04:00
public bool HasChanges => Values.Count > 0;
}
}