Notifications.cs 867 B

12345678910111213141516171819202122232425262728
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace InABox.Core.Notifications
  5. {
  6. public class Notifications
  7. {
  8. private Dictionary<Type, INotificationHandler> Handlers = new Dictionary<Type, INotificationHandler>();
  9. public void AddHandler<TNotification>(NotificationHandler<TNotification> handler)
  10. {
  11. Handlers[typeof(TNotification)] = handler;
  12. }
  13. public void AddHandler<TNotification>(NotificationHandler<TNotification>.ReceiveEvent receive)
  14. {
  15. Handlers[typeof(TNotification)] = new NotificationHandler<TNotification>(receive);
  16. }
  17. public void Notify(Type type, object? notification)
  18. {
  19. if (Handlers.TryGetValue(type, out var handler))
  20. {
  21. handler.Receive(notification);
  22. }
  23. }
  24. }
  25. }