// (c) Copyright Ascensio System SIA 2010-2022 // // This program is a free software product. // You can redistribute it and/or modify it under the terms // of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software // Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended // to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of // any third-party rights. // // This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see // the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html // // You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021. // // The interactive user interfaces in modified source and object code versions of the Program must // display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3. // // Pursuant to Section 7(b) of the License you must retain the original Product logo when // distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under // trademark law for use of our trademarks. // // All the Product's GUI elements, including illustrations and icon sets, as well as technical writing // content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0 // International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode namespace ASC.EventBus; public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager { private readonly Dictionary> _handlers; private readonly List _eventTypes; public event EventHandler OnEventRemoved; public InMemoryEventBusSubscriptionsManager() { _handlers = new Dictionary>(); _eventTypes = new List(); } public bool IsEmpty => _handlers is { Count: 0 }; public void Clear() => _handlers.Clear(); public void AddDynamicSubscription(string eventName) where TH : IDynamicIntegrationEventHandler { DoAddSubscription(typeof(TH), eventName, isDynamic: true); } public void AddSubscription() where T : IntegrationEvent where TH : IIntegrationEventHandler { var eventName = GetEventKey(); DoAddSubscription(typeof(TH), eventName, isDynamic: false); if (!_eventTypes.Contains(typeof(T))) { _eventTypes.Add(typeof(T)); } } private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic) { if (!HasSubscriptionsForEvent(eventName)) { _handlers.Add(eventName, new List()); } if (_handlers[eventName].Any(s => s.HandlerType == handlerType)) { throw new ArgumentException( $"Handler Type {handlerType.Name} already registered for '{eventName}'", nameof(handlerType)); } if (isDynamic) { _handlers[eventName].Add(SubscriptionInfo.Dynamic(handlerType)); } else { _handlers[eventName].Add(SubscriptionInfo.Typed(handlerType)); } } public void RemoveDynamicSubscription(string eventName) where TH : IDynamicIntegrationEventHandler { var handlerToRemove = FindDynamicSubscriptionToRemove(eventName); DoRemoveHandler(eventName, handlerToRemove); } public void RemoveSubscription() where TH : IIntegrationEventHandler where T : IntegrationEvent { var handlerToRemove = FindSubscriptionToRemove(); var eventName = GetEventKey(); DoRemoveHandler(eventName, handlerToRemove); } private void DoRemoveHandler(string eventName, SubscriptionInfo subsToRemove) { if (subsToRemove != null) { _handlers[eventName].Remove(subsToRemove); if (!_handlers[eventName].Any()) { _handlers.Remove(eventName); var eventType = _eventTypes.SingleOrDefault(e => e.Name == eventName); if (eventType != null) { _eventTypes.Remove(eventType); } RaiseOnEventRemoved(eventName); } } } public IEnumerable GetHandlersForEvent() where T : IntegrationEvent { var key = GetEventKey(); return GetHandlersForEvent(key); } public IEnumerable GetHandlersForEvent(string eventName) => _handlers[eventName]; private void RaiseOnEventRemoved(string eventName) { var handler = OnEventRemoved; handler?.Invoke(this, eventName); } private SubscriptionInfo FindDynamicSubscriptionToRemove(string eventName) where TH : IDynamicIntegrationEventHandler { return DoFindSubscriptionToRemove(eventName, typeof(TH)); } private SubscriptionInfo FindSubscriptionToRemove() where T : IntegrationEvent where TH : IIntegrationEventHandler { var eventName = GetEventKey(); return DoFindSubscriptionToRemove(eventName, typeof(TH)); } private SubscriptionInfo DoFindSubscriptionToRemove(string eventName, Type handlerType) { if (!HasSubscriptionsForEvent(eventName)) { return null; } return _handlers[eventName].SingleOrDefault(s => s.HandlerType == handlerType); } public bool HasSubscriptionsForEvent() where T : IntegrationEvent { var key = GetEventKey(); return HasSubscriptionsForEvent(key); } public bool HasSubscriptionsForEvent(string eventName) => _handlers.ContainsKey(eventName); public Type GetEventTypeByName(string eventName) => _eventTypes.SingleOrDefault(t => t.Name == eventName); public string GetEventKey() { return typeof(T).Name; } }