Scan.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.Threading.Tasks;
  5. namespace System.Reactive.Linq
  6. {
  7. partial class AsyncObservable
  8. {
  9. public static IAsyncObservable<TSource> Scan<TSource>(this IAsyncObservable<TSource> source, Func<TSource, TSource, TSource> func)
  10. {
  11. if (source == null)
  12. throw new ArgumentNullException(nameof(source));
  13. if (func == null)
  14. throw new ArgumentNullException(nameof(func));
  15. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Scan(observer, func)));
  16. }
  17. public static IAsyncObservable<TSource> Scan<TSource>(this IAsyncObservable<TSource> source, Func<TSource, TSource, Task<TSource>> func)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. if (func == null)
  22. throw new ArgumentNullException(nameof(func));
  23. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Scan(observer, func)));
  24. }
  25. public static IAsyncObservable<TResult> Scan<TSource, TResult>(this IAsyncObservable<TSource> source, TResult seed, Func<TResult, TSource, TResult> func)
  26. {
  27. if (source == null)
  28. throw new ArgumentNullException(nameof(source));
  29. if (func == null)
  30. throw new ArgumentNullException(nameof(func));
  31. return Create<TResult>(observer => source.SubscribeSafeAsync(AsyncObserver.Scan(observer, seed, func)));
  32. }
  33. public static IAsyncObservable<TResult> Scan<TSource, TResult>(this IAsyncObservable<TSource> source, TResult seed, Func<TResult, TSource, Task<TResult>> func)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. if (func == null)
  38. throw new ArgumentNullException(nameof(func));
  39. return Create<TResult>(observer => source.SubscribeSafeAsync(AsyncObserver.Scan(observer, seed, func)));
  40. }
  41. }
  42. partial class AsyncObserver
  43. {
  44. public static IAsyncObserver<TSource> Scan<TSource>(IAsyncObserver<TSource> observer, Func<TSource, TSource, TSource> func)
  45. {
  46. if (observer == null)
  47. throw new ArgumentNullException(nameof(observer));
  48. if (func == null)
  49. throw new ArgumentNullException(nameof(func));
  50. return Scan(observer, (a, x) => Task.FromResult(func(a, x)));
  51. }
  52. public static IAsyncObserver<TSource> Scan<TSource>(IAsyncObserver<TSource> observer, Func<TSource, TSource, Task<TSource>> func)
  53. {
  54. if (observer == null)
  55. throw new ArgumentNullException(nameof(observer));
  56. if (func == null)
  57. throw new ArgumentNullException(nameof(func));
  58. var hasValue = false;
  59. var value = default(TSource);
  60. return Create<TSource>(
  61. async x =>
  62. {
  63. if (hasValue)
  64. {
  65. try
  66. {
  67. value = await func(value, x).ConfigureAwait(false);
  68. }
  69. catch (Exception ex)
  70. {
  71. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  72. return;
  73. }
  74. }
  75. else
  76. {
  77. value = x;
  78. hasValue = true;
  79. }
  80. await observer.OnNextAsync(value).ConfigureAwait(false);
  81. },
  82. observer.OnErrorAsync,
  83. observer.OnCompletedAsync
  84. );
  85. }
  86. public static IAsyncObserver<TSource> Scan<TSource, TResult>(IAsyncObserver<TResult> observer, TResult seed, Func<TResult, TSource, TResult> func)
  87. {
  88. if (observer == null)
  89. throw new ArgumentNullException(nameof(observer));
  90. if (func == null)
  91. throw new ArgumentNullException(nameof(func));
  92. return Scan<TSource, TResult>(observer, seed, (a, x) => Task.FromResult(func(a, x)));
  93. }
  94. public static IAsyncObserver<TSource> Scan<TSource, TResult>(IAsyncObserver<TResult> observer, TResult seed, Func<TResult, TSource, Task<TResult>> func)
  95. {
  96. if (observer == null)
  97. throw new ArgumentNullException(nameof(observer));
  98. if (func == null)
  99. throw new ArgumentNullException(nameof(func));
  100. var value = seed;
  101. return Create<TSource>(
  102. async x =>
  103. {
  104. try
  105. {
  106. value = await func(value, x).ConfigureAwait(false);
  107. }
  108. catch (Exception ex)
  109. {
  110. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  111. return;
  112. }
  113. await observer.OnNextAsync(value).ConfigureAwait(false);
  114. },
  115. observer.OnErrorAsync,
  116. observer.OnCompletedAsync
  117. );
  118. }
  119. }
  120. }