终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

EventBus.cs 3.3 KiB

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