ToObservable.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class AsyncEnumerable
  8. {
  9. #if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
  10. // Moved to AsyncEnumerableEx in System.Interactive.Async.
  11. // System.Linq.AsyncEnumerable has chosen not to implement this. We continue to implement this because
  12. // we believe it is a useful feature, but since it's now in the category of LINQ-adjacent functionality
  13. // not built into the .NET runtime libraries, it now lives in System.Interactive.Async.
  14. /// <summary>
  15. /// Converts an async-enumerable sequence to an observable sequence.
  16. /// </summary>
  17. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  18. /// <param name="source">Enumerable sequence to convert to an observable sequence.</param>
  19. /// <returns>The observable sequence whose elements are pulled from the given enumerable sequence.</returns>
  20. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  21. public static IObservable<TSource> ToObservable<TSource>(this IAsyncEnumerable<TSource> source)
  22. {
  23. if (source == null)
  24. throw Error.ArgumentNull(nameof(source));
  25. return new ToObservableObservable<TSource>(source);
  26. }
  27. private sealed class ToObservableObservable<T> : IObservable<T>
  28. {
  29. private readonly IAsyncEnumerable<T> _source;
  30. public ToObservableObservable(IAsyncEnumerable<T> source)
  31. {
  32. _source = source;
  33. }
  34. public IDisposable Subscribe(IObserver<T> observer)
  35. {
  36. var ctd = new CancellationTokenDisposable();
  37. async void Core()
  38. {
  39. await using var e = _source.GetAsyncEnumerator(ctd.Token);
  40. do
  41. {
  42. bool hasNext;
  43. var value = default(T)!;
  44. try
  45. {
  46. hasNext = await e.MoveNextAsync().ConfigureAwait(false);
  47. if (hasNext)
  48. {
  49. value = e.Current;
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. if (!ctd.Token.IsCancellationRequested)
  55. {
  56. observer.OnError(ex);
  57. }
  58. return;
  59. }
  60. if (!hasNext)
  61. {
  62. observer.OnCompleted();
  63. return;
  64. }
  65. observer.OnNext(value);
  66. }
  67. while (!ctd.Token.IsCancellationRequested);
  68. }
  69. // Fire and forget
  70. Core();
  71. return ctd;
  72. }
  73. }
  74. #endif // INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
  75. }
  76. }