Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. public static class ReactiveProperty
  2. {
  3. public static ReactiveProperty<T> Create<T>(IObservable<T> observable) => new ReactiveProperty<T>(observable);
  4. }
  5.  
  6. public class ReactiveProperty<T> : INotifyPropertyChanged, IDisposable
  7. {
  8. private readonly IDisposable _subscription;
  9. private Exception _exception;
  10. private T _value;
  11.  
  12. public ReactiveProperty(IObservable<T> observable)
  13. {
  14. _subscription = observable.Subscribe(
  15. v => { _value = v; Notify(); },
  16. ex => { _exception = ex; Notify(); },
  17. () => ExceptionHandling.Abort());
  18. }
  19.  
  20. public T Value
  21. {
  22. get
  23. {
  24. if (_exception != null) throw _exception;
  25. return _value;
  26. }
  27. }
  28.  
  29. public void Dispose()
  30. {
  31. _subscription.Dispose();
  32. }
  33.  
  34. public event PropertyChangedEventHandler PropertyChanged;
  35. private void Notify()
  36. {
  37. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement