committed by
Steven Kirk
3 changed files with 51 additions and 83 deletions
@ -1,42 +0,0 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// 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.Disposables; |
|||
|
|||
namespace Avalonia.Reactive |
|||
{ |
|||
/// <summary>
|
|||
/// An <see cref="IObservable{T}"/> with an additional description.
|
|||
/// </summary>
|
|||
/// <typeparam name="T">The type of the elements in the sequence.</typeparam>
|
|||
public class AvaloniaObservable<T> : ObservableBase<T>, IDescription |
|||
{ |
|||
private readonly Func<IObserver<T>, IDisposable> _subscribe; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="AvaloniaObservable{T}"/> class.
|
|||
/// </summary>
|
|||
/// <param name="subscribe">The subscribe function.</param>
|
|||
/// <param name="description">The description of the observable.</param>
|
|||
public AvaloniaObservable(Func<IObserver<T>, IDisposable> subscribe, string description) |
|||
{ |
|||
Contract.Requires<ArgumentNullException>(subscribe != null); |
|||
|
|||
_subscribe = subscribe; |
|||
Description = description; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the description of the observable.
|
|||
/// </summary>
|
|||
public string Description { get; } |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override IDisposable SubscribeCore(IObserver<T> observer) |
|||
{ |
|||
return _subscribe(observer) ?? Disposable.Empty; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
using System; |
|||
|
|||
namespace Avalonia.Reactive |
|||
{ |
|||
public class AvaloniaPropertyObservable<T> : LightweightObservableBase<T>, IDescription |
|||
{ |
|||
private readonly IAvaloniaObject _target; |
|||
private readonly AvaloniaProperty _property; |
|||
private T _value; |
|||
|
|||
public AvaloniaPropertyObservable( |
|||
IAvaloniaObject target, |
|||
AvaloniaProperty property) |
|||
{ |
|||
_target = target; |
|||
_property = property; |
|||
} |
|||
|
|||
public string Description => $"{_target.GetType().Name}.{_property.Name}"; |
|||
|
|||
protected override void Initialize() |
|||
{ |
|||
_value = (T)_target.GetValue(_property); |
|||
_target.PropertyChanged += PropertyChanged; |
|||
} |
|||
|
|||
protected override void Deinitialize() |
|||
{ |
|||
_target.PropertyChanged -= PropertyChanged; |
|||
} |
|||
|
|||
protected override void Subscribed(IObserver<T> observer, bool first) |
|||
{ |
|||
observer.OnNext(_value); |
|||
} |
|||
|
|||
private void PropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e) |
|||
{ |
|||
if (e.Property == _property) |
|||
{ |
|||
_value = (T)e.NewValue; |
|||
PublishNext(_value); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue