Пример #1
0
        /// <summary>
        /// Registers a listener for a specific event
        /// </summary>
        /// <param name="eventType">Event the listener is listening for</param>
        /// <param name="listener">Listener receiving messages for the specified event</param>
        public void RegisterListener(EventType eventType, IEventHandler listener)
        {
            List<IEventHandler> listeners = RegisterEvent(eventType);
            if (listeners.Contains(listener))
                throw new Exception(string.Format("Listener {0} already listen to event {1}", listener, eventType.EventName));

            listeners.Add(listener);
        }
Пример #2
0
        /// <summary>
        /// Create or get an event type instance
        /// </summary>
        /// <param name="name">Name of the event</param>
        /// <returns>Return an instance of EventType</returns>
        public static EventType CreateEvent(string name)
        {
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");

            EventType ev;
            AvailableEvent.TryGetValue(name, out ev);
            if (ev != null) return ev;

            ev = new EventType(name);
            AvailableEvent.Add(name, ev);

            return ev;
        }
Пример #3
0
        /// <summary>
        /// Unregisters an event, mediator will stop dispatching message for this event
        /// </summary>
        /// <param name="eventType">Event to unregister</param>
        /// <returns>Return true if the event is unregistered, otherwise false</returns>
        private bool UnregisterEvent(EventType eventType)
        {
            List<IEventHandler> listeners;
            _eventListenersMap.TryGetValue(eventType.EventHash, out listeners);

            return (listeners != null) && _eventListenersMap.Remove(eventType.EventHash);
        }
Пример #4
0
        /// <summary>
        /// Registers a new event managed by the mediator, it will dispatches messages
        /// for this event
        /// </summary>
        /// <param name="eventType">Event to register</param>
        private List<IEventHandler> RegisterEvent(EventType eventType)
        {
            List<IEventHandler> listeners = GetListeners(eventType);
            if (listeners != null)
                return listeners;

            listeners = new List<IEventHandler>();
            _eventListenersMap.Add(eventType.EventHash, listeners);

            return listeners;
        }
Пример #5
0
        /// <summary>
        /// Gets list of listeners for a specific event
        /// </summary>
        /// <param name="eventType">Event type</param>
        /// <returns>Returns a list of listeners if found otherwise null</returns>
        private List<IEventHandler> GetListeners(EventType eventType)
        {
            List<IEventHandler> listeners;
            _eventListenersMap.TryGetValue(eventType.EventHash, out listeners);

            return listeners;
        }
Пример #6
0
        /// <summary>
        /// Unregisters a listener for a specific event
        /// </summary>
        /// <param name="eventType">Event to stop listening for</param>
        /// <param name="listener">Listener</param>
        /// <returns>Return true if the listener stop listening, otherwise false</returns>
        public void UnregisterListener(EventType eventType, IEventHandler listener)
        {
            List<IEventHandler> listeners = GetListeners(eventType);
            if (listeners == null) return;

            listeners.Remove(listener);
        }
Пример #7
0
        /// <summary>
        /// Unregisters all listeners for a specific event
        /// </summary>
        /// <param name="eventType">Event to stop listening for</param>
        public void UnregisterListener(EventType eventType)
        {
            List<IEventHandler> listeners = GetListeners(eventType);
            if(listeners == null) return;

            listeners.Clear();
        }