Created
March 30, 2017 12:20
-
-
Save dcolthorp/69de6379f384b5d14a1e6510b0911fba to your computer and use it in GitHub Desktop.
ReactiveProperty<T>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ReactiveProperty | |
{ | |
public static ReactiveProperty<T> Create<T>(IObservable<T> observable) => new ReactiveProperty<T>(observable); | |
} | |
public class ReactiveProperty<T> : INotifyPropertyChanged, IDisposable | |
{ | |
private readonly IDisposable _subscription; | |
private Exception _exception; | |
private T _value; | |
public ReactiveProperty(IObservable<T> observable) | |
{ | |
_subscription = observable.Subscribe( | |
v => { _value = v; Notify(); }, | |
ex => { _exception = ex; Notify(); }, | |
() => ExceptionHandling.Abort()); | |
} | |
public T Value | |
{ | |
get | |
{ | |
if (_exception != null) throw _exception; | |
return _value; | |
} | |
} | |
public void Dispose() | |
{ | |
_subscription.Dispose(); | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
private void Notify() | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment