Example #1
0
        /// <summary>
        ///  移除观察者
        /// </summary>
        /// <param name="receiver"></param>
        /// <param name="msgId"></param>
        public void RemoveObserver(object receiver, ulong key)
        {
            List <ObserverDelegate> list;

            if (messagePool.TryGetValue(key, out list))
            {
                int i = 0;
                while (i < list.Count)
                {
                    ObserverDelegate o = list[i];
                    if (o.receiver == receiver)
                    {
                        list.RemoveAt(i);
                        o.Destroy();
                        break;
                    }
                    i++;
                }
            }
        }
Example #2
0
 public void Send(NotificationMessage msg)
 {
     try
     {
         List <ObserverDelegate> list;
         if (messagePool.TryGetValue(msg.Key, out list))
         {
             for (int i = 0; i < list.Count; i++)
             {
                 ObserverDelegate o = list[i];
                 if (o.receiver != null && o.handler != null)
                 {
                     o.handler(msg);
                 }
             }
         }
     }
     catch (Exception e)
     {
         HFLog.E(e);
         throw;
     }
 }
Example #3
0
        /// <summary>
        ///  添加一个观察者
        /// </summary>
        /// <param name="receiver"></param>
        /// <param name="msgID"></param>
        /// <param name="callback"></param>
        public void AddObserver(object receiver, ushort moduleId, int msgId, Action <NotificationMessage> callback)
        {
            if (receiver == null)
            {
                HFLog.E(" moduleID = " + moduleId + " msgID = " + msgId + " receiver 不能为空");
                return;
            }

            if (callback == null)
            {
                HFLog.E(" moduleID = " + moduleId + " msgID = " + msgId + " callback 不能为空");
                return;
            }

            ObserverDelegate        o = new ObserverDelegate(receiver, moduleId, msgId, callback);
            List <ObserverDelegate> list;

            if (!messagePool.TryGetValue(o.Key, out list))
            {
                list = new List <ObserverDelegate>();
                messagePool.Add(o.Key, list);
            }
            list.Add(o);
        }