1
0

If.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.Concurrency;
  5. using System.Threading.Tasks;
  6. namespace System.Reactive.Linq
  7. {
  8. public partial class AsyncObservable
  9. {
  10. public static IAsyncObservable<TResult> If<TResult>(Func<bool> condition, IAsyncObservable<TResult> thenSource) => If(condition, thenSource, Empty<TResult>());
  11. public static IAsyncObservable<TResult> If<TResult>(Func<bool> condition, IAsyncObservable<TResult> thenSource, IAsyncScheduler scheduler) => If(condition, thenSource, Empty<TResult>(scheduler));
  12. public static IAsyncObservable<TResult> If<TResult>(Func<bool> condition, IAsyncObservable<TResult> thenSource, IAsyncObservable<TResult> elseSource)
  13. {
  14. if (condition == null)
  15. throw new ArgumentNullException(nameof(condition));
  16. if (thenSource == null)
  17. throw new ArgumentNullException(nameof(thenSource));
  18. if (elseSource == null)
  19. throw new ArgumentNullException(nameof(elseSource));
  20. return CreateAsyncObservable<TResult>.From(
  21. thenSource,
  22. (elseSource, condition),
  23. static (thenSource, state, observer) =>
  24. {
  25. var b = default(bool);
  26. try
  27. {
  28. b = state.condition();
  29. }
  30. catch (Exception ex)
  31. {
  32. return Throw<TResult>(ex).SubscribeAsync(observer);
  33. }
  34. return (b ? thenSource : state.elseSource).SubscribeSafeAsync(observer);
  35. });
  36. }
  37. public static IAsyncObservable<TResult> If<TResult>(Func<ValueTask<bool>> condition, IAsyncObservable<TResult> thenSource) => If(condition, thenSource, Empty<TResult>());
  38. public static IAsyncObservable<TResult> If<TResult>(Func<ValueTask<bool>> condition, IAsyncObservable<TResult> thenSource, IAsyncScheduler scheduler) => If(condition, thenSource, Empty<TResult>(scheduler));
  39. public static IAsyncObservable<TResult> If<TResult>(Func<ValueTask<bool>> condition, IAsyncObservable<TResult> thenSource, IAsyncObservable<TResult> elseSource)
  40. {
  41. if (condition == null)
  42. throw new ArgumentNullException(nameof(condition));
  43. if (thenSource == null)
  44. throw new ArgumentNullException(nameof(thenSource));
  45. if (elseSource == null)
  46. throw new ArgumentNullException(nameof(elseSource));
  47. return CreateAsyncObservable<TResult>.From(
  48. thenSource,
  49. (elseSource, condition),
  50. static async (thenSource, state, observer) =>
  51. {
  52. var b = default(bool);
  53. try
  54. {
  55. b = await state.condition().ConfigureAwait(false);
  56. }
  57. catch (Exception ex)
  58. {
  59. return await Throw<TResult>(ex).SubscribeAsync(observer).ConfigureAwait(false);
  60. }
  61. return await (b ? thenSource : state.elseSource).SubscribeSafeAsync(observer).ConfigureAwait(false);
  62. });
  63. }
  64. }
  65. }