SynchronizedObserver.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. namespace System.Reactive
  5. {
  6. internal class SynchronizedObserver<T> : ObserverBase<T>
  7. {
  8. private readonly object _gate;
  9. private readonly IObserver<T> _observer;
  10. public SynchronizedObserver(IObserver<T> observer, object gate)
  11. {
  12. _gate = gate;
  13. _observer = observer;
  14. }
  15. protected override void OnNextCore(T value)
  16. {
  17. lock (_gate)
  18. {
  19. _observer.OnNext(value);
  20. }
  21. }
  22. protected override void OnErrorCore(Exception exception)
  23. {
  24. lock (_gate)
  25. {
  26. _observer.OnError(exception);
  27. }
  28. }
  29. protected override void OnCompletedCore()
  30. {
  31. lock (_gate)
  32. {
  33. _observer.OnCompleted();
  34. }
  35. }
  36. }
  37. }