|
| 1 | +// Copyright (c) 2023 .NET Foundation and Contributors. All rights reserved. |
| 2 | +// Licensed to the .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | +// See the LICENSE file in the project root for full license information. |
| 5 | + |
| 6 | +namespace ReactiveUI; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// ReactiveProperty - a two way bindable declarative observable property with imperative get set. |
| 10 | +/// </summary> |
| 11 | +/// <typeparam name="T">The type of the property.</typeparam> |
| 12 | +/// <seealso cref="ReactiveObject" /> |
| 13 | +/// <seealso cref="IReactiveProperty<T>" /> |
| 14 | +[DataContract] |
| 15 | +public class ReactiveProperty<T> : ReactiveObject, IReactiveProperty<T> |
| 16 | +{ |
| 17 | + private readonly IScheduler _scheduler; |
| 18 | + private readonly CompositeDisposable _disposables = []; |
| 19 | + private T? _value; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Initializes a new instance of the <see cref="ReactiveProperty{T}"/> class. |
| 23 | + /// </summary> |
| 24 | + public ReactiveProperty() => _scheduler = RxApp.TaskpoolScheduler; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Initializes a new instance of the <see cref="ReactiveProperty{T}"/> class. |
| 28 | + /// </summary> |
| 29 | + /// <param name="initialValue">The initial value.</param> |
| 30 | + public ReactiveProperty(T? initialValue) |
| 31 | + { |
| 32 | + Value = initialValue; |
| 33 | + _scheduler = RxApp.TaskpoolScheduler; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Initializes a new instance of the <see cref="ReactiveProperty{T}"/> class. |
| 38 | + /// </summary> |
| 39 | + /// <param name="initialValue">The initial value.</param> |
| 40 | + /// <param name="scheduler">The scheduler.</param> |
| 41 | + public ReactiveProperty(T? initialValue, IScheduler? scheduler) |
| 42 | + { |
| 43 | + Value = initialValue; |
| 44 | + _scheduler = scheduler ?? RxApp.TaskpoolScheduler; |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Gets a value indicating whether gets a value that indicates whether the object is disposed. |
| 49 | + /// </summary> |
| 50 | + public bool IsDisposed => _disposables.IsDisposed; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Gets or sets the value. |
| 54 | + /// </summary> |
| 55 | + /// <value> |
| 56 | + /// The value. |
| 57 | + /// </value> |
| 58 | + [DataMember] |
| 59 | + [JsonInclude] |
| 60 | + public T? Value |
| 61 | + { |
| 62 | + get => _value; |
| 63 | + set => this.RaiseAndSetIfChanged(ref _value, value); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. |
| 68 | + /// </summary> |
| 69 | + public void Dispose() |
| 70 | + { |
| 71 | + Dispose(true); |
| 72 | + GC.SuppressFinalize(this); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Notifies the provider that an observer is to receive notifications. |
| 77 | + /// </summary> |
| 78 | + /// <param name="observer">The object that is to receive notifications.</param> |
| 79 | + /// <returns> |
| 80 | + /// A reference to an interface that allows observers to stop receiving notifications before |
| 81 | + /// the provider has finished sending them. |
| 82 | + /// </returns> |
| 83 | + public IDisposable Subscribe(IObserver<T?> observer) => |
| 84 | + this.WhenAnyValue(vm => vm.Value) |
| 85 | + .ObserveOn(_scheduler) |
| 86 | + .Subscribe(observer) |
| 87 | + .DisposeWith(_disposables); |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Releases unmanaged and - optionally - managed resources. |
| 91 | + /// </summary> |
| 92 | + /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> |
| 93 | + protected virtual void Dispose(bool disposing) |
| 94 | + { |
| 95 | + if (_disposables?.IsDisposed == false && disposing) |
| 96 | + { |
| 97 | + _disposables?.Dispose(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments