Exemple #1
0
        private void OnPropertyChanged(object sender, PropertyChangedEventArgs args)
        {
            ForEachCallback(args.PropertyName, (callbacksForProperty, currentNode) =>
            {
                IWeakEventCallback <PropertyChangedEventArgs> callback = (IWeakEventCallback <PropertyChangedEventArgs>)currentNode.Value;

                if (!callback.Invoke(sender, args))
                {
                    callbacksForProperty.MarkForRemove(currentNode);
                }
                return(true);
            });
        }
Exemple #2
0
        private void OnPropertyChanging(object sender, PropertyChangingEventArgs args)
        {
            //var callbacksForProperty = LookupCallbacksForProperty(args.PropertyName);
            //if (callbacksForProperty == null)
            //{
            //    return;
            //}

            //ulong version = callbacksForProperty.Version;
            //var currentNode = callbacksForProperty.StartIterating(version);

            //try
            //{
            //    while (currentNode != null)
            //    {
            //        IWeakEventCallback<PropertyChangingEventArgs> callback = currentNode.Value as IWeakEventCallback<PropertyChangingEventArgs>;
            //        if (callback != null)
            //        {
            //            if (!callback.Invoke(sender, args))
            //            {
            //                callbacksForProperty.MarkForRemove(currentNode);
            //            }
            //        }
            //        currentNode = callbacksForProperty.GetNext(version, currentNode);
            //    }
            //}
            //finally
            //{
            //    callbacksForProperty.StopIterating();
            //}
            ForEachCallback(args.PropertyName, (callbacksForProperty, currentNode) =>
            {
                IWeakEventCallback <PropertyChangingEventArgs> callback = currentNode.Value as IWeakEventCallback <PropertyChangingEventArgs>;
                if (callback != null)
                {
                    if (!callback.Invoke(sender, args))
                    {
                        callbacksForProperty.MarkForRemove(currentNode);
                    }
                }
                return(true);
            });
        }
Exemple #3
0
        public void RemoveListener(object listener, string propertyName, object rootSubject)
        {
            ForEachCallback(propertyName, (callbacksForProperty, currentNode) =>
            {
                IWeakEventCallback <PropertyChangedEventArgs> callback = (IWeakEventCallback <PropertyChangedEventArgs>)currentNode.Value;
                object listenerForCallback    = callback.ListenerReference.Target;
                object rootSubjectForCallback = callback.RootSubject.Target;

                if (listenerForCallback == null)
                {
                    callbacksForProperty.MarkForRemove(currentNode);
                }
                else if (listenerForCallback == listener && rootSubject == rootSubjectForCallback)
                {
                    callbacksForProperty.MarkForRemove(currentNode);
                    return(false);
                }

                return(true);
            });
        }
 public Guid Register(IWeakEventCallback callback)
 {
     if (callback == null)
     {
         return Guid.Empty;
     }
     var id = Guid.NewGuid();
     _references[id] = new WeakReference<IWeakEventCallback>(callback);
     return id;
 }
 /// <summary>
 /// Register callback.
 /// </summary>
 /// <param name="callback">Weak event callback.</param>
 /// <returns>Callback registration token.</returns>
 public Guid AddCallback(IWeakEventCallback callback)
 {
     lock (_lock)
     {
         if (_containers.Count == 0)
         {
             _containers.Insert(0, new WeakEventContainer());
         }
         if (_containers[0].Count > MaxCount)
         {
             _containers.Insert(0, new WeakEventContainer());
         }
         return _containers[0].Register(callback);
     }
 }