using System; using System.Collections; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace ZeroLevel.Services.Async { public class TimerAsyncBatchQueue : IAsyncBatchCollection, IDisposable { private readonly Timer _flushTimer; private readonly IAsyncBatchCollection _innerCollection; public TimerAsyncBatchQueue(IAsyncBatchCollection 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> TakeAsync(CancellationToken cancellationToken) => _innerCollection.TakeAsync(cancellationToken); public void Flush() => _innerCollection.Flush(); public IEnumerator> GetEnumerator() => _innerCollection.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => (_innerCollection as IEnumerable).GetEnumerator(); public void Dispose() => _flushTimer.Dispose(); } }