Latest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.Linq.ObservableImpl
  6. {
  7. internal sealed class Latest<TSource> : PushToPullAdapter<TSource, TSource>
  8. {
  9. public Latest(IObservable<TSource> source)
  10. : base(source)
  11. {
  12. }
  13. protected override PushToPullSink<TSource, TSource> Run()
  14. {
  15. return new _();
  16. }
  17. private sealed class _ : PushToPullSink<TSource, TSource>
  18. {
  19. private readonly object _gate;
  20. private readonly SemaphoreSlim _semaphore;
  21. public _()
  22. {
  23. _gate = new object();
  24. _semaphore = new SemaphoreSlim(0, 1);
  25. }
  26. private bool _notificationAvailable;
  27. private NotificationKind _kind;
  28. private TSource _value;
  29. private Exception _error;
  30. public override void OnNext(TSource value)
  31. {
  32. var lackedValue = false;
  33. lock (_gate)
  34. {
  35. lackedValue = !_notificationAvailable;
  36. _notificationAvailable = true;
  37. _kind = NotificationKind.OnNext;
  38. _value = value;
  39. }
  40. if (lackedValue)
  41. {
  42. _semaphore.Release();
  43. }
  44. }
  45. public override void OnError(Exception error)
  46. {
  47. Dispose();
  48. var lackedValue = false;
  49. lock (_gate)
  50. {
  51. lackedValue = !_notificationAvailable;
  52. _notificationAvailable = true;
  53. _kind = NotificationKind.OnError;
  54. _error = error;
  55. }
  56. if (lackedValue)
  57. {
  58. _semaphore.Release();
  59. }
  60. }
  61. public override void OnCompleted()
  62. {
  63. Dispose();
  64. var lackedValue = false;
  65. lock (_gate)
  66. {
  67. lackedValue = !_notificationAvailable;
  68. _notificationAvailable = true;
  69. _kind = NotificationKind.OnCompleted;
  70. }
  71. if (lackedValue)
  72. {
  73. _semaphore.Release();
  74. }
  75. }
  76. public override bool TryMoveNext(out TSource current)
  77. {
  78. var kind = default(NotificationKind);
  79. var value = default(TSource);
  80. var error = default(Exception);
  81. _semaphore.Wait();
  82. lock (_gate)
  83. {
  84. kind = _kind;
  85. switch (kind)
  86. {
  87. case NotificationKind.OnNext:
  88. value = _value;
  89. break;
  90. case NotificationKind.OnError:
  91. error = _error;
  92. break;
  93. }
  94. _notificationAvailable = false;
  95. }
  96. switch (kind)
  97. {
  98. case NotificationKind.OnNext:
  99. current = value;
  100. return true;
  101. case NotificationKind.OnError:
  102. error.Throw();
  103. break;
  104. case NotificationKind.OnCompleted:
  105. break;
  106. }
  107. current = default;
  108. return false;
  109. }
  110. }
  111. }
  112. }