Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement