예제 #1
0
        //--------------------------------------------------------------------------------------------------------------------------------------
        public bool RemoveEventHandler <T>(EventCb <T> cb)
        {
            //null check
            if (cb == null)
            {
                return(false);
            }

            //Get route
            var evType = typeof(T);

            lock (_ActiveRoutes)
            {
                var allRoutesForType = _ActiveRoutes.TryGetOrDefault(evType);

                //check if handlers are registered for this event type
                if (allRoutesForType == null)
                {
                    DebugEx.Assert("no such event found registered");
                    return(false);
                }

                //Remove from route
                var removed = allRoutesForType.RemoveWhere(h => Object.Equals(h.Cb, cb)) > 0;

                //check if this event type has any other handlers, otherwise remove the type as well
                if (allRoutesForType.Count == 0)
                {
                    _ActiveRoutes.Remove(evType);
                }

                //return if something has been removed or not
                return(removed);
            }
        }
예제 #2
0
        //--------------------------------------------------------------------------------------------------------------------------------------
        public bool AddEventHandler <T>(EventCb <T> cb, int priority, bool receiveDerivedTypeEvents = true)
        {
            //null check
            if (cb == null)
            {
                DebugEx.Assert("Cannot give Null callback for YEventRouter");
                return(false);
            }

            //Get or create route
            var evType = typeof(T);
            SortedSetTS <EvHandler> allRoutesForType;

            lock (_ActiveRoutes)
            {
                allRoutesForType = _ActiveRoutes.TryGetOrDefault(evType);
                if (allRoutesForType == null)
                {
                    allRoutesForType = _ActiveRoutes[evType] = new SortedSetTS <EvHandler>(s_EvHandlerCmp);
                }

                //add to route
                return(allRoutesForType.Add(new EvHandler()
                {
                    Cb = cb,
                    Priority = priority,
                    ReceiveDerivedTypeEvents = receiveDerivedTypeEvents,
                    RunAsync = false
                }));
            }
        }