CheckedObserver.cs 1.8 KB

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