Browse Source

Enable reuse of UserWorkItem class currently nested in DefaultScheduler.

Daniel C. Weber 7 years ago
parent
commit
dfde17ab5e

+ 0 - 37
Rx.NET/Source/src/System.Reactive/Concurrency/DefaultScheduler.cs

@@ -12,43 +12,6 @@ namespace System.Reactive.Concurrency
     /// <seealso cref="Scheduler.Default">Singleton instance of this type exposed through this static property.</seealso>
     public sealed class DefaultScheduler : LocalScheduler, ISchedulerPeriodic
     {
-        private sealed class UserWorkItem<TState> : IDisposable
-        {
-            private IDisposable _cancelRunDisposable;
-            private IDisposable _cancelQueueDisposable;
-
-            private readonly TState _state;
-            private readonly IScheduler _scheduler;
-            private readonly Func<IScheduler, TState, IDisposable> _action;
-
-            public UserWorkItem(IScheduler scheduler, TState state, Func<IScheduler, TState, IDisposable> action)
-            {
-                _state = state;
-                _action = action;
-                _scheduler = scheduler;
-            }
-
-            public void Run()
-            {
-                if (!Disposable.GetIsDisposed(ref _cancelRunDisposable))
-                {
-                    Disposable.SetSingle(ref _cancelRunDisposable, _action(_scheduler, _state));
-                }
-            }
-
-            public IDisposable CancelQueueDisposable
-            {
-                get => Disposable.GetValue(ref _cancelQueueDisposable);
-                set => Disposable.SetSingle(ref _cancelQueueDisposable, value);
-            }
-
-            public void Dispose()
-            {
-                Disposable.TryDispose(ref _cancelQueueDisposable);
-                Disposable.TryDispose(ref _cancelRunDisposable);
-            }
-        }
-
         private static readonly Lazy<DefaultScheduler> s_instance = new Lazy<DefaultScheduler>(() => new DefaultScheduler());
         private static IConcurrencyAbstractionLayer s_cal = ConcurrencyAbstractionLayer.Current;
 

+ 0 - 29
Rx.NET/Source/src/System.Reactive/Concurrency/ThreadPoolScheduler.cs

@@ -15,35 +15,6 @@ namespace System.Reactive.Concurrency
     /// <seealso cref="ThreadPoolScheduler.Instance">Singleton instance of this type exposed through this static property.</seealso>
     public sealed class ThreadPoolScheduler : LocalScheduler, ISchedulerLongRunning, ISchedulerPeriodic
     {
-        private sealed class UserWorkItem<TState> : IDisposable
-        {
-            private IDisposable _cancelRunDisposable;
-
-            private readonly TState _state;
-            private readonly IScheduler _scheduler;
-            private readonly Func<IScheduler, TState, IDisposable> _action;
-
-            public UserWorkItem(IScheduler scheduler, TState state, Func<IScheduler, TState, IDisposable> action)
-            {
-                _state = state;
-                _action = action;
-                _scheduler = scheduler;
-            }
-
-            public void Run()
-            {
-                if (!Disposable.GetIsDisposed(ref _cancelRunDisposable))
-                {
-                    Disposable.SetSingle(ref _cancelRunDisposable, _action(_scheduler, _state));
-                }
-            }
-
-            public void Dispose()
-            {
-                Disposable.TryDispose(ref _cancelRunDisposable);
-            }
-        }
-
         private static readonly Lazy<ThreadPoolScheduler> s_instance = new Lazy<ThreadPoolScheduler>(() => new ThreadPoolScheduler());
         private static readonly Lazy<NewThreadScheduler> s_newBackgroundThread = new Lazy<NewThreadScheduler>(() => new NewThreadScheduler(action => new Thread(action) { IsBackground = true }));
 

+ 45 - 0
Rx.NET/Source/src/System.Reactive/Concurrency/UserWorkItem.cs

@@ -0,0 +1,45 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information.
+
+using System.Reactive.Disposables;
+
+namespace System.Reactive.Concurrency
+{
+    internal sealed class UserWorkItem<TState> : IDisposable
+    {
+        private IDisposable _cancelRunDisposable;
+        private IDisposable _cancelQueueDisposable;
+
+        private readonly TState _state;
+        private readonly IScheduler _scheduler;
+        private readonly Func<IScheduler, TState, IDisposable> _action;
+
+        public UserWorkItem(IScheduler scheduler, TState state, Func<IScheduler, TState, IDisposable> action)
+        {
+            _state = state;
+            _action = action;
+            _scheduler = scheduler;
+        }
+
+        public void Run()
+        {
+            if (!Disposable.GetIsDisposed(ref _cancelRunDisposable))
+            {
+                Disposable.SetSingle(ref _cancelRunDisposable, _action(_scheduler, _state));
+            }
+        }
+
+        public IDisposable CancelQueueDisposable
+        {
+            get => Disposable.GetValue(ref _cancelQueueDisposable);
+            set => Disposable.SetSingle(ref _cancelQueueDisposable, value);
+        }
+
+        public void Dispose()
+        {
+            Disposable.TryDispose(ref _cancelQueueDisposable);
+            Disposable.TryDispose(ref _cancelRunDisposable);
+        }
+    }
+}