From ce997f6965e9e1190e1cecbb88190ff7f50e1253 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 22 Jan 2016 22:10:05 +0100 Subject: [PATCH] Refactor ActivatedSubject Inherit from ActivatedObject. --- .../Styling/ActivatedObservable.cs | 26 ++++------ .../Styling/ActivatedSubject.cs | 48 ++++--------------- 2 files changed, 18 insertions(+), 56 deletions(-) diff --git a/src/Perspex.Styling/Styling/ActivatedObservable.cs b/src/Perspex.Styling/Styling/ActivatedObservable.cs index ed313fa3a6..97c1b73106 100644 --- a/src/Perspex.Styling/Styling/ActivatedObservable.cs +++ b/src/Perspex.Styling/Styling/ActivatedObservable.cs @@ -19,11 +19,6 @@ namespace Perspex.Styling /// internal class ActivatedObservable : ObservableBase, IDescription { - /// - /// The activator. - /// - private readonly IObservable _activator; - /// /// Initializes a new instance of the class. /// @@ -35,26 +30,25 @@ namespace Perspex.Styling IObservable source, string description) { - _activator = activator; + Activator = activator; Description = description; Source = source; } + /// + /// Gets the activator observable. + /// + public IObservable Activator { get; } + /// /// Gets a description of the binding. /// - public string Description - { - get; - } + public string Description { get; } /// /// Gets an observable which produces the . /// - public IObservable Source - { - get; - } + public IObservable Source { get; } /// /// Notifies the provider that an observer is to receive notifications. @@ -66,10 +60,10 @@ namespace Perspex.Styling Contract.Requires(observer != null); var sourceCompleted = Source.TakeLast(1).Select(_ => Unit.Default); - var activatorCompleted = _activator.TakeLast(1).Select(_ => Unit.Default); + var activatorCompleted = Activator.TakeLast(1).Select(_ => Unit.Default); var completed = sourceCompleted.Merge(activatorCompleted); - return _activator + return Activator .CombineLatest(Source, (x, y) => new { Active = x, Value = y }) .Select(x => x.Active ? x.Value : PerspexProperty.UnsetValue) .DistinctUntilChanged() diff --git a/src/Perspex.Styling/Styling/ActivatedSubject.cs b/src/Perspex.Styling/Styling/ActivatedSubject.cs index d99ffae105..99da010dae 100644 --- a/src/Perspex.Styling/Styling/ActivatedSubject.cs +++ b/src/Perspex.Styling/Styling/ActivatedSubject.cs @@ -2,7 +2,6 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; -using System.Reactive; using System.Reactive.Linq; using System.Reactive.Subjects; @@ -18,11 +17,10 @@ namespace Perspex.Styling /// produce the current activated value. When the activator produces false it will produce /// . /// - internal class ActivatedSubject : ISubject, IDescription + internal class ActivatedSubject : ActivatedObservable, ISubject, IDescription { - private IObservable _activator; private bool _active; - private object _pushValue; + private object _value; /// /// Initializes a new instance of the class. @@ -34,28 +32,17 @@ namespace Perspex.Styling IObservable activator, ISubject source, string description) + : base(activator, source, description) { - _activator = activator; - Description = description; - Source = source; - - _activator.Skip(1).Subscribe(ActivatorChanged); - } - - /// - /// Gets a description of the binding. - /// - public string Description - { - get; + Activator.Skip(1).Subscribe(ActivatorChanged); } /// /// Gets the underlying subject. /// - public ISubject Source + public new ISubject Source { - get; + get { return (ISubject)base.Source; } } /// @@ -88,7 +75,7 @@ namespace Perspex.Styling /// The value to send to all subscribed observers. public void OnNext(object value) { - _pushValue = value; + _value = value; if (_active) { @@ -96,29 +83,10 @@ namespace Perspex.Styling } } - /// - /// Notifies the provider that an observer is to receive notifications. - /// - /// The observer. - /// IDisposable object used to unsubscribe from the observable sequence. - public IDisposable Subscribe(IObserver observer) - { - Contract.Requires(observer != null); - - var completed = _activator.TakeLast(1).Select(_ => Unit.Default); - - return _activator - .CombineLatest(Source, (x, y) => new { Active = x, Value = y }) - .Select(x => x.Active ? x.Value : PerspexProperty.UnsetValue) - .DistinctUntilChanged() - .TakeUntil(completed) - .Subscribe(observer); - } - private void ActivatorChanged(bool active) { _active = active; - Source.OnNext(active ? _pushValue : PerspexProperty.UnsetValue); + Source.OnNext(active ? _value : PerspexProperty.UnsetValue); } } }