Browse Source

Use LightweightObservableBase for WeakCollectionChangedObservable

pull/1690/head
Steven Kirk 8 years ago
parent
commit
de325ec501
  1. 59
      src/Avalonia.Base/Collections/NotifyCollectionChangedExtensions.cs

59
src/Avalonia.Base/Collections/NotifyCollectionChangedExtensions.cs

@ -2,13 +2,9 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information. // Licensed under the MIT license. See licence.md file in the project root for full license information.
using System; using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reactive.Subjects; using Avalonia.Reactive;
using Avalonia.Utilities; using Avalonia.Utilities;
namespace Avalonia.Collections namespace Avalonia.Collections
@ -43,9 +39,8 @@ namespace Avalonia.Collections
Contract.Requires<ArgumentNullException>(collection != null); Contract.Requires<ArgumentNullException>(collection != null);
Contract.Requires<ArgumentNullException>(handler != null); Contract.Requires<ArgumentNullException>(handler != null);
return return collection.GetWeakCollectionChangedObservable()
collection.GetWeakCollectionChangedObservable() .Subscribe(e => handler(collection, e));
.Subscribe(e => handler.Invoke(collection, e));
} }
/// <summary> /// <summary>
@ -63,18 +58,13 @@ namespace Avalonia.Collections
Contract.Requires<ArgumentNullException>(collection != null); Contract.Requires<ArgumentNullException>(collection != null);
Contract.Requires<ArgumentNullException>(handler != null); Contract.Requires<ArgumentNullException>(handler != null);
return return collection.GetWeakCollectionChangedObservable().Subscribe(handler);
collection.GetWeakCollectionChangedObservable()
.Subscribe(handler);
} }
private class WeakCollectionChangedObservable : ObservableBase<NotifyCollectionChangedEventArgs>, private class WeakCollectionChangedObservable : LightweightObservableBase<NotifyCollectionChangedEventArgs>,
IWeakSubscriber<NotifyCollectionChangedEventArgs> IWeakSubscriber<NotifyCollectionChangedEventArgs>
{ {
private WeakReference<INotifyCollectionChanged> _sourceReference; private WeakReference<INotifyCollectionChanged> _sourceReference;
private readonly Subject<NotifyCollectionChangedEventArgs> _changed = new Subject<NotifyCollectionChangedEventArgs>();
private int _count;
public WeakCollectionChangedObservable(WeakReference<INotifyCollectionChanged> source) public WeakCollectionChangedObservable(WeakReference<INotifyCollectionChanged> source)
{ {
@ -83,43 +73,28 @@ namespace Avalonia.Collections
public void OnEvent(object sender, NotifyCollectionChangedEventArgs e) public void OnEvent(object sender, NotifyCollectionChangedEventArgs e)
{ {
_changed.OnNext(e); PublishNext(e);
} }
protected override IDisposable SubscribeCore(IObserver<NotifyCollectionChangedEventArgs> observer) protected override void Initialize()
{ {
if (_sourceReference.TryGetTarget(out INotifyCollectionChanged instance)) if (_sourceReference.TryGetTarget(out INotifyCollectionChanged instance))
{ {
if (_count++ == 0) WeakSubscriptionManager.Subscribe(
{ instance,
WeakSubscriptionManager.Subscribe( nameof(instance.CollectionChanged),
instance, this);
nameof(instance.CollectionChanged),
this);
}
return Observable.Using(() => Disposable.Create(DecrementCount), _ => _changed)
.Subscribe(observer);
}
else
{
_changed.OnCompleted();
observer.OnCompleted();
return Disposable.Empty;
} }
} }
private void DecrementCount() protected override void Deinitialize()
{ {
if (--_count == 0) if (_sourceReference.TryGetTarget(out INotifyCollectionChanged instance))
{ {
if (_sourceReference.TryGetTarget(out INotifyCollectionChanged instance)) WeakSubscriptionManager.Unsubscribe(
{ instance,
WeakSubscriptionManager.Unsubscribe( nameof(instance.CollectionChanged),
instance, this);
nameof(instance.CollectionChanged),
this);
}
} }
} }
} }

Loading…
Cancel
Save