Ejemplo n.º 1
0
        /// <summary>
        /// Publish event.
        /// </summary>
        /// <param name="eventMessage">Event message.</param>
        public void Publish <T> (T eventMessage)
        {
            var eventType = typeof(T);
            FastList <Func <T, bool> > list = null;

            lock (_syncObj) {
                if (_eventsInCall.Contains(eventType))
                {
                    Debug.LogError("Already in calling of " + eventType.Name);
                    return;
                }
                object objList;
                if (_events.TryGetValue(eventType, out objList))
                {
                    list = (FastList <Func <T, bool> >)objList;

                    // kept for no new GC alloc, but empty.
                    if (list.Count == 0)
                    {
                        list = null;
                    }
                }
                if (list != null)
                {
                    _eventsInCall.Add(eventType);
                }
            }
            if (list != null)
            {
                var cacheList = _eventSpecificListCaches[eventType];
                int i;
                int iMax;
                var listData = list.GetData(out iMax);
                cacheList.Reserve(iMax, true, false);
                var cacheListData = cacheList.GetData();
                // we cant use direct copy because cached list dont know T-generic type of event.
                for (i = 0; i < iMax; i++)
                {
                    cacheListData[i] = listData[i];
                }
                try {
                    for (i = 0; i < iMax; i++)
                    {
                        if (((Func <T, bool>)cacheListData[i])(eventMessage))
                        {
                            // Event was interrupted / processed, we can exit.
                            return;
                        }
                    }
                } finally {
                    cacheList.Clear();
                    lock (_syncObj) {
                        _eventsInCall.Remove(eventType);
                    }
                }
            }
        }