Eabei.Events/EventsBus.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Eabei.Events
{
    public class EventsBus : Eabei.Helper.SingleInstance<EventsBus>
    {
        //事件处理委托
        public delegate void EventCallBackHandle(params object[] args);

        public delegate void EventBusHandler(IEvent @event, EventCallBackHandle callBack = null);

        private ConcurrentDictionary<string, List<EventBusHandler>> eventDictionary = new ConcurrentDictionary<string, List<EventBusHandler>>();


        public void Subscribe<TEvent>(EventBusHandler @handle) where TEvent : IEvent
        {
            string name = typeof(TEvent).Name;

            if (eventDictionary.ContainsKey(name))
                eventDictionary[name].Add(@handle);
            else
                eventDictionary.TryAdd(name, new List<EventBusHandler>() { @handle });
        }

        public void UnSubscribe<TEvent>(EventBusHandler @handle) where TEvent : IEvent
        {
            string name = typeof(TEvent).Name;

            if (eventDictionary.ContainsKey(name))
                eventDictionary[name].Remove(@handle);
        }

        /// <summary>
        /// 事件发布执行,不带返回
        /// </summary>
        public void Publish(IEvent @event, EventCallBackHandle eventCallBack)
        {
            string name = @event.GetType().Name;
            List<object> result = new List<object>();
            if (eventDictionary.ContainsKey(name))
            {
                for (int i = eventDictionary[name].Count - 1; i >= 0; i--)
                    eventDictionary[name][i](@event, delegate (object[] args)
                     {
                         result.AddRange(args);
                     });
            }
            eventCallBack.Invoke(result.ToArray());
        }

        /// <summary>
        /// 事件发布执行,不带返回
        /// </summary>
        public void Publish(IEvent @event)
        {
后面还有 17 行代码,购买后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。