Browse Source

Refactor ActivatedSubject

Inherit from ActivatedObject.
pull/396/head
Steven Kirk 11 years ago
parent
commit
ce997f6965
  1. 26
      src/Perspex.Styling/Styling/ActivatedObservable.cs
  2. 48
      src/Perspex.Styling/Styling/ActivatedSubject.cs

26
src/Perspex.Styling/Styling/ActivatedObservable.cs

@ -19,11 +19,6 @@ namespace Perspex.Styling
/// </remarks>
internal class ActivatedObservable : ObservableBase<object>, IDescription
{
/// <summary>
/// The activator.
/// </summary>
private readonly IObservable<bool> _activator;
/// <summary>
/// Initializes a new instance of the <see cref="ActivatedObservable"/> class.
/// </summary>
@ -35,26 +30,25 @@ namespace Perspex.Styling
IObservable<object> source,
string description)
{
_activator = activator;
Activator = activator;
Description = description;
Source = source;
}
/// <summary>
/// Gets the activator observable.
/// </summary>
public IObservable<bool> Activator { get; }
/// <summary>
/// Gets a description of the binding.
/// </summary>
public string Description
{
get;
}
public string Description { get; }
/// <summary>
/// Gets an observable which produces the <see cref="ActivatedValue"/>.
/// </summary>
public IObservable<object> Source
{
get;
}
public IObservable<object> Source { get; }
/// <summary>
/// Notifies the provider that an observer is to receive notifications.
@ -66,10 +60,10 @@ namespace Perspex.Styling
Contract.Requires<ArgumentNullException>(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()

48
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
/// <see cref="PerspexProperty.UnsetValue"/>.
/// </remarks>
internal class ActivatedSubject : ISubject<object>, IDescription
internal class ActivatedSubject : ActivatedObservable, ISubject<object>, IDescription
{
private IObservable<bool> _activator;
private bool _active;
private object _pushValue;
private object _value;
/// <summary>
/// Initializes a new instance of the <see cref="ActivatedSubject"/> class.
@ -34,28 +32,17 @@ namespace Perspex.Styling
IObservable<bool> activator,
ISubject<object> source,
string description)
: base(activator, source, description)
{
_activator = activator;
Description = description;
Source = source;
_activator.Skip(1).Subscribe(ActivatorChanged);
}
/// <summary>
/// Gets a description of the binding.
/// </summary>
public string Description
{
get;
Activator.Skip(1).Subscribe(ActivatorChanged);
}
/// <summary>
/// Gets the underlying subject.
/// </summary>
public ISubject<object> Source
public new ISubject<object> Source
{
get;
get { return (ISubject<object>)base.Source; }
}
/// <summary>
@ -88,7 +75,7 @@ namespace Perspex.Styling
/// <param name="value">The value to send to all subscribed observers.</param>
public void OnNext(object value)
{
_pushValue = value;
_value = value;
if (_active)
{
@ -96,29 +83,10 @@ namespace Perspex.Styling
}
}
/// <summary>
/// Notifies the provider that an observer is to receive notifications.
/// </summary>
/// <param name="observer">The observer.</param>
/// <returns>IDisposable object used to unsubscribe from the observable sequence.</returns>
public IDisposable Subscribe(IObserver<object> observer)
{
Contract.Requires<ArgumentNullException>(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);
}
}
}

Loading…
Cancel
Save