using SharedLibraryCore.Interfaces; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace IW4MAdmin.Application.Misc { class MiddlewareActionHandler : IMiddlewareActionHandler { private static readonly IDictionary> _actions = new Dictionary>(); public async Task Execute(T value, string name = null) { string key = string.IsNullOrEmpty(name) ? typeof(T).ToString() : name; if (_actions.ContainsKey(key)) { foreach (var action in _actions[key]) { try { value = await ((IMiddlewareAction)action).Invoke(value); } // todo: probably log this somewhere catch { } } return value; } return value; } public void Register(T actionType, IMiddlewareAction action, string name = null) { string key = string.IsNullOrEmpty(name) ? typeof(T).ToString() : name; if (_actions.ContainsKey(key)) { _actions[key].Add(action); } else { _actions.Add(key, new[] { action }); } } } }