Notify.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace InABox.Core
  5. {
  6. public class Notify
  7. {
  8. private static List<IPollHandler> Handlers = new List<IPollHandler>();
  9. private static List<Notifier> Notifiers { get; set; } = new List<Notifier>();
  10. private Notify() { }
  11. public static void AddNotifier(Notifier notifier) =>
  12. Notifiers.Add(notifier);
  13. public static void Push<TNotification>(TNotification notification) where TNotification : BaseObject =>
  14. Notifiers.ForEach(x => x.Push(notification));
  15. public static void Push<TNotification>(Guid session, TNotification notification) where TNotification : BaseObject =>
  16. Notifiers.ForEach(x => x.Push(session, notification));
  17. public static void PushUser<TNotification>(Guid userID, TNotification notification) where TNotification : BaseObject =>
  18. Notifiers.ForEach(x => x.PushUser(userID, notification));
  19. public static void Push<TNotification>(Platform platform, TNotification notification) where TNotification : BaseObject =>
  20. Notifiers.ForEach(x => x.Push(platform, notification));
  21. public static void Poll(Guid session)
  22. {
  23. foreach (var handler in Handlers)
  24. {
  25. foreach (var notification in handler.Poll(session))
  26. {
  27. Notifiers.ForEach(x => x.Push(session, handler.Type, notification));
  28. }
  29. }
  30. }
  31. public static void AddPollHandler<TNotification>(PollHandler<TNotification> handler)
  32. where TNotification : BaseObject
  33. {
  34. Handlers.Add(handler);
  35. }
  36. public static void AddPollHandler<TNotification>(PollHandler<TNotification>.PollEvent poll)
  37. where TNotification : BaseObject
  38. {
  39. Handlers.Add(new PollHandler<TNotification>(poll));
  40. }
  41. }
  42. }