Observable.Concatenate.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Linq;
  5. using UniRx.Operators;
  6. namespace UniRx
  7. {
  8. // concatenate multiple observable
  9. // merge, concat, zip...
  10. public static partial class Observable
  11. {
  12. static IEnumerable<IObservable<T>> CombineSources<T>(IObservable<T> first, IObservable<T>[] seconds)
  13. {
  14. yield return first;
  15. for (int i = 0; i < seconds.Length; i++)
  16. {
  17. yield return seconds[i];
  18. }
  19. }
  20. public static IObservable<TSource> Concat<TSource>(params IObservable<TSource>[] sources)
  21. {
  22. if (sources == null) throw new ArgumentNullException("sources");
  23. return new ConcatObservable<TSource>(sources);
  24. }
  25. public static IObservable<TSource> Concat<TSource>(this IEnumerable<IObservable<TSource>> sources)
  26. {
  27. if (sources == null) throw new ArgumentNullException("sources");
  28. return new ConcatObservable<TSource>(sources);
  29. }
  30. public static IObservable<TSource> Concat<TSource>(this IObservable<IObservable<TSource>> sources)
  31. {
  32. return sources.Merge(maxConcurrent: 1);
  33. }
  34. public static IObservable<TSource> Concat<TSource>(this IObservable<TSource> first, params IObservable<TSource>[] seconds)
  35. {
  36. if (first == null) throw new ArgumentNullException("first");
  37. if (seconds == null) throw new ArgumentNullException("seconds");
  38. var concat = first as ConcatObservable<TSource>;
  39. if (concat != null)
  40. {
  41. return concat.Combine(seconds);
  42. }
  43. return Concat(CombineSources(first, seconds));
  44. }
  45. public static IObservable<TSource> Merge<TSource>(this IEnumerable<IObservable<TSource>> sources)
  46. {
  47. return Merge(sources, Scheduler.DefaultSchedulers.ConstantTimeOperations);
  48. }
  49. public static IObservable<TSource> Merge<TSource>(this IEnumerable<IObservable<TSource>> sources, IScheduler scheduler)
  50. {
  51. return new MergeObservable<TSource>(sources.ToObservable(scheduler), scheduler == Scheduler.CurrentThread);
  52. }
  53. public static IObservable<TSource> Merge<TSource>(this IEnumerable<IObservable<TSource>> sources, int maxConcurrent)
  54. {
  55. return Merge(sources, maxConcurrent, Scheduler.DefaultSchedulers.ConstantTimeOperations);
  56. }
  57. public static IObservable<TSource> Merge<TSource>(this IEnumerable<IObservable<TSource>> sources, int maxConcurrent, IScheduler scheduler)
  58. {
  59. return new MergeObservable<TSource>(sources.ToObservable(scheduler), maxConcurrent, scheduler == Scheduler.CurrentThread);
  60. }
  61. public static IObservable<TSource> Merge<TSource>(params IObservable<TSource>[] sources)
  62. {
  63. return Merge(Scheduler.DefaultSchedulers.ConstantTimeOperations, sources);
  64. }
  65. public static IObservable<TSource> Merge<TSource>(IScheduler scheduler, params IObservable<TSource>[] sources)
  66. {
  67. return new MergeObservable<TSource>(sources.ToObservable(scheduler), scheduler == Scheduler.CurrentThread);
  68. }
  69. public static IObservable<T> Merge<T>(this IObservable<T> first, params IObservable<T>[] seconds)
  70. {
  71. return Merge(CombineSources(first, seconds));
  72. }
  73. public static IObservable<T> Merge<T>(this IObservable<T> first, IObservable<T> second, IScheduler scheduler)
  74. {
  75. return Merge(scheduler, new[] { first, second });
  76. }
  77. public static IObservable<T> Merge<T>(this IObservable<IObservable<T>> sources)
  78. {
  79. return new MergeObservable<T>(sources, false);
  80. }
  81. public static IObservable<T> Merge<T>(this IObservable<IObservable<T>> sources, int maxConcurrent)
  82. {
  83. return new MergeObservable<T>(sources, maxConcurrent, false);
  84. }
  85. public static IObservable<TResult> Zip<TLeft, TRight, TResult>(this IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, TRight, TResult> selector)
  86. {
  87. return new ZipObservable<TLeft, TRight, TResult>(left, right, selector);
  88. }
  89. public static IObservable<IList<T>> Zip<T>(this IEnumerable<IObservable<T>> sources)
  90. {
  91. return Zip(sources.ToArray());
  92. }
  93. public static IObservable<IList<T>> Zip<T>(params IObservable<T>[] sources)
  94. {
  95. return new ZipObservable<T>(sources);
  96. }
  97. public static IObservable<TR> Zip<T1, T2, T3, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, ZipFunc<T1, T2, T3, TR> resultSelector)
  98. {
  99. return new ZipObservable<T1, T2, T3, TR>(source1, source2, source3, resultSelector);
  100. }
  101. public static IObservable<TR> Zip<T1, T2, T3, T4, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, ZipFunc<T1, T2, T3, T4, TR> resultSelector)
  102. {
  103. return new ZipObservable<T1, T2, T3, T4, TR>(source1, source2, source3, source4, resultSelector);
  104. }
  105. public static IObservable<TR> Zip<T1, T2, T3, T4, T5, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, IObservable<T5> source5, ZipFunc<T1, T2, T3, T4, T5, TR> resultSelector)
  106. {
  107. return new ZipObservable<T1, T2, T3, T4, T5, TR>(source1, source2, source3, source4, source5, resultSelector);
  108. }
  109. public static IObservable<TR> Zip<T1, T2, T3, T4, T5, T6, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, IObservable<T5> source5, IObservable<T6> source6, ZipFunc<T1, T2, T3, T4, T5, T6, TR> resultSelector)
  110. {
  111. return new ZipObservable<T1, T2, T3, T4, T5, T6, TR>(source1, source2, source3, source4, source5, source6, resultSelector);
  112. }
  113. public static IObservable<TR> Zip<T1, T2, T3, T4, T5, T6, T7, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, IObservable<T5> source5, IObservable<T6> source6, IObservable<T7> source7, ZipFunc<T1, T2, T3, T4, T5, T6, T7, TR> resultSelector)
  114. {
  115. return new ZipObservable<T1, T2, T3, T4, T5, T6, T7, TR>(source1, source2, source3, source4, source5, source6, source7, resultSelector);
  116. }
  117. public static IObservable<TResult> CombineLatest<TLeft, TRight, TResult>(this IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, TRight, TResult> selector)
  118. {
  119. return new CombineLatestObservable<TLeft, TRight, TResult>(left, right, selector);
  120. }
  121. public static IObservable<IList<T>> CombineLatest<T>(this IEnumerable<IObservable<T>> sources)
  122. {
  123. return CombineLatest(sources.ToArray());
  124. }
  125. public static IObservable<IList<TSource>> CombineLatest<TSource>(params IObservable<TSource>[] sources)
  126. {
  127. return new CombineLatestObservable<TSource>(sources);
  128. }
  129. public static IObservable<TR> CombineLatest<T1, T2, T3, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, CombineLatestFunc<T1, T2, T3, TR> resultSelector)
  130. {
  131. return new CombineLatestObservable<T1, T2, T3, TR>(source1, source2, source3, resultSelector);
  132. }
  133. public static IObservable<TR> CombineLatest<T1, T2, T3, T4, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, CombineLatestFunc<T1, T2, T3, T4, TR> resultSelector)
  134. {
  135. return new CombineLatestObservable<T1, T2, T3, T4, TR>(source1, source2, source3, source4, resultSelector);
  136. }
  137. public static IObservable<TR> CombineLatest<T1, T2, T3, T4, T5, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, IObservable<T5> source5, CombineLatestFunc<T1, T2, T3, T4, T5, TR> resultSelector)
  138. {
  139. return new CombineLatestObservable<T1, T2, T3, T4, T5, TR>(source1, source2, source3, source4, source5, resultSelector);
  140. }
  141. public static IObservable<TR> CombineLatest<T1, T2, T3, T4, T5, T6, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, IObservable<T5> source5, IObservable<T6> source6, CombineLatestFunc<T1, T2, T3, T4, T5, T6, TR> resultSelector)
  142. {
  143. return new CombineLatestObservable<T1, T2, T3, T4, T5, T6, TR>(source1, source2, source3, source4, source5, source6, resultSelector);
  144. }
  145. public static IObservable<TR> CombineLatest<T1, T2, T3, T4, T5, T6, T7, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, IObservable<T5> source5, IObservable<T6> source6, IObservable<T7> source7, CombineLatestFunc<T1, T2, T3, T4, T5, T6, T7, TR> resultSelector)
  146. {
  147. return new CombineLatestObservable<T1, T2, T3, T4, T5, T6, T7, TR>(source1, source2, source3, source4, source5, source6, source7, resultSelector);
  148. }
  149. public static IObservable<TResult> ZipLatest<TLeft, TRight, TResult>(this IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, TRight, TResult> selector)
  150. {
  151. return new ZipLatestObservable<TLeft, TRight, TResult>(left, right, selector);
  152. }
  153. public static IObservable<IList<T>> ZipLatest<T>(this IEnumerable<IObservable<T>> sources)
  154. {
  155. return ZipLatest(sources.ToArray());
  156. }
  157. public static IObservable<IList<TSource>> ZipLatest<TSource>(params IObservable<TSource>[] sources)
  158. {
  159. return new ZipLatestObservable<TSource>(sources);
  160. }
  161. public static IObservable<TR> ZipLatest<T1, T2, T3, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, ZipLatestFunc<T1, T2, T3, TR> resultSelector)
  162. {
  163. return new ZipLatestObservable<T1, T2, T3, TR>(source1, source2, source3, resultSelector);
  164. }
  165. public static IObservable<TR> ZipLatest<T1, T2, T3, T4, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, ZipLatestFunc<T1, T2, T3, T4, TR> resultSelector)
  166. {
  167. return new ZipLatestObservable<T1, T2, T3, T4, TR>(source1, source2, source3, source4, resultSelector);
  168. }
  169. public static IObservable<TR> ZipLatest<T1, T2, T3, T4, T5, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, IObservable<T5> source5, ZipLatestFunc<T1, T2, T3, T4, T5, TR> resultSelector)
  170. {
  171. return new ZipLatestObservable<T1, T2, T3, T4, T5, TR>(source1, source2, source3, source4, source5, resultSelector);
  172. }
  173. public static IObservable<TR> ZipLatest<T1, T2, T3, T4, T5, T6, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, IObservable<T5> source5, IObservable<T6> source6, ZipLatestFunc<T1, T2, T3, T4, T5, T6, TR> resultSelector)
  174. {
  175. return new ZipLatestObservable<T1, T2, T3, T4, T5, T6, TR>(source1, source2, source3, source4, source5, source6, resultSelector);
  176. }
  177. public static IObservable<TR> ZipLatest<T1, T2, T3, T4, T5, T6, T7, TR>(this IObservable<T1> source1, IObservable<T2> source2, IObservable<T3> source3, IObservable<T4> source4, IObservable<T5> source5, IObservable<T6> source6, IObservable<T7> source7, ZipLatestFunc<T1, T2, T3, T4, T5, T6, T7, TR> resultSelector)
  178. {
  179. return new ZipLatestObservable<T1, T2, T3, T4, T5, T6, T7, TR>(source1, source2, source3, source4, source5, source6, source7, resultSelector);
  180. }
  181. public static IObservable<T> Switch<T>(this IObservable<IObservable<T>> sources)
  182. {
  183. return new SwitchObservable<T>(sources);
  184. }
  185. public static IObservable<TResult> WithLatestFrom<TLeft, TRight, TResult>(this IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, TRight, TResult> selector)
  186. {
  187. return new WithLatestFromObservable<TLeft, TRight, TResult>(left, right, selector);
  188. }
  189. /// <summary>
  190. /// <para>Specialized for single async operations like Task.WhenAll, Zip.Take(1).</para>
  191. /// <para>If sequence is empty, return T[0] array.</para>
  192. /// </summary>
  193. public static IObservable<T[]> WhenAll<T>(params IObservable<T>[] sources)
  194. {
  195. if (sources.Length == 0) return Observable.Return(new T[0]);
  196. return new WhenAllObservable<T>(sources);
  197. }
  198. /// <summary>
  199. /// <para>Specialized for single async operations like Task.WhenAll, Zip.Take(1).</para>
  200. /// </summary>
  201. public static IObservable<Unit> WhenAll(params IObservable<Unit>[] sources)
  202. {
  203. if (sources.Length == 0) return Observable.ReturnUnit();
  204. return new WhenAllObservable(sources);
  205. }
  206. /// <summary>
  207. /// <para>Specialized for single async operations like Task.WhenAll, Zip.Take(1).</para>
  208. /// <para>If sequence is empty, return T[0] array.</para>
  209. /// </summary>
  210. public static IObservable<T[]> WhenAll<T>(this IEnumerable<IObservable<T>> sources)
  211. {
  212. var array = sources as IObservable<T>[];
  213. if (array != null) return WhenAll(array);
  214. return new WhenAllObservable<T>(sources);
  215. }
  216. /// <summary>
  217. /// <para>Specialized for single async operations like Task.WhenAll, Zip.Take(1).</para>
  218. /// </summary>
  219. public static IObservable<Unit> WhenAll(this IEnumerable<IObservable<Unit>> sources)
  220. {
  221. var array = sources as IObservable<Unit>[];
  222. if (array != null) return WhenAll(array);
  223. return new WhenAllObservable(sources);
  224. }
  225. public static IObservable<T> StartWith<T>(this IObservable<T> source, T value)
  226. {
  227. return new StartWithObservable<T>(source, value);
  228. }
  229. public static IObservable<T> StartWith<T>(this IObservable<T> source, Func<T> valueFactory)
  230. {
  231. return new StartWithObservable<T>(source, valueFactory);
  232. }
  233. public static IObservable<T> StartWith<T>(this IObservable<T> source, params T[] values)
  234. {
  235. return StartWith(source, Scheduler.DefaultSchedulers.ConstantTimeOperations, values);
  236. }
  237. public static IObservable<T> StartWith<T>(this IObservable<T> source, IEnumerable<T> values)
  238. {
  239. return StartWith(source, Scheduler.DefaultSchedulers.ConstantTimeOperations, values);
  240. }
  241. public static IObservable<T> StartWith<T>(this IObservable<T> source, IScheduler scheduler, T value)
  242. {
  243. return Observable.Return(value, scheduler).Concat(source);
  244. }
  245. public static IObservable<T> StartWith<T>(this IObservable<T> source, IScheduler scheduler, IEnumerable<T> values)
  246. {
  247. var array = values as T[];
  248. if (array == null)
  249. {
  250. array = values.ToArray();
  251. }
  252. return StartWith(source, scheduler, array);
  253. }
  254. public static IObservable<T> StartWith<T>(this IObservable<T> source, IScheduler scheduler, params T[] values)
  255. {
  256. return values.ToObservable(scheduler).Concat(source);
  257. }
  258. }
  259. }