NotificationHandler.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace InABox.Core
  7. {
  8. public interface INotificationHandler
  9. {
  10. /// <summary>
  11. /// Receives a new notification from the server, called when a notification is pushed.
  12. /// </summary>
  13. /// <param name="o">The notification object</param>
  14. void Receive(object? o);
  15. }
  16. public class NotificationHandler<TNotification> : INotificationHandler
  17. {
  18. public delegate void ReceiveEvent(TNotification notification);
  19. public event ReceiveEvent? OnReceive;
  20. public NotificationHandler() { }
  21. public NotificationHandler(ReceiveEvent receive)
  22. {
  23. OnReceive += receive;
  24. }
  25. public void Receive(object? notification) => Receive((TNotification)notification);
  26. /// <summary>
  27. /// Receives a new notification from the server, called when a notification is pushed.
  28. /// </summary>
  29. /// <param name="notification">The notification received</param>
  30. public void Receive(TNotification notification)
  31. {
  32. OnReceive?.Invoke(notification);
  33. }
  34. }
  35. }