You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Async/TimerAsyncBatchQueue.cs

32 lines
1.2 KiB

5 years ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace ZeroLevel.Services.Async
{
public class TimerAsyncBatchQueue<T>
: IAsyncBatchCollection<T>, IDisposable
{
private readonly Timer _flushTimer;
private readonly IAsyncBatchCollection<T> _innerCollection;
public TimerAsyncBatchQueue(IAsyncBatchCollection<T> innerCollection, TimeSpan flushPeriod)
{
_innerCollection = innerCollection;
_flushTimer = new Timer(_ => Flush(), null, flushPeriod, flushPeriod);
}
public int BatchSize => _innerCollection.BatchSize;
public int Count => _innerCollection.Count;
public void Add(T item) => _innerCollection.Add(item);
public ValueTask<IReadOnlyList<T>> TakeAsync(CancellationToken cancellationToken) => _innerCollection.TakeAsync(cancellationToken);
public void Flush() => _innerCollection.Flush();
public IEnumerator<IReadOnlyList<T>> GetEnumerator() => _innerCollection.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => (_innerCollection as IEnumerable).GetEnumerator();
public void Dispose() => _flushTimer.Dispose();
}
}

Powered by TurnKey Linux.