/// <summary> /// 摘除一个监听eventKey消息的消息监听器 /// 监听器将于下一逻辑帧执行前摘除 /// </summary> /// <param name="listener"></param> /// <param name="eventKey"></param> public void DetachListener(string type, EventListenerDele _listener) { if (threadSafe) { lock (m_listenersToUpdate) { m_listenersToUpdate.Enqueue(ListenerPack.Get(EventInfo.Get(type, _listener), type, false)); } } else { m_listenersToUpdate.Enqueue(ListenerPack.Get(EventInfo.Get(type, _listener), type, false)); } }
/// <summary> /// 挂接一个监听eventKey消息的消息监听器 /// 监听器将于下一逻辑帧执行前挂接 /// </summary> /// <param name="type">消息id</param> /// <param name="_listener">回调函数</param> /// <param name="_priority">优先级</param> /// <param name="_dispatchOnce">是否只接受一次</param> public void AttachListener(string type, EventListenerDele _listener, int _priority = 0, bool _dispatchOnce = false) { if (threadSafe) { lock (m_listenersToUpdate) { m_listenersToUpdate.Enqueue(ListenerPack.Get(EventInfo.Get(type, _listener, _priority, _dispatchOnce), type, true)); } } else { m_listenersToUpdate.Enqueue(ListenerPack.Get(EventInfo.Get(type, _listener, _priority, _dispatchOnce), type, true)); } }
public static ListenerPack Get(EventInfo info, string eKey, bool addOrRe) { ListenerPack pack = null; if (mListenerPackPools.Count > 0) { pack = mListenerPackPools.Dequeue(); pack.Set(info, eKey, addOrRe); } else { pack = new ListenerPack(info, eKey, addOrRe); } return(pack); }
private void UpdateListenerMap() { if (m_DelingEventArray.Count > 0) { foreach (uint eventKey in m_DelingEventArray) { if (eventKey == 0) { continue; } var listenerList = m_listenerTable[eventKey]; for (int i = 0; i < listenerList.Count;) { if (listenerList[i].Target == null) { listenerList.RemoveAt(i); continue; } ++i; } if (listenerList.Count == 0) { m_listenerTable.Remove(eventKey); } } m_DelingEventArray.Clear(); } lock (m_listenersToUpdateCache) { while (m_listenersToUpdateCache.Count != 0) { ListenerPack pack = m_listenersToUpdateCache.Dequeue() as ListenerPack; if (pack.m_addOrRemove) { AttachListenerNow(pack.m_listener.Target as IEventListener, pack.m_eventKey); } else { DetachListenerNow(pack.m_listener.Target as IEventListener, pack.m_eventKey); } } } }
/// <summary> /// 更新消息监听器表格的实现 /// </summary> private void _UpdateListenerMap() { int countListenerPack = m_listenersToUpdate.Count; while (countListenerPack != 0) { ListenerPack pack = (ListenerPack)m_listenersToUpdate.Dequeue(); countListenerPack--; if (pack.addOrRemove) { AttachListenerNow(pack.eventKey, pack.listener.listener, pack.listener.priority, pack.listener.dispatchOnce); } else { DetachListenerNow(pack.eventKey, pack.listener.listener); } } }
public static void Return(ListenerPack info) { info.Clean(); mListenerPackPools.Enqueue(info); }