CheckedObserver.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.Threading;
  5. namespace System.Reactive
  6. {
  7. internal class CheckedObserver<T> : IObserver<T>
  8. {
  9. private readonly IObserver<T> _observer;
  10. private int _state;
  11. private const int IDLE = 0;
  12. private const int BUSY = 1;
  13. private const int DONE = 2;
  14. public CheckedObserver(IObserver<T> observer)
  15. {
  16. _observer = observer;
  17. }
  18. public void OnNext(T value)
  19. {
  20. CheckAccess();
  21. try
  22. {
  23. _observer.OnNext(value);
  24. }
  25. finally
  26. {
  27. Interlocked.Exchange(ref _state, IDLE);
  28. }
  29. }
  30. public void OnError(Exception error)
  31. {
  32. CheckAccess();
  33. try
  34. {
  35. _observer.OnError(error);
  36. }
  37. finally
  38. {
  39. Interlocked.Exchange(ref _state, DONE);
  40. }
  41. }
  42. public void OnCompleted()
  43. {
  44. CheckAccess();
  45. try
  46. {
  47. _observer.OnCompleted();
  48. }
  49. finally
  50. {
  51. Interlocked.Exchange(ref _state, DONE);
  52. }
  53. }
  54. private void CheckAccess()
  55. {
  56. switch (Interlocked.CompareExchange(ref _state, BUSY, IDLE))
  57. {
  58. case BUSY:
  59. throw new InvalidOperationException(Strings_Core.REENTRANCY_DETECTED);
  60. case DONE:
  61. throw new InvalidOperationException(Strings_Core.OBSERVER_TERMINATED);
  62. }
  63. }
  64. }
  65. }