Ejemplo n.º 1
0
        /// <summary>
        /// Sign up a subscriber to a value's events
        /// </summary>
        /// <param name="sub"></param>
        public void Subscribe(ISubscriber <T> sub)
        {
            lock (_lock)
            {
                if (!SubscriberDictionaries.ContainsKey(sub.ValueName))
                {
                    //Add the subscriber to listen for value changes.
                    SubscriberDictionaries.Add(sub.ValueName, null);
                    SubscriberDictionaries[sub.ValueName] += sub.OnValueChange;
                }
                else
                {
                    SubscriberDictionaries[sub.ValueName] += sub.OnValueChange;
                }

                //Check if there is a default value yet.
                if (!CurrentValues.ContainsKey(sub.ValueName))
                {
                    CurrentValues.Add(sub.ValueName, _zeroValue);
                }

                //Set the value of the subscriber to the default via the built-in publish mechanism.
                sub.OnValueChange(this, new PubSubEventArgs <T>(_zeroValue, CurrentValues[sub.ValueName]));
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Unsubscribe a sub from its value's events.
 /// </summary>
 /// <param name="sub"></param>
 public void UnSubscribe(ISubscriber <T> sub)
 {
     lock (_lock)
     {
         if (SubscriberDictionaries.ContainsKey(sub.ValueName))
         {
             SubscriberDictionaries[sub.ValueName] -= sub.OnValueChange;
         }
     }
 }