瀏覽代碼

Introduce SerialDisposableValue.

Daniel Weber 5 年之前
父節點
當前提交
29e65f2f4b

+ 5 - 5
Rx.NET/Source/src/System.Reactive/Disposables/SerialDisposable.cs

@@ -9,7 +9,7 @@ namespace System.Reactive.Disposables
     /// </summary>
     public sealed class SerialDisposable : ICancelable
     {
-        private IDisposable? _current;
+        private SerialDisposableValue _current;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.SerialDisposable"/> class.
@@ -21,7 +21,7 @@ namespace System.Reactive.Disposables
         /// <summary>
         /// Gets a value that indicates whether the object is disposed.
         /// </summary>
-        public bool IsDisposed => Disposables.Disposable.GetIsDisposed(ref _current);
+        public bool IsDisposed => _current.IsDisposed;
 
         /// <summary>
         /// Gets or sets the underlying disposable.
@@ -29,8 +29,8 @@ namespace System.Reactive.Disposables
         /// <remarks>If the SerialDisposable has already been disposed, assignment to this property causes immediate disposal of the given disposable object. Assigning this property disposes the previous disposable object.</remarks>
         public IDisposable? Disposable
         {
-            get => Disposables.Disposable.GetValue(ref _current);
-            set => Disposables.Disposable.TrySetSerial(ref _current, value);
+            get => _current.Disposable;
+            set => _current.Disposable = value;
         }
 
         /// <summary>
@@ -38,7 +38,7 @@ namespace System.Reactive.Disposables
         /// </summary>
         public void Dispose()
         {
-            Disposables.Disposable.Dispose(ref _current);
+            _current.Dispose();
         }
     }
 }

+ 37 - 0
Rx.NET/Source/src/System.Reactive/Disposables/SerialDisposableValue.cs

@@ -0,0 +1,37 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT License.
+// See the LICENSE file in the project root for more information. 
+
+namespace System.Reactive.Disposables
+{
+    /// <summary>
+    /// Represents a disposable resource whose underlying disposable resource can be replaced by another disposable resource, causing automatic disposal of the previous underlying disposable resource.
+    /// </summary>
+    internal struct SerialDisposableValue : ICancelable
+    {
+        private IDisposable? _current;
+
+        /// <summary>
+        /// Gets a value that indicates whether the object is disposed.
+        /// </summary>
+        public bool IsDisposed => Disposables.Disposable.GetIsDisposed(ref _current);
+
+        /// <summary>
+        /// Gets or sets the underlying disposable.
+        /// </summary>
+        /// <remarks>If the SerialDisposable has already been disposed, assignment to this property causes immediate disposal of the given disposable object. Assigning this property disposes the previous disposable object.</remarks>
+        public IDisposable? Disposable
+        {
+            get => Disposables.Disposable.GetValue(ref _current);
+            set => Disposables.Disposable.TrySetSerial(ref _current, value);
+        }
+
+        /// <summary>
+        /// Disposes the underlying disposable as well as all future replacements.
+        /// </summary>
+        public void Dispose()
+        {
+            Disposables.Disposable.Dispose(ref _current);
+        }
+    }
+}