コード例 #1
0
        public static void TriggerEvent <TSubscriber>(Action <TSubscriber> action) where TSubscriber : IGlobalSubscriber
        {
            List <Type> nestedInterfaces = EventBusHelper.GetNestedInterfaces(typeof(TSubscriber));

            bool wasIterating = s_IsIterating;

            s_IsIterating = true;

            foreach (Type nestedInterface in nestedInterfaces)
            {
                if (s_GlobalSubscribers.ContainsKey(nestedInterface))
                {
                    foreach (GlobalSubscriberNode globalSubscriber in s_GlobalSubscribers[nestedInterface])
                    {
                        if (!globalSubscriber.IsReleased)
                        {
                            globalSubscriber.TriggerAction(action);
                        }
                    }
                }
            }

            s_IsIterating = wasIterating;
            if (!s_IsIterating)
            {
                CleanUp();
            }
        }
コード例 #2
0
        public static IDisposable Subscribe(IGlobalSubscriber subscriber)
        {
            List <Type> implementedGlobalSubscribers = EventBusHelper.GetImplementedGlobalSubscribers(subscriber);

            bool wasIterating = s_IsIterating;

            s_IsIterating = true;

            foreach (Type interfaceType in implementedGlobalSubscribers)
            {
                List <GlobalSubscriberNode> correspondingList;
                if (s_GlobalSubscribers.ContainsKey(interfaceType))
                {
                    correspondingList = s_GlobalSubscribers[interfaceType];
                }
                else
                {
                    correspondingList = new List <GlobalSubscriberNode>();
                    s_GlobalSubscribers[interfaceType] = correspondingList;
                }

                correspondingList.Add(new GlobalSubscriberNode(subscriber));
            }

            s_IsIterating = wasIterating;
            if (!s_IsIterating)
            {
                CleanUp();
            }

            return(new DisposeAction(() => Unsubscribe(subscriber)));
        }
コード例 #3
0
        private static void Unsubscribe(IGlobalSubscriber subscriber)
        {
            List <Type> implementedGlobalSubscribers = EventBusHelper.GetImplementedGlobalSubscribers(subscriber);

            foreach (Type interfaceType in implementedGlobalSubscribers)
            {
                if (s_GlobalSubscribers.ContainsKey(interfaceType))
                {
                    List <GlobalSubscriberNode> subscribers = s_GlobalSubscribers[interfaceType];
                    subscribers.FirstOrDefault(node => node.SubscriberEquals(subscriber))?.Release();
                }
            }

            if (!s_IsIterating)
            {
                CleanUp();
            }
        }