using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using static System.Runtime.InteropServices.JavaScript.JSType; namespace EVCB_OCPP.WSServer.Helper; public class QueueHandler { public QueueHandler(Func handler = null,int maxConcurrency = 1) { _queue = new (); this._handler = handler; this._semaphore = new (maxConcurrency); this._handlerPair = new(); } public void Enqueue(T data) { _queue.Enqueue(data); _ = TrtStartHandler(); } public void Enqueue(T data, Func handler) { _queue.Enqueue(data); _handlerPair.Add(data, handler); _ = TrtStartHandler(); } private readonly ConcurrentQueue _queue; private readonly Func _handler; private readonly SemaphoreSlim _semaphore; private readonly Dictionary> _handlerPair; private async Task TrtStartHandler() { if(_semaphore.Wait(0)) { return; } if(!_queue.TryDequeue(out var data)) { return; } var handler = GetHandler(data); await handler(data); _semaphore.Release(); _ = TrtStartHandler(); } private Func GetHandler(T data) { if (_handlerPair.TryGetValue(data, out Func value)) { return value; } if (_handler is not null) { return _handler; } return (data) => Task.CompletedTask; } }