终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

129 行
4.3 KiB

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