ForEachAsync.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.Reactive.Disposables;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Reactive.Linq
  8. {
  9. public partial class AsyncObservable
  10. {
  11. public static Task ForEachAsync<TSource>(this IAsyncObservable<TSource> source, Action<TSource> onNext, CancellationToken token = default)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. if (onNext == null)
  16. throw new ArgumentNullException(nameof(onNext));
  17. return ForEachAsyncCore(source, (x, i) => { onNext(x); return Task.CompletedTask; }, token);
  18. }
  19. public static Task ForEachAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, Task> onNext, CancellationToken token = default)
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (onNext == null)
  24. throw new ArgumentNullException(nameof(onNext));
  25. return ForEachAsyncCore(source, (x, i) => onNext(x), token);
  26. }
  27. public static Task ForEachAsync<TSource>(this IAsyncObservable<TSource> source, Action<TSource, int> onNext, CancellationToken token = default)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (onNext == null)
  32. throw new ArgumentNullException(nameof(onNext));
  33. return ForEachAsyncCore(source, (x, i) => { onNext(x, i); return Task.CompletedTask; }, token);
  34. }
  35. public static Task ForEachAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, int, Task> onNext, CancellationToken token = default)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. if (onNext == null)
  40. throw new ArgumentNullException(nameof(onNext));
  41. return ForEachAsyncCore(source, onNext, token);
  42. }
  43. private static async Task ForEachAsyncCore<TSource>(IAsyncObservable<TSource> source, Func<TSource, int, Task> onNext, CancellationToken token)
  44. {
  45. token.ThrowIfCancellationRequested();
  46. var tcs = new TaskCompletionSource<object>();
  47. var subscription = new SingleAssignmentAsyncDisposable();
  48. using (token.Register(() =>
  49. {
  50. tcs.TrySetCanceled(token);
  51. subscription.DisposeAsync().AsTask().ContinueWith(t =>
  52. {
  53. if (t.Exception != null)
  54. {
  55. // TODO: Trace?
  56. }
  57. });
  58. }))
  59. {
  60. var i = 0;
  61. var o = AsyncObserver.Create<TSource>(
  62. async x =>
  63. {
  64. try
  65. {
  66. await onNext(x, checked(i++)).ConfigureAwait(false);
  67. }
  68. catch (Exception ex)
  69. {
  70. try
  71. {
  72. tcs.TrySetException(ex);
  73. }
  74. finally
  75. {
  76. await subscription.DisposeAsync().ConfigureAwait(false);
  77. }
  78. }
  79. },
  80. async ex =>
  81. {
  82. try
  83. {
  84. tcs.TrySetException(ex);
  85. }
  86. finally
  87. {
  88. await subscription.DisposeAsync().ConfigureAwait(false);
  89. }
  90. },
  91. async () =>
  92. {
  93. try
  94. {
  95. tcs.TrySetResult(null);
  96. }
  97. finally
  98. {
  99. await subscription.DisposeAsync().ConfigureAwait(false);
  100. }
  101. }
  102. );
  103. //
  104. // NB: If any of the lines below throw, the result will go into the Task returned from the async method.
  105. // There's also no need to use SubscribeSafeAsync here; the exception will propagate just fine.
  106. //
  107. var d = await source.SubscribeAsync(o).ConfigureAwait(false);
  108. await subscription.AssignAsync(d).ConfigureAwait(false);
  109. }
  110. await tcs.Task.ConfigureAwait(false);
  111. }
  112. }
  113. }