终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

109 lignes
3.3 KiB

  1. using BPASmartClient.Bus.EventBus;
  2. using BPASmartClient.Helper;
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. /* ***********************************************
  10.  * subject 事件总线,总线入口,后续按类型分发
  11.  * author 张原川
  12.  * date   2019/6/3 15:49:03
  13.  * ***********************************************/
  14. namespace LandStation.Bus
  15. {
  16. public class EventBus : Singleton<EventBus>
  17. {
  18. //事件处理委托
  19. public delegate void EventCallBackHandle(params object[] args);
  20. //事件处理委托
  21. public delegate void EventHandle(IEvent @event, EventCallBackHandle callBack = null);
  22. //事件订阅者集合
  23. private ConcurrentDictionary<Type, List<EventHandle>> _eventHandls = new ConcurrentDictionary<Type, List<EventHandle>>();
  24. /// <summary>
  25. /// 事件订阅
  26. /// </summary>
  27. public void Subscribe<TEvent>(EventHandle handle)
  28. {
  29. if (!_eventHandls.ContainsKey(typeof(TEvent)))
  30. _eventHandls.TryAdd(typeof(TEvent), new List<EventHandle>());
  31. lock (_eventHandls)
  32. _eventHandls[typeof(TEvent)].Add(handle);
  33. }
  34. /// <summary>
  35. /// 事件退订
  36. /// </summary>
  37. public void UnSubscribe<TEvent>(EventHandle handle)
  38. {
  39. if (_eventHandls.ContainsKey(typeof(TEvent)))
  40. {
  41. if (_eventHandls[typeof(TEvent)].Contains(handle))
  42. {
  43. lock (_eventHandls)
  44. _eventHandls[typeof(TEvent)].Remove(handle);
  45. }
  46. }
  47. }
  48. /// <summary>
  49. /// 事件发布,不带返回
  50. /// </summary>
  51. public void Publish<TEvent>(TEvent @event) where TEvent : IEvent
  52. {
  53. if (_eventHandls.ContainsKey(typeof(TEvent)))
  54. {
  55. for (int i = _eventHandls[typeof(TEvent)].Count - 1; i >= 0; i--)
  56. _eventHandls[typeof(TEvent)][i](@event);
  57. //_eventHandls[typeof(TEvent)].ForEach(p =>
  58. //{
  59. // p(@event);
  60. //});
  61. }
  62. }
  63. /// <summary>
  64. /// 事件发布,带返回
  65. /// </summary>
  66. public void Publish<TEvent>(TEvent @event, EventCallBackHandle eventCallBack) where TEvent : IEvent
  67. {
  68. List<object> result = new List<object>();
  69. if (_eventHandls.ContainsKey(typeof(TEvent)))
  70. {
  71. //_eventHandls[typeof(TEvent)].ForEach(p =>
  72. //{
  73. // p(@event, delegate (object[] args)
  74. // {
  75. // result.AddRange(args);
  76. // });
  77. //});
  78. for (int i = _eventHandls[typeof(TEvent)].Count - 1; i >= 0; i--)
  79. {
  80. _eventHandls[typeof(TEvent)][i](@event, delegate (object[] args)
  81. {
  82. result.AddRange(args);
  83. });
  84. }
  85. }
  86. eventCallBack.Invoke(result.ToArray());
  87. }
  88. /// <summary>
  89. /// 事件总线释放
  90. /// </summary>
  91. public void Dispose()
  92. {
  93. _eventHandls.Clear();
  94. }
  95. }
  96. }