When.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. using System.Reactive.Disposables;
  6. using System.Reactive.Joins;
  7. using System.Threading;
  8. namespace System.Reactive.Linq
  9. {
  10. partial class AsyncObservable
  11. {
  12. public static IAsyncObservable<TResult> When<TResult>(IEnumerable<AsyncPlan<TResult>> plans)
  13. {
  14. if (plans == null)
  15. throw new ArgumentNullException(nameof(plans));
  16. return Create<TResult>(async observer =>
  17. {
  18. var externalSubscriptions = new Dictionary<object, IAsyncJoinObserver>();
  19. var gate = new AsyncLock();
  20. var activePlans = new List<ActiveAsyncPlan>();
  21. var outputObserver = AsyncObserver.Create<TResult>(
  22. observer.OnNextAsync,
  23. async ex =>
  24. {
  25. foreach (var subscription in externalSubscriptions.Values)
  26. {
  27. await subscription.DisposeAsync().ConfigureAwait(false);
  28. }
  29. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  30. },
  31. observer.OnCompletedAsync
  32. );
  33. try
  34. {
  35. foreach (var plan in plans)
  36. {
  37. var activatedPlan = plan.Activate(externalSubscriptions, outputObserver, async activePlan =>
  38. {
  39. activePlans.Remove(activePlan);
  40. if (activePlans.Count == 0)
  41. {
  42. await outputObserver.OnCompletedAsync().ConfigureAwait(false);
  43. }
  44. });
  45. activePlans.Add(activatedPlan);
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. return await Throw<TResult>(ex).SubscribeAsync(observer).ConfigureAwait(false);
  51. }
  52. var d = new CompositeAsyncDisposable();
  53. foreach (var joinObserver in externalSubscriptions.Values)
  54. {
  55. await joinObserver.SubscribeAsync(gate).ConfigureAwait(false);
  56. await d.AddAsync(joinObserver).ConfigureAwait(false);
  57. }
  58. return d;
  59. });
  60. }
  61. public static IAsyncObservable<TResult> When<TResult>(params AsyncPlan<TResult>[] plans) => When((IEnumerable<AsyncPlan<TResult>>)plans);
  62. }
  63. }