/// <summary> /// 取消订阅 /// </summary> /// <param name="item">订阅项</param> public static void UnSubscibe(SubscibeItem item) { if (item == null) { return; } SubscibeGroup subscibeGroup; if (!_subscibeItemDic.TryGetValue(item.Topic, out subscibeGroup)) { return; } subscibeGroup.Remove(item); //如果订阅组内的订阅项数为0,则移除该订阅组 if (subscibeGroup.Count == 0) { lock (_subscibeItemDicLock) { if (subscibeGroup.Count == 0) { subscibeGroup.Dispose(); _subscibeItemDic.TryRemove(item.Topic, out subscibeGroup); } } } }
/// <summary> /// 移除订阅项 /// </summary> /// <param name="item">订阅项</param> /// <returns>移除结果</returns> public bool Remove(SubscibeItem item) { if (item == null) { return(false); } Monitor.Enter(this._subscibeItemListMonitor); try { return(this._subscibeItemList.Remove(item)); } finally { Monitor.Exit(this._subscibeItemListMonitor); } }
/// <summary> /// 订阅 /// </summary> /// <param name="item">订阅项</param> public static void Subscibe(SubscibeItem item) { SubscibeGroup subscibeGroup; if (!_subscibeItemDic.TryGetValue(item.Topic, out subscibeGroup)) { lock (_subscibeItemDicLock) { if (!_subscibeItemDic.TryGetValue(item.Topic, out subscibeGroup)) { subscibeGroup = new SubscibeGroup(item.Topic); _subscibeItemDic.TryAdd(item.Topic, subscibeGroup); } } } subscibeGroup.Add(item); }
/// <summary> /// 添加订阅项 /// </summary> /// <param name="item">订阅项</param> public void Add(SubscibeItem item) { if (item == null) { return; } Monitor.Enter(this._subscibeItemListMonitor); try { if (!this._subscibeItemList.Contains(item)) { this._subscibeItemList.Add(item); } } finally { Monitor.Exit(this._subscibeItemListMonitor); } }