Browse Source

Use LightweightObservableBase in TypeNameAndClassSelector

pull/1690/head
Steven Kirk 8 years ago
parent
commit
9b0aff8cc1
  1. 32
      src/Avalonia.Styling/Styling/TypeNameAndClassSelector.cs

32
src/Avalonia.Styling/Styling/TypeNameAndClassSelector.cs

@ -4,12 +4,10 @@
using System; using System;
using System.Collections.Generic; 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.Reflection; using System.Reflection;
using System.Text; using System.Text;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Reactive;
namespace Avalonia.Styling namespace Avalonia.Styling
{ {
@ -200,10 +198,9 @@ namespace Avalonia.Styling
return builder.ToString(); return builder.ToString();
} }
private class ClassObserver : IObservable<bool> private class ClassObserver : LightweightObservableBase<bool>
{ {
readonly IList<string> _match; readonly IList<string> _match;
readonly List<IObserver<bool>> _observers = new List<IObserver<bool>>();
IAvaloniaReadOnlyList<string> _classes; IAvaloniaReadOnlyList<string> _classes;
public ClassObserver(IAvaloniaReadOnlyList<string> classes, IList<string> match) public ClassObserver(IAvaloniaReadOnlyList<string> classes, IList<string> match)
@ -212,34 +209,19 @@ namespace Avalonia.Styling
_match = match; _match = match;
} }
public IDisposable Subscribe(IObserver<bool> observer) protected override void Deinitialize() => _classes.CollectionChanged -= ClassesChanged;
{ protected override void Initialize() => _classes.CollectionChanged += ClassesChanged;
if (_observers.Count == 0)
{
_classes.CollectionChanged += ClassesChanged;
}
_observers.Add(observer); protected override void Subscribed(IObserver<bool> observer, bool first)
{
observer.OnNext(GetResult()); observer.OnNext(GetResult());
return Disposable.Create(() =>
{
_observers.Remove(observer);
if (_observers.Count == 0)
{
_classes.CollectionChanged -= ClassesChanged;
}
});
} }
private void ClassesChanged(object sender, NotifyCollectionChangedEventArgs e) private void ClassesChanged(object sender, NotifyCollectionChangedEventArgs e)
{ {
if (e.Action != NotifyCollectionChangedAction.Move) if (e.Action != NotifyCollectionChangedAction.Move)
{ {
foreach (var observer in _observers) PublishNext(GetResult());
{
observer.OnNext(GetResult());
}
} }
} }

Loading…
Cancel
Save