Пример #1
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
                }));
            }
        }
Пример #2
0
        //--------------------------------------------------------------------------------------------------------------------------------------
        private void _ExecuteHandler <T>(object sender, T ev, object priv, bool forceAsync)
        {
            //declares
            SortedSetTS <EvHandler> allRoutesForType = null;
            var evType = typeof(T);

            //null check
            if (ev == null)
            {
                return;
            }

            //create event info
            var evInfo = new EvInfo()
            {
                Priv           = priv,
                IsDerivedMatch = false,
            };

            //Execute
            var executedCallbacks = new HashSet <object>();

            while (evType != null && evType != typeof(object))
            {
                //get routes for type
                allRoutesForType = _ActiveRoutes.TryGetOrDefault(evType);
                if (allRoutesForType != null)
                {
                    //Broadcast event
                    foreach (var evhandle in allRoutesForType)
                    {
                        if (!evInfo.IsDerivedMatch || evhandle.ReceiveDerivedTypeEvents)
                        {
                            try
                            {
                                //check that we haven't already run this callback
                                if (!executedCallbacks.Contains(evhandle.Cb))
                                {
                                    //run synchronously or asynchronously?
                                    if (evhandle.RunAsync)
                                    {
                                        //proper async mode; callbacks will be executed in series
                                        EventQueue.Enqueue(new EventDescriptor()
                                        {
                                            Ev = ev, EvHandle = evhandle, EvInfo = evInfo, Sender = sender
                                        });
                                    }
                                    else if (forceAsync)
                                    {
                                        //caller did TriggerEventAsync() and callback expects strong type
                                        TaskEx.RunSafe(() => __exec(sender, ev, evInfo, evhandle));
                                    }
                                    else
                                    {
                                        __exec(sender, ev, evInfo, evhandle);
                                    }
                                    //add to hashset so that it's not executed again
                                    executedCallbacks.Add(evhandle.Cb);
                                }
                            }
                            catch (Exception ex)
                            {
                                DebugEx.TraceErrorException(ex);
                            }
                        }
                    }
                }

                //now moving up the inheritance tree (toward object)
#if NETFX
                evType = evType.BaseType;
#elif UNIVERSAL
                evType = evType.GetTypeInfo().BaseType;
#endif
                evInfo.IsDerivedMatch = true;
            }
        }
Пример #3
0
        //--------------------------------------------------------------------------------------------------------------------------------------
        private void _ExecuteHandler <T>(object sender, T ev)
        {
            //declares
            SortedSetTS <EvHandler> allRoutesForType = null;
            var evType = typeof(T);

            //null check
            if (ev == null)
            {
                return;
            }

            //create event info
            var evInfo = new EvInfo()
            {
                priv           = null,
                IsDerivedMatch = false,
            };

            //Execute
            var executedCallbacks = new HashSet <object>();

            while (evType != null && evType != typeof(object))
            {
                //get routes for type
                allRoutesForType = _ActiveRoutes.TryGetOrDefault(evType);
                if (allRoutesForType != null)
                {
                    //Broadcast event
                    foreach (var evhandle in allRoutesForType)
                    {
                        if (!evInfo.IsDerivedMatch || evhandle.ReceiveDerivedTypeEvents)
                        {
                            try
                            {
                                //check that we haven't already run this callback
                                if (!executedCallbacks.Contains(evhandle.Cb))
                                {
                                    //use Dynamic callback if derived type; use known type otherwise
                                    if (evInfo.IsDerivedMatch)
                                    {
                                        ((dynamic)evhandle.Cb)(sender, evInfo, ev);
                                    }
                                    else
                                    {
                                        ((EventCb <T>)evhandle.Cb)(sender, evInfo, ev);
                                    }

                                    //add to hashset so that it's not executed again
                                    executedCallbacks.Add(evhandle.Cb);
                                }
                            }
                            catch (Exception ex)
                            {
                                DebugEx.TraceErrorException(ex);
                            }
                        }
                    }
                }

                //now moving up the inheritance tree (toward object)
#if NETFX
                evType = evType.BaseType;
#elif UNIVERSAL
                evType = evType.GetTypeInfo().BaseType;
#endif
                evInfo.IsDerivedMatch = true;
            }
        }