PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/icndb/Pods/RxSwift/RxSwift/Observables/Observable+Multiple.swift

https://gitlab.com/Tj3n123/ios-icndb-app
Swift | 330 lines | 114 code | 44 blank | 172 comment | 2 complexity | d19daf3fda509eb04a3aa35d7435a547 MD5 | raw file
  1. //
  2. // Observable+Multiple.swift
  3. // Rx
  4. //
  5. // Created by Krunoslav Zaher on 3/12/15.
  6. // Copyright © 2015 Krunoslav Zaher. All rights reserved.
  7. //
  8. import Foundation
  9. // MARK: combineLatest
  10. extension CollectionType where Generator.Element : ObservableType {
  11. /**
  12. Merges the specified observable sequences into one observable sequence by using the selector function whenever any of the observable sequences produces an element.
  13. - seealso: [combinelatest operator on reactivex.io](http://reactivex.io/documentation/operators/combinelatest.html)
  14. - parameter resultSelector: Function to invoke whenever any of the sources produces an element.
  15. - returns: An observable sequence containing the result of combining elements of the sources using the specified result selector function.
  16. */
  17. @warn_unused_result(message="http://git.io/rxs.uo")
  18. public func combineLatest<R>(resultSelector: [Generator.Element.E] throws -> R) -> Observable<R> {
  19. return CombineLatestCollectionType(sources: self, resultSelector: resultSelector)
  20. }
  21. }
  22. // MARK: zip
  23. extension CollectionType where Generator.Element : ObservableType {
  24. /**
  25. Merges the specified observable sequences into one observable sequence by using the selector function whenever all of the observable sequences have produced an element at a corresponding index.
  26. - seealso: [zip operator on reactivex.io](http://reactivex.io/documentation/operators/zip.html)
  27. - parameter resultSelector: Function to invoke for each series of elements at corresponding indexes in the sources.
  28. - returns: An observable sequence containing the result of combining elements of the sources using the specified result selector function.
  29. */
  30. @warn_unused_result(message="http://git.io/rxs.uo")
  31. public func zip<R>(resultSelector: [Generator.Element.E] throws -> R) -> Observable<R> {
  32. return ZipCollectionType(sources: self, resultSelector: resultSelector)
  33. }
  34. }
  35. // MARK: switch
  36. extension ObservableType where E : ObservableConvertibleType {
  37. /**
  38. Transforms an observable sequence of observable sequences into an observable sequence
  39. producing values only from the most recent observable sequence.
  40. Each time a new inner observable sequence is received, unsubscribe from the
  41. previous inner observable sequence.
  42. - seealso: [switch operator on reactivex.io](http://reactivex.io/documentation/operators/switch.html)
  43. - returns: The observable sequence that at any point in time produces the elements of the most recent inner observable sequence that has been received.
  44. */
  45. @warn_unused_result(message="http://git.io/rxs.uo")
  46. public func switchLatest() -> Observable<E.E> {
  47. return Switch(source: asObservable())
  48. }
  49. }
  50. // MARK: concat
  51. extension ObservableType {
  52. /**
  53. Concatenates the second observable sequence to `self` upon successful termination of `self`.
  54. - seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html)
  55. - parameter second: Second observable sequence.
  56. - returns: An observable sequence that contains the elements of `self`, followed by those of the second sequence.
  57. */
  58. @warn_unused_result(message="http://git.io/rxs.uo")
  59. public func concat<O: ObservableConvertibleType where O.E == E>(second: O) -> Observable<E> {
  60. return [asObservable(), second.asObservable()].concat()
  61. }
  62. }
  63. extension SequenceType where Generator.Element : ObservableType {
  64. /**
  65. Concatenates all observable sequences in the given sequence, as long as the previous observable sequence terminated successfully.
  66. This operator has tail recursive optimizations that will prevent stack overflow.
  67. Optimizations will be performed in cases equivalent to following:
  68. [1, [2, [3, .....].concat()].concat].concat()
  69. - seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html)
  70. - returns: An observable sequence that contains the elements of each given sequence, in sequential order.
  71. */
  72. @warn_unused_result(message="http://git.io/rxs.uo")
  73. public func concat()
  74. -> Observable<Generator.Element.E> {
  75. return Concat(sources: self, count: nil)
  76. }
  77. }
  78. extension CollectionType where Generator.Element : ObservableType {
  79. /**
  80. Concatenates all observable sequences in the given sequence, as long as the previous observable sequence terminated successfully.
  81. This operator has tail recursive optimizations that will prevent stack overflow and enable generating
  82. infinite observable sequences while using limited amount of memory during generation.
  83. Optimizations will be performed in cases equivalent to following:
  84. [1, [2, [3, .....].concat()].concat].concat()
  85. - seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html)
  86. - returns: An observable sequence that contains the elements of each given sequence, in sequential order.
  87. */
  88. @warn_unused_result(message="http://git.io/rxs.uo")
  89. public func concat()
  90. -> Observable<Generator.Element.E> {
  91. return Concat(sources: self, count: self.count.toIntMax())
  92. }
  93. }
  94. extension ObservableType where E : ObservableConvertibleType {
  95. /**
  96. Concatenates all inner observable sequences, as long as the previous observable sequence terminated successfully.
  97. - seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html)
  98. - returns: An observable sequence that contains the elements of each observed inner sequence, in sequential order.
  99. */
  100. @warn_unused_result(message="http://git.io/rxs.uo")
  101. public func concat() -> Observable<E.E> {
  102. return merge(maxConcurrent: 1)
  103. }
  104. }
  105. // MARK: merge
  106. extension ObservableType where E : ObservableConvertibleType {
  107. /**
  108. Merges elements from all observable sequences in the given enumerable sequence into a single observable sequence.
  109. - seealso: [merge operator on reactivex.io](http://reactivex.io/documentation/operators/merge.html)
  110. - returns: The observable sequence that merges the elements of the observable sequences.
  111. */
  112. @warn_unused_result(message="http://git.io/rxs.uo")
  113. public func merge() -> Observable<E.E> {
  114. return Merge(source: asObservable())
  115. }
  116. /**
  117. Merges elements from all inner observable sequences into a single observable sequence, limiting the number of concurrent subscriptions to inner sequences.
  118. - seealso: [merge operator on reactivex.io](http://reactivex.io/documentation/operators/merge.html)
  119. - parameter maxConcurrent: Maximum number of inner observable sequences being subscribed to concurrently.
  120. - returns: The observable sequence that merges the elements of the inner sequences.
  121. */
  122. @warn_unused_result(message="http://git.io/rxs.uo")
  123. public func merge(maxConcurrent maxConcurrent: Int)
  124. -> Observable<E.E> {
  125. return MergeLimited(source: asObservable(), maxConcurrent: maxConcurrent)
  126. }
  127. }
  128. // MARK: catch
  129. extension ObservableType {
  130. /**
  131. Continues an observable sequence that is terminated by an error with the observable sequence produced by the handler.
  132. - seealso: [catch operator on reactivex.io](http://reactivex.io/documentation/operators/catch.html)
  133. - parameter handler: Error handler function, producing another observable sequence.
  134. - returns: An observable sequence containing the source sequence's elements, followed by the elements produced by the handler's resulting observable sequence in case an error occurred.
  135. */
  136. @warn_unused_result(message="http://git.io/rxs.uo")
  137. public func catchError(handler: (ErrorType) throws -> Observable<E>)
  138. -> Observable<E> {
  139. return Catch(source: asObservable(), handler: handler)
  140. }
  141. /**
  142. Continues an observable sequence that is terminated by an error with a single element.
  143. - seealso: [catch operator on reactivex.io](http://reactivex.io/documentation/operators/catch.html)
  144. - parameter element: Last element in an observable sequence in case error occurs.
  145. - returns: An observable sequence containing the source sequence's elements, followed by the `element` in case an error occurred.
  146. */
  147. @warn_unused_result(message="http://git.io/rxs.uo")
  148. public func catchErrorJustReturn(element: E)
  149. -> Observable<E> {
  150. return Catch(source: asObservable(), handler: { _ in Observable.just(element) })
  151. }
  152. }
  153. extension SequenceType where Generator.Element : ObservableType {
  154. /**
  155. Continues an observable sequence that is terminated by an error with the next observable sequence.
  156. - seealso: [catch operator on reactivex.io](http://reactivex.io/documentation/operators/catch.html)
  157. - returns: An observable sequence containing elements from consecutive source sequences until a source sequence terminates successfully.
  158. */
  159. @warn_unused_result(message="http://git.io/rxs.uo")
  160. public func catchError()
  161. -> Observable<Generator.Element.E> {
  162. return CatchSequence(sources: self)
  163. }
  164. }
  165. // MARK: takeUntil
  166. extension ObservableType {
  167. /**
  168. Returns the elements from the source observable sequence until the other observable sequence produces an element.
  169. - seealso: [takeUntil operator on reactivex.io](http://reactivex.io/documentation/operators/takeuntil.html)
  170. - parameter other: Observable sequence that terminates propagation of elements of the source sequence.
  171. - returns: An observable sequence containing the elements of the source sequence up to the point the other sequence interrupted further propagation.
  172. */
  173. @warn_unused_result(message="http://git.io/rxs.uo")
  174. public func takeUntil<O: ObservableType>(other: O)
  175. -> Observable<E> {
  176. return TakeUntil(source: asObservable(), other: other.asObservable())
  177. }
  178. }
  179. // MARK: skipUntil
  180. extension ObservableType {
  181. /**
  182. Returns the elements from the source observable sequence until the other observable sequence produces an element.
  183. - seealso: [skipUntil operator on reactivex.io](http://reactivex.io/documentation/operators/skipuntil.html)
  184. - parameter other: Observable sequence that terminates propagation of elements of the source sequence.
  185. - returns: An observable sequence containing the elements of the source sequence up to the point the other sequence interrupted further propagation.
  186. */
  187. @warn_unused_result(message="http://git.io/rxs.uo")
  188. public func skipUntil<O: ObservableType>(other: O)
  189. -> Observable<E> {
  190. return SkipUntil(source: asObservable(), other: other.asObservable())
  191. }
  192. }
  193. // MARK: amb
  194. extension ObservableType {
  195. /**
  196. Propagates the observable sequence that reacts first.
  197. - seealso: [amb operator on reactivex.io](http://reactivex.io/documentation/operators/amb.html)
  198. - parameter right: Second observable sequence.
  199. - returns: An observable sequence that surfaces either of the given sequences, whichever reacted first.
  200. */
  201. @warn_unused_result(message="http://git.io/rxs.uo")
  202. public func amb<O2: ObservableType where O2.E == E>
  203. (right: O2)
  204. -> Observable<E> {
  205. return Amb(left: asObservable(), right: right.asObservable())
  206. }
  207. }
  208. extension SequenceType where Generator.Element : ObservableType {
  209. /**
  210. Propagates the observable sequence that reacts first.
  211. - seealso: [amb operator on reactivex.io](http://reactivex.io/documentation/operators/amb.html)
  212. - returns: An observable sequence that surfaces any of the given sequences, whichever reacted first.
  213. */
  214. @warn_unused_result(message="http://git.io/rxs.uo")
  215. public func amb()
  216. -> Observable<Generator.Element.E> {
  217. return self.reduce(Observable.never()) { a, o in
  218. return a.amb(o.asObservable())
  219. }
  220. }
  221. }
  222. // withLatestFrom
  223. extension ObservableType {
  224. /**
  225. Merges two observable sequences into one observable sequence by combining each element from self with the latest element from the second source, if any.
  226. - seealso: [combineLatest operator on reactivex.io](http://reactivex.io/documentation/operators/combinelatest.html)
  227. - parameter second: Second observable source.
  228. - parameter resultSelector: Function to invoke for each element from the self combined with the latest element from the second source, if any.
  229. - returns: An observable sequence containing the result of combining each element of the self with the latest element from the second source, if any, using the specified result selector function.
  230. */
  231. public func withLatestFrom<SecondO: ObservableConvertibleType, ResultType>(second: SecondO, resultSelector: (E, SecondO.E) throws -> ResultType) -> Observable<ResultType> {
  232. return WithLatestFrom(first: asObservable(), second: second.asObservable(), resultSelector: resultSelector)
  233. }
  234. /**
  235. Merges two observable sequences into one observable sequence by using latest element from the second sequence every time when `self` emitts an element.
  236. - seealso: [combineLatest operator on reactivex.io](http://reactivex.io/documentation/operators/combinelatest.html)
  237. - parameter second: Second observable source.
  238. - returns: An observable sequence containing the result of combining each element of the self with the latest element from the second source, if any, using the specified result selector function.
  239. */
  240. public func withLatestFrom<SecondO: ObservableConvertibleType>(second: SecondO) -> Observable<SecondO.E> {
  241. return WithLatestFrom(first: asObservable(), second: second.asObservable(), resultSelector: { $1 })
  242. }
  243. }