ToObservable.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 new ArgumentNullException(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. void Core() => e.MoveNextAsync().AsTask().ContinueWith(
  27. async t =>
  28. {
  29. if (t.IsFaulted)
  30. {
  31. observer.OnError(t.Exception);
  32. await e.DisposeAsync().ConfigureAwait(false);
  33. }
  34. else if (t.IsCanceled)
  35. {
  36. await e.DisposeAsync().ConfigureAwait(false);
  37. }
  38. else if (t.IsCompleted)
  39. {
  40. if (t.Result)
  41. {
  42. observer.OnNext(e.Current);
  43. if (!ctd.Token.IsCancellationRequested)
  44. {
  45. Core();
  46. }
  47. // In case cancellation is requested, this could only have happened
  48. // by disposing the returned composite disposable (see below).
  49. // In that case, e will be disposed too, so there is no need to dispose e here.
  50. }
  51. else
  52. {
  53. observer.OnCompleted();
  54. await e.DisposeAsync().ConfigureAwait(false);
  55. }
  56. }
  57. }, ctd.Token);
  58. Core();
  59. return Disposable.Create(ctd, Disposable.Create(() => { e.DisposeAsync(); /* REVIEW: fire-and-forget? */ }));
  60. }
  61. }
  62. }
  63. }