Exemplo n.º 1
0
        /// <summary>
        /// Subscribes a given method to any changes in dependencies.
        /// </summary>
        /// <param name="subscribe">A method that takes in a Cell as an argument.</param>
        /// <returns></returns>
        private bool Subscribe(Action <Cell> subscribe)
        {
            if (subscribeCallback == null)
            {
                subscribeCallback = subscribe;
            }
            else
            {
                //Unsubscribe to start fresh.
                Unsubscribe();
            }

            //We have all dependencies, so if the owner is in it, there's a problem.
            if (Dependencies.Contains(Owner.Name))
            {
                ErrorCallback(Owner);
                return(false);
            }

            subscribeCallback = subscribe;

            //Subscribe to all direct dependencies.
            foreach (Dependency dependency in Dependencies.Cast <DictionaryEntry>().Select(d => d.Value as Dependency).Where(dependency => dependency.IsDirect))
            {
                dependency.Cell.Changed += subscribeCallback;
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Unsubscribes from all dependencies.
        /// </summary>
        public void Unsubscribe()
        {
            if (subscribeCallback == null)
            {
                return;
            }

            foreach (Dependency dependency in Dependencies.Cast <DictionaryEntry>().Select(d => d.Value as Dependency).Where(dependency => dependency.IsDirect))
            {
                dependency.Cell.Changed -= subscribeCallback;
            }

            subscribeCallback = null;
        }