AsyncLockObserver.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Concurrency;
  5. namespace System.Reactive
  6. {
  7. internal sealed class AsyncLockObserver<T> : ObserverBase<T>
  8. {
  9. private readonly AsyncLock _gate;
  10. private readonly IObserver<T> _observer;
  11. public AsyncLockObserver(IObserver<T> observer, AsyncLock gate)
  12. {
  13. _gate = gate;
  14. _observer = observer;
  15. }
  16. protected override void OnNextCore(T value)
  17. {
  18. _gate.Wait(() =>
  19. {
  20. _observer.OnNext(value);
  21. });
  22. }
  23. protected override void OnErrorCore(Exception exception)
  24. {
  25. _gate.Wait(() =>
  26. {
  27. _observer.OnError(exception);
  28. });
  29. }
  30. protected override void OnCompletedCore()
  31. {
  32. _gate.Wait(() =>
  33. {
  34. _observer.OnCompleted();
  35. });
  36. }
  37. }
  38. }