ToObservable.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. namespace System.Linq
  6. {
  7. public static partial class AsyncEnumerable
  8. {
  9. public static IObservable<TSource> ToObservable<TSource>(this IAsyncEnumerable<TSource> source)
  10. {
  11. if (source == null)
  12. throw Error.ArgumentNull(nameof(source));
  13. return new ToObservableObservable<TSource>(source);
  14. }
  15. private sealed class ToObservableObservable<T> : IObservable<T>
  16. {
  17. private readonly IAsyncEnumerable<T> _source;
  18. public ToObservableObservable(IAsyncEnumerable<T> source)
  19. {
  20. _source = source;
  21. }
  22. public IDisposable Subscribe(IObserver<T> observer)
  23. {
  24. var ctd = new CancellationTokenDisposable();
  25. var e = _source.GetAsyncEnumerator(ctd.Token);
  26. async void Core()
  27. {
  28. bool hasNext;
  29. try
  30. {
  31. hasNext = await e.MoveNextAsync().ConfigureAwait(false);
  32. }
  33. catch (Exception ex)
  34. {
  35. if (!ctd.Token.IsCancellationRequested)
  36. {
  37. observer.OnError(ex);
  38. await e.DisposeAsync().ConfigureAwait(false);
  39. }
  40. return;
  41. }
  42. if (hasNext)
  43. {
  44. observer.OnNext(e.Current);
  45. if (!ctd.Token.IsCancellationRequested)
  46. {
  47. Core();
  48. }
  49. // In case cancellation is requested, this could only have happened
  50. // by disposing the returned composite disposable (see below).
  51. // In that case, e will be disposed too, so there is no need to dispose e here.
  52. }
  53. else
  54. {
  55. observer.OnCompleted();
  56. await e.DisposeAsync().ConfigureAwait(false);
  57. }
  58. }
  59. Core();
  60. // REVIEW: Safety of concurrent dispose operation; fire-and-forget nature of dispose?
  61. return Disposable.Create(ctd, Disposable.Create(() => { e.DisposeAsync(); }));
  62. }
  63. }
  64. }
  65. }