فهرست منبع

Adding AsyncLockObserver<T>.

Bart De Smet 8 سال پیش
والد
کامیت
3fce4343d8
1فایلهای تغییر یافته به همراه26 افزوده شده و 0 حذف شده
  1. 26 0
      AsyncRx.NET/System.Reactive.Async.Linq/System/Reactive/AsyncLockObserver.cs

+ 26 - 0
AsyncRx.NET/System.Reactive.Async.Linq/System/Reactive/AsyncLockObserver.cs

@@ -0,0 +1,26 @@
+// 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.Threading;
+using System.Threading.Tasks;
+
+namespace System.Reactive
+{
+    internal sealed class AsyncLockObserver<T> : AsyncObserverBase<T>
+    {
+        private readonly AsyncQueueLock _gate = new AsyncQueueLock();
+        private readonly IAsyncObserver<T> _observer;
+
+        public AsyncLockObserver(IAsyncObserver<T> observer)
+        {
+            _observer = observer;
+        }
+
+        protected override Task OnCompletedAsyncCore() => _gate.WaitAsync(_observer.OnCompletedAsync);
+
+        protected override Task OnErrorAsyncCore(Exception error) => _gate.WaitAsync(() => _observer.OnErrorAsync(error));
+
+        protected override Task OnNextAsyncCore(T value) => _gate.WaitAsync(() => _observer.OnNextAsync(value));
+    }
+}