Skip to content

Instantly share code, notes, and snippets.

@dcolthorp
Created March 30, 2017 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcolthorp/69de6379f384b5d14a1e6510b0911fba to your computer and use it in GitHub Desktop.
Save dcolthorp/69de6379f384b5d14a1e6510b0911fba to your computer and use it in GitHub Desktop.
ReactiveProperty<T>
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