ToAsyncEnumerable.Observable.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.Collections.Generic;
  5. using System.Threading.Tasks;
  6. namespace System.Linq
  7. {
  8. public static partial class AsyncEnumerable
  9. {
  10. public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IObservable<TSource> source)
  11. {
  12. if (source == null)
  13. throw Error.ArgumentNull(nameof(source));
  14. return CreateEnumerable(
  15. ct =>
  16. {
  17. var observer = new ToAsyncEnumerableObserver<TSource>();
  18. var subscription = source.Subscribe(observer);
  19. // REVIEW: Review possible concurrency issues with Dispose calls.
  20. var ctr = ct.Register(subscription.Dispose);
  21. return AsyncEnumerator.Create(
  22. tcs =>
  23. {
  24. var hasValue = false;
  25. var hasCompleted = false;
  26. var error = default(Exception);
  27. lock (observer.SyncRoot)
  28. {
  29. if (observer.Values.Count > 0)
  30. {
  31. hasValue = true;
  32. observer.Current = observer.Values.Dequeue();
  33. }
  34. else if (observer.HasCompleted)
  35. {
  36. hasCompleted = true;
  37. }
  38. else if (observer.Error != null)
  39. {
  40. error = observer.Error;
  41. }
  42. else
  43. {
  44. observer.TaskCompletionSource = tcs;
  45. }
  46. }
  47. if (hasValue)
  48. {
  49. tcs.TrySetResult(true);
  50. }
  51. else if (hasCompleted)
  52. {
  53. tcs.TrySetResult(false);
  54. }
  55. else if (error != null)
  56. {
  57. tcs.TrySetException(error);
  58. }
  59. return new ValueTask<bool>(tcs.Task);
  60. },
  61. () => observer.Current,
  62. () =>
  63. {
  64. ctr.Dispose();
  65. subscription.Dispose();
  66. // Should we cancel in-flight operations somehow?
  67. return default;
  68. });
  69. });
  70. }
  71. private sealed class ToAsyncEnumerableObserver<T> : IObserver<T>
  72. {
  73. public readonly Queue<T> Values;
  74. public T Current;
  75. public Exception Error;
  76. public bool HasCompleted;
  77. public TaskCompletionSource<bool> TaskCompletionSource;
  78. public ToAsyncEnumerableObserver()
  79. {
  80. Values = new Queue<T>();
  81. }
  82. public object SyncRoot
  83. {
  84. get { return Values; }
  85. }
  86. public void OnCompleted()
  87. {
  88. var tcs = default(TaskCompletionSource<bool>);
  89. lock (SyncRoot)
  90. {
  91. HasCompleted = true;
  92. if (TaskCompletionSource != null)
  93. {
  94. tcs = TaskCompletionSource;
  95. TaskCompletionSource = null;
  96. }
  97. }
  98. tcs?.TrySetResult(false);
  99. }
  100. public void OnError(Exception error)
  101. {
  102. var tcs = default(TaskCompletionSource<bool>);
  103. lock (SyncRoot)
  104. {
  105. Error = error;
  106. if (TaskCompletionSource != null)
  107. {
  108. tcs = TaskCompletionSource;
  109. TaskCompletionSource = null;
  110. }
  111. }
  112. tcs?.TrySetException(error);
  113. }
  114. public void OnNext(T value)
  115. {
  116. var tcs = default(TaskCompletionSource<bool>);
  117. lock (SyncRoot)
  118. {
  119. if (TaskCompletionSource == null)
  120. {
  121. Values.Enqueue(value);
  122. }
  123. else
  124. {
  125. Current = value;
  126. tcs = TaskCompletionSource;
  127. TaskCompletionSource = null;
  128. }
  129. }
  130. tcs?.TrySetResult(true);
  131. }
  132. }
  133. }
  134. }