If.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 Create(
  21. thenSource,
  22. (elseSource, condition),
  23. default(TResult),
  24. (thenSource, state, observer) =>
  25. {
  26. var b = default(bool);
  27. try
  28. {
  29. b = state.condition();
  30. }
  31. catch (Exception ex)
  32. {
  33. return Throw<TResult>(ex).SubscribeAsync(observer);
  34. }
  35. return (b ? thenSource : state.elseSource).SubscribeSafeAsync(observer);
  36. });
  37. }
  38. public static IAsyncObservable<TResult> If<TResult>(Func<ValueTask<bool>> condition, IAsyncObservable<TResult> thenSource) => If(condition, thenSource, Empty<TResult>());
  39. public static IAsyncObservable<TResult> If<TResult>(Func<ValueTask<bool>> condition, IAsyncObservable<TResult> thenSource, IAsyncScheduler scheduler) => If(condition, thenSource, Empty<TResult>(scheduler));
  40. public static IAsyncObservable<TResult> If<TResult>(Func<ValueTask<bool>> condition, IAsyncObservable<TResult> thenSource, IAsyncObservable<TResult> elseSource)
  41. {
  42. if (condition == null)
  43. throw new ArgumentNullException(nameof(condition));
  44. if (thenSource == null)
  45. throw new ArgumentNullException(nameof(thenSource));
  46. if (elseSource == null)
  47. throw new ArgumentNullException(nameof(elseSource));
  48. return Create(
  49. thenSource,
  50. (elseSource, condition),
  51. default(TResult),
  52. async (thenSource, state, observer) =>
  53. {
  54. var b = default(bool);
  55. try
  56. {
  57. b = await state.condition().ConfigureAwait(false);
  58. }
  59. catch (Exception ex)
  60. {
  61. return await Throw<TResult>(ex).SubscribeAsync(observer).ConfigureAwait(false);
  62. }
  63. return await (b ? thenSource : state.elseSource).SubscribeSafeAsync(observer).ConfigureAwait(false);
  64. });
  65. }
  66. }
  67. }