Exemplo n.º 1
0
 private IDisposable AddInternal(WeakEventListenerWrapper weakItem, string path)
 {
     lock (_propertyChanged)
     {
         //if value was removed from another thread
         if (ReferenceEquals(_listeners, Empty))
         {
             var source = (INotifyPropertyChanged)_propertyChanged.Target;
             if (source != null)
             {
                 var state = new object[] { this, weakItem, path, null };
                 ServiceProvider.AttachedValueProvider.AddOrUpdate(source, PropertyChangedMember, AddSourceEventDelegate, UpdateSourceEventDelegate, state);
                 return((IDisposable)state[3]);
             }
         }
         else if (_listeners.Length == 0)
         {
             _listeners = new[] { new KeyValuePair <WeakEventListenerWrapper, string>(weakItem, path) }
         }
         ;
         else
         {
             Update(weakItem, path);
         }
     }
     return(new Unsubscriber(this, weakItem));
 }
Exemplo n.º 2
0
        protected void Update(WeakEventListenerWrapper newItem)
        {
            WeakEventListenerWrapper[] references = newItem.IsEmpty
                ? new WeakEventListenerWrapper[Listeners.Length]
                : new WeakEventListenerWrapper[Listeners.Length + 1];
            int index = 0;

            for (int i = 0; i < Listeners.Length; i++)
            {
                WeakEventListenerWrapper reference = Listeners[i];
                if (reference.EventListener.IsAlive)
                {
                    references[index++] = reference;
                }
            }
            if (!newItem.IsEmpty)
            {
                references[index] = newItem;
                index++;
            }
            else if (index == 0)
            {
                OnEmpty();
                return;
            }
            if (references.Length != index)
            {
                Array.Resize(ref references, index);
            }
            Listeners = references;
        }
Exemplo n.º 3
0
 protected IDisposable AddInternal(WeakEventListenerWrapper weakItem, bool withUnsubscriber)
 {
     //it's normal here.
     lock (this)
     {
         IDisposable disposable;
         if (OnAdd(weakItem, withUnsubscriber, out disposable))
         {
             return(disposable);
         }
         if (Listeners.Length == 0)
         {
             Listeners = new[] { weakItem }
         }
         ;
         else
         {
             Update(weakItem);
         }
     }
     if (withUnsubscriber)
     {
         return(new Unsubscriber(this, weakItem));
     }
     return(null);
 }
 public SizeObserver(View view, IEventListener handler)
     : base(view, false)
 {
     _listenerRef = handler.ToWeakWrapper();
     _height      = view.Height;
     _width       = view.Width;
 }
Exemplo n.º 5
0
        private void Cleanup()
        {
            var size = _size;

            _size        = 0;
            _removedSize = 0;
            for (int i = 0; i < size; i++)
            {
                var reference = _listeners[i];
                if (reference.EventListener.IsAlive)
                {
                    _listeners[_size++] = reference;
                }
            }
            if (_size == 0)
            {
                _listeners = Empty.Array <WeakEventListenerWrapper>();
            }
            else if (_listeners.Length / (float)_size > 2)
            {
                var listeners = new WeakEventListenerWrapper[_size + (_size >> 2)];
                Array.Copy(_listeners, 0, listeners, 0, _size);
                _listeners = listeners;
            }
        }
Exemplo n.º 6
0
            public DependencyPropertyListener(DependencyObject source, string propertyToBind, IEventListener listener)
            {
                _listener = listener.ToWeakWrapper();
                BindingOperations.SetBinding(this, ValueProperty,
                                             new BindingEx
                {
                    Path   = new PropertyPath(propertyToBind),
                    Source = source,
                    Mode   = BindingMode.OneWay,
#if !WINDOWS_PHONE
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
#endif

#if !WINDOWSCOMMON
#if !WPF || !NET4
                    ValidatesOnNotifyDataErrors = false,
#endif
                    ValidatesOnDataErrors   = false,
                    ValidatesOnExceptions   = false,
                    NotifyOnValidationError = false,
#endif

#if WPF
                    NotifyOnSourceUpdated = false,
                    NotifyOnTargetUpdated = false
#endif
                });
            }
Exemplo n.º 7
0
                public void Dispose()
                {
                    var listener = _eventListener;
                    var weakItem = _weakItem;

                    if (listener != null && !weakItem.IsEmpty)
                    {
                        _eventListener = null;
                        _weakItem      = WeakEventListenerWrapper.Empty;
                        listener.Remove(weakItem);
                    }
                }
Exemplo n.º 8
0
 private void Remove(WeakEventListenerWrapper weakItem)
 {
     lock (_propertyChanged)
     {
         for (int i = 0; i < _listeners.Length; i++)
         {
             if (ReferenceEquals(_listeners[i].Key.Source, weakItem.Source))
             {
                 _listeners[i] = new KeyValuePair <WeakEventListenerWrapper, string>(WeakEventListenerWrapper.Empty, string.Empty);
                 Update(WeakEventListenerWrapper.Empty, null);
                 return;
             }
         }
     }
 }
Exemplo n.º 9
0
 private void Dispose(bool dispose)
 {
     if (_listener.IsEmpty)
     {
         return;
     }
     if (dispose)
     {
         _listener = WeakEventListenerWrapper.Empty;
     }
     for (int i = 0; i < Count; i++)
     {
         this[i].Dispose();
     }
     Clear();
 }
Exemplo n.º 10
0
 private void Remove(WeakEventListenerWrapper weakItem)
 {
     //it's normal here.
     lock (this)
     {
         for (int i = 0; i < Listeners.Length; i++)
         {
             if (ReferenceEquals(Listeners[i].Source, weakItem.Source))
             {
                 Listeners[i] = WeakEventListenerWrapper.Empty;
                 Update(WeakEventListenerWrapper.Empty);
                 return;
             }
         }
     }
 }
Exemplo n.º 11
0
 private void Remove(WeakEventListenerWrapper weakItem, string propertyName)
 {
     lock (this)
     {
         for (int i = 0; i < _listeners.Length; i++)
         {
             var pair = _listeners[i];
             if (!pair.Key.IsEmpty && pair.Value == propertyName && ReferenceEquals(pair.Key.Source, weakItem.Source))
             {
                 ++_removedSize;
                 _listeners[i] = default(KeyValuePair <WeakEventListenerWrapper, string>);
                 return;
             }
         }
     }
 }
Exemplo n.º 12
0
 private void Remove(WeakEventListenerWrapper weakItem)
 {
     //it's normal here.
     lock (this)
     {
         for (int i = 0; i < _listeners.Length; i++)
         {
             var wrapper = _listeners[i];
             if (!wrapper.IsEmpty && ReferenceEquals(wrapper.Source, weakItem.Source))
             {
                 RemoveAt(i);
                 return;
             }
         }
     }
 }
Exemplo n.º 13
0
            /// <summary>
            /// Occurs on add a listener.
            /// </summary>
            protected override bool OnAdd(WeakEventListenerWrapper weakItem, bool withUnsubscriber, out IDisposable unsubscriber)
            {
                unsubscriber = null;
                if (!ReferenceEquals(Listeners, Empty))
                {
                    return(false);
                }
                var source = _sourceRef.Target;

                if (source == null)
                {
                    return(false);
                }
                var    state  = new object[] { this, weakItem, null };
                string member = _eventInfo == null ? BindingContextMember : EventPrefix + _eventInfo.Name;

                ServiceProvider.AttachedValueProvider.AddOrUpdate(source, member, AddSourceEventDelegate, UpdateSourceEventDelegate, state);
                unsubscriber = (IDisposable)state[2];
                return(true);
            }
Exemplo n.º 14
0
 private IDisposable AddInternal(WeakEventListenerWrapper weakItem, bool withUnsubscriber)
 {
     lock (this)
     {
         if (_listeners.Length == 0)
         {
             _listeners   = new[] { weakItem };
             _size        = 1;
             _removedSize = 0;
         }
         else
         {
             if (_removedSize == 0)
             {
                 if (_size == _listeners.Length)
                 {
                     EnsureCapacity(ref _listeners, _size, _size + 1);
                 }
                 _listeners[_size++] = weakItem;
             }
             else
             {
                 for (int i = 0; i < _size; i++)
                 {
                     if (_listeners[i].IsEmpty)
                     {
                         _listeners[i] = weakItem;
                         --_removedSize;
                         break;
                     }
                 }
             }
         }
     }
     if (withUnsubscriber)
     {
         return(new Unsubscriber(this, weakItem));
     }
     return(null);
 }
Exemplo n.º 15
0
            private void Update(WeakEventListenerWrapper newItem, string path)
            {
                var references = newItem.IsEmpty
                    ? new KeyValuePair <WeakEventListenerWrapper, string> [_listeners.Length]
                    : new KeyValuePair <WeakEventListenerWrapper, string> [_listeners.Length + 1];
                int index = 0;

                for (int i = 0; i < _listeners.Length; i++)
                {
                    var reference = _listeners[i];
                    if (reference.Key.EventListener.IsAlive)
                    {
                        references[index++] = reference;
                    }
                }
                if (!newItem.IsEmpty)
                {
                    references[index] = new KeyValuePair <WeakEventListenerWrapper, string>(newItem, path);
                    index++;
                }
                else if (index == 0)
                {
                    //Remove event from source, if no listeners.
                    _listeners = Empty;
                    var target = (INotifyPropertyChanged)_propertyChanged.Target;
                    if (target != null)
                    {
                        target.PropertyChanged -= Handle;
                        ServiceProvider.AttachedValueProvider.Clear(target, PropertyChangedMember);
                    }
                    return;
                }
                if (references.Length != index)
                {
                    Array.Resize(ref references, index);
                }
                _listeners = references;
            }
Exemplo n.º 16
0
 private IDisposable AddInternal(WeakEventListenerWrapper weakItem, string path)
 {
     lock (this)
     {
         if (_listeners.Length == 0)
         {
             _listeners   = new[] { new KeyValuePair <WeakEventListenerWrapper, string>(weakItem, path) };
             _size        = 1;
             _removedSize = 0;
         }
         else
         {
             if (_removedSize == 0)
             {
                 if (_size == _listeners.Length)
                 {
                     EventListenerList.EnsureCapacity(ref _listeners, _size, _size + 1);
                 }
                 _listeners[_size++] = new KeyValuePair <WeakEventListenerWrapper, string>(weakItem, path);
             }
             else
             {
                 for (int i = 0; i < _size; i++)
                 {
                     if (_listeners[i].Key.IsEmpty)
                     {
                         _listeners[i] = new KeyValuePair <WeakEventListenerWrapper, string>(weakItem, path);
                         --_removedSize;
                         break;
                     }
                 }
             }
         }
     }
     return(new Unsubscriber(this, weakItem, path));
 }
 public VisiblityObserver(View view, IEventListener handler)
     : base(view, true)
 {
     _listenerRef = handler.ToWeakWrapper();
     _oldValue    = view.Visibility;
 }
Exemplo n.º 18
0
 public Unsubscriber(WeakPropertyChangedListener eventListener, WeakEventListenerWrapper weakItem, string propertyName)
 {
     _eventListener = eventListener;
     _weakItem      = weakItem;
     _propertyName  = propertyName;
 }
Exemplo n.º 19
0
 protected virtual bool OnAdd(WeakEventListenerWrapper weakItem, bool withUnsubscriber, out IDisposable unsubscriber)
 {
     unsubscriber = null;
     return(false);
 }
Exemplo n.º 20
0
 public RootObserver(object target, IEventListener listener)
 {
     _reference = ServiceProvider.WeakReferenceFactory(target, true);
     _listener  = listener.ToWeakWrapper();
     TryHandle(null, null);
 }
Exemplo n.º 21
0
 public Unsubscriber(WeakPropertyChangedListener eventListener, WeakEventListenerWrapper weakItem)
 {
     _eventListener = eventListener;
     _weakItem      = weakItem;
 }
Exemplo n.º 22
0
 public Unsubscriber(EventListenerList eventListener, WeakEventListenerWrapper weakItem)
 {
     _eventListener = eventListener;
     _weakItem      = weakItem;
 }