public void UnsubscribeFromType(string a_Type, OnThingEvent a_Callback = null, string a_Path = "") { if (m_SubscriberMap.ContainsKey(a_Path)) { Dictionary <string, List <Subscriber> > types = m_SubscriberMap[a_Path]; if (types.ContainsKey(a_Type)) { if (a_Callback != null) { List <Subscriber> subs = types[a_Type]; for (int i = 0; i < subs.Count; ++i) { if (subs[i].m_Callback == a_Callback) { subs.RemoveAt(i); break; } } if (subs.Count == 0) { types.Remove(a_Type); } } else { types.Remove(a_Type); } } // only remove if this was the last subscriber for the given type.. if (!types.ContainsKey(a_Type)) { Dictionary <string, object> unsubscribe = new Dictionary <string, object>(); unsubscribe["event"] = "unsubscribe_from_type"; unsubscribe["type"] = a_Type; TopicClient.Instance.Publish(a_Path + "blackboard", Json.Serialize(unsubscribe)); } } }
public void SubscribeToType(string a_Type, OnThingEvent a_Callback, ThingEventType a_EventMask = ThingEventType.TE_ALL, string a_Path = "") { if (!m_SubscriberMap.ContainsKey(a_Path)) { TopicClient.Instance.Subscribe(a_Path + "blackboard", OnBlackBoardEvent); m_SubscriberMap[a_Path] = new Dictionary <string, List <Subscriber> >(); } Dictionary <string, List <Subscriber> > types = m_SubscriberMap[a_Path]; if (!types.ContainsKey(a_Type)) { types[a_Type] = new List <Subscriber>(); Dictionary <string, object> subscribe = new Dictionary <string, object>(); subscribe["event"] = "subscribe_to_type"; subscribe["type"] = a_Type; subscribe["event_mask"] = (int)ThingEventType.TE_ALL; // we want all events, we will filter those events on this side TopicClient.Instance.Publish(a_Path + "blackboard", Json.Serialize(subscribe)); } types[a_Type].Add(new Subscriber(a_Callback, a_EventMask, a_Path)); }
public Subscriber(OnThingEvent a_Callback, ThingEventType a_EventMask, string a_Path) { m_Callback = a_Callback; m_EventMask = a_EventMask; m_Path = a_Path; }