mirror of
https://github.com/gwdevhub/Daybreak.git
synced 2026-07-15 23:29:46 +00:00
3044861652
Co-authored-by: Alexandru Macocian <amacocian@microsoft.com>
24 lines
638 B
C#
24 lines
638 B
C#
using System.Core.Extensions;
|
|
|
|
namespace Daybreak.API.Models;
|
|
|
|
public sealed class ByteConsumerEntry(
|
|
Guid id, TimeSpan freq, Action<ReadOnlySpan<byte>> handler)
|
|
{
|
|
private readonly Action<ReadOnlySpan<byte>> handler = handler.ThrowIfNull();
|
|
|
|
private DateTime lastConsume = DateTime.MinValue;
|
|
|
|
public Guid Id { get; } = id;
|
|
public TimeSpan Frequency { get; } = freq;
|
|
|
|
public void TryConsume(DateTime currentTime, ReadOnlySpan<byte> value)
|
|
{
|
|
if (currentTime - this.lastConsume >= this.Frequency)
|
|
{
|
|
this.handler(value);
|
|
this.lastConsume = currentTime;
|
|
}
|
|
}
|
|
}
|