PushHandler.cs 1.0 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 IPushHandler
  9. {
  10. /// <summary>
  11. /// Receives a new push from the server, called when a push is pushed.
  12. /// </summary>
  13. /// <param name="o">The push object</param>
  14. void Receive(object? o);
  15. }
  16. public class PushHandler<TPush> : IPushHandler
  17. {
  18. public delegate void ReceiveEvent(TPush push);
  19. public event ReceiveEvent? OnReceive;
  20. public PushHandler() { }
  21. public PushHandler(ReceiveEvent receive)
  22. {
  23. OnReceive += receive;
  24. }
  25. public void Receive(object? push) => Receive((TPush)push);
  26. /// <summary>
  27. /// Receives a new push message from the server, called when it is pushed.
  28. /// </summary>
  29. /// <param name="push">The push message received</param>
  30. public void Receive(TPush push)
  31. {
  32. OnReceive?.Invoke(push);
  33. }
  34. }
  35. }