Zip.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.Linq;
  6. using System.Reactive.Disposables;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Reactive.Linq
  10. {
  11. partial class AsyncObservable
  12. {
  13. // TODO: Add Zip<T>(IAsyncObservable<T>, IAsyncEnumerable<T>) overload when we have reference to IAsyncEnumerable<T>.
  14. public static IAsyncObservable<IList<TSource>> Zip<TSource>(IEnumerable<IAsyncObservable<TSource>> sources) => Zip(sources.ToArray());
  15. public static IAsyncObservable<IList<TSource>> Zip<TSource>(params IAsyncObservable<TSource>[] sources)
  16. {
  17. if (sources == null)
  18. throw new ArgumentNullException(nameof(sources));
  19. return Create<IList<TSource>>(async observer =>
  20. {
  21. var count = sources.Length;
  22. var observers = AsyncObserver.Zip(observer, count);
  23. var tasks = new Task<IAsyncDisposable>[count];
  24. for (var i = 0; i < count; i++)
  25. {
  26. tasks[i] = sources[i].SubscribeSafeAsync(observers[i]);
  27. }
  28. await Task.WhenAll(tasks).ConfigureAwait(false);
  29. return StableCompositeAsyncDisposable.Create(tasks.Select(t => t.Result));
  30. });
  31. }
  32. }
  33. partial class AsyncObserver
  34. {
  35. public static IAsyncObserver<TSource>[] Zip<TSource>(IAsyncObserver<IList<TSource>> observer, int count)
  36. {
  37. if (observer == null)
  38. throw new ArgumentNullException(nameof(observer));
  39. if (count < 0)
  40. throw new ArgumentOutOfRangeException(nameof(count));
  41. var gate = new AsyncLock();
  42. var queues = new Queue<TSource>[count];
  43. var isDone = new bool[count];
  44. var res = new IAsyncObserver<TSource>[count];
  45. IAsyncObserver<TSource> CreateObserver(int index) =>
  46. Create<TSource>(
  47. async x =>
  48. {
  49. using (await gate.LockAsync().ConfigureAwait(false))
  50. {
  51. queues[index].Enqueue(x);
  52. if (queues.All(queue => queue.Count > 0))
  53. {
  54. var list = new TSource[count];
  55. for (var i = 0; i < count; i++)
  56. {
  57. list[i] = queues[i].Dequeue();
  58. }
  59. await observer.OnNextAsync(list).ConfigureAwait(false);
  60. }
  61. else
  62. {
  63. var allDone = true;
  64. for (var i = 0; i < count; i++)
  65. {
  66. if (i != index && !isDone[i])
  67. {
  68. allDone = false;
  69. break;
  70. }
  71. }
  72. if (allDone)
  73. {
  74. await observer.OnCompletedAsync().ConfigureAwait(false);
  75. }
  76. }
  77. }
  78. },
  79. async ex =>
  80. {
  81. using (await gate.LockAsync().ConfigureAwait(false))
  82. {
  83. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  84. }
  85. },
  86. async () =>
  87. {
  88. using (await gate.LockAsync().ConfigureAwait(false))
  89. {
  90. isDone[index] = true;
  91. var allDone = true;
  92. for (var i = 0; i < count; i++)
  93. {
  94. if (!isDone[i])
  95. {
  96. allDone = false;
  97. break;
  98. }
  99. }
  100. if (allDone)
  101. {
  102. await observer.OnCompletedAsync().ConfigureAwait(false);
  103. }
  104. }
  105. }
  106. );
  107. for (var i = 0; i < count; i++)
  108. {
  109. queues[i] = new Queue<TSource>();
  110. res[i] = CreateObserver(i);
  111. }
  112. return res;
  113. }
  114. }
  115. }