internal IStrongListener ListenImpl(Action <T> handler) { IWeakListener innerListener = this.ListenWeakImpl(handler); StrongListener listener = null; listener = new StrongListener( () => { innerListener.Unlisten(); // ReSharper disable AccessToModifiedClosure if (listener != null) { lock (this.KeepListenersAlive) { this.KeepListenersAlive.StopKeepingListenerAlive(listener); } } // ReSharper restore AccessToModifiedClosure }, innerListener); lock (this.KeepListenersAlive) { this.KeepListenersAlive.KeepListenerAlive(listener); } return(listener); }
public void TestMultipleUnlistenWeak() { StreamSink <int> s = Stream.CreateSink <int>(); List <int> @out = new List <int>(); ((Action)(() => { // ReSharper disable once UnusedVariable IWeakListener l = s.ListenWeak(@out.Add); s.Send(1); l.Unlisten(); l.Unlisten(); s.Send(2); l.Unlisten(); }))(); s.Send(3); s.Send(4); Assert.AreEqual(1, @out.Count); }
protected override void OnSourceChanged(object oldSource, object newSource) { base.OnSourceChanged(oldSource, newSource); if (Listener != null) { Listener.Detach(); Listener = null; } if (newSource is INotifyCollectionChanged) { Listener = new WeakCollectionChangedListener((INotifyCollectionChanged)newSource, this); } GetIndexer(); }
public void TestListenWeak() { StreamSink <int> s = Stream.CreateSink <int>(); List <int> @out = new List <int>(); ((Action)(() => { // ReSharper disable once UnusedVariable IWeakListener l = s.ListenWeak(@out.Add); s.Send(1); s.Send(2); }))(); GC.Collect(0, GCCollectionMode.Forced); s.Send(3); s.Send(4); Assert.AreEqual(2, @out.Count); }
internal virtual void OnItemsSourceChanged(IEnumerable oldSource, IEnumerable newSource) { if (CollectionListener != null) { CollectionListener.Detach(); CollectionListener = null; } if (newSource != null) { if (newSource is INotifyCollectionChanged) { CollectionListener = new WeakCollectionChangedListener(((INotifyCollectionChanged)newSource), this); } Items.SetIsReadOnly(true); itemsIsDataBound = true; Items.ClearImpl(); foreach (var v in newSource) { Items.AddImpl(v); } // Setting itemsIsDataBound to true prevents normal notifications from propagating, so do it manually here OnItemsChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } else { itemsIsDataBound = false; Items.SetIsReadOnly(false); Items.ClearImpl(); } // Yes this is stupid and shouldn't be here, but DRT 348 sets an empty collection as the ItemsSource // and expects the LayoutUpdated event to be raised. This is the only way that makes sense for this // to happen. It's all very strange. InvalidateMeasure(); }
public static IWeakListener AppendWeak(IWeakListener listener1, IWeakListener listener2) => CreateWeakComposite(new[] { listener1, listener2 });
protected override void OnSourceChanged (object oldSource, object newSource) { base.OnSourceChanged (oldSource, newSource); if (Listener != null) { Listener.Detach (); Listener = null; } if (newSource is INotifyCollectionChanged) Listener = new WeakCollectionChangedListener ((INotifyCollectionChanged) newSource, this); GetIndexer (); }
public void AddListener(IWeakListener listener) { this.listeners.Add(listener); }
internal virtual void OnItemsSourceChanged (IEnumerable oldSource, IEnumerable newSource) { if (CollectionListener != null) { CollectionListener.Detach (); CollectionListener = null; } if (newSource != null) { if (newSource is INotifyCollectionChanged) { CollectionListener = new WeakCollectionChangedListener (((INotifyCollectionChanged)newSource), this); } Items.SetIsReadOnly (true); itemsIsDataBound = true; Items.ClearImpl (); foreach (var v in newSource) Items.AddImpl (v); // Setting itemsIsDataBound to true prevents normal notifications from propagating, so do it manually here OnItemsChanged (new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Reset)); } else { itemsIsDataBound = false; Items.SetIsReadOnly (false); Items.ClearImpl (); } // Yes this is stupid and shouldn't be here, but DRT 348 sets an empty collection as the ItemsSource // and expects the LayoutUpdated event to be raised. This is the only way that makes sense for this // to happen. It's all very strange. InvalidateMeasure (); }