AsyncObserverBase.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace System.Reactive
  7. {
  8. public abstract class AsyncObserverBase<T> : IAsyncObserver<T>
  9. {
  10. private const int Idle = 0;
  11. private const int Busy = 1;
  12. private const int Done = 2;
  13. private int _status = Idle;
  14. public ValueTask OnCompletedAsync()
  15. {
  16. TryEnter();
  17. try
  18. {
  19. return OnCompletedAsyncCore();
  20. }
  21. finally
  22. {
  23. Interlocked.Exchange(ref _status, Done);
  24. }
  25. }
  26. protected abstract ValueTask OnCompletedAsyncCore();
  27. public ValueTask OnErrorAsync(Exception error)
  28. {
  29. if (error == null)
  30. throw new ArgumentNullException(nameof(error));
  31. TryEnter();
  32. try
  33. {
  34. return OnErrorAsyncCore(error);
  35. }
  36. finally
  37. {
  38. Interlocked.Exchange(ref _status, Done);
  39. }
  40. }
  41. protected abstract ValueTask OnErrorAsyncCore(Exception error);
  42. public ValueTask OnNextAsync(T value)
  43. {
  44. TryEnter();
  45. try
  46. {
  47. return OnNextAsyncCore(value);
  48. }
  49. finally
  50. {
  51. Interlocked.Exchange(ref _status, Idle);
  52. }
  53. }
  54. protected abstract ValueTask OnNextAsyncCore(T value);
  55. private void TryEnter()
  56. {
  57. var old = Interlocked.CompareExchange(ref _status, Busy, Idle);
  58. switch (old)
  59. {
  60. case Busy:
  61. throw new InvalidOperationException("The observer is currently processing a notification.");
  62. case Done:
  63. throw new InvalidOperationException("The observer has already terminated.");
  64. }
  65. }
  66. }
  67. }