Skip to content

Commit

Permalink
withLatestFrom note and playground example
Browse files Browse the repository at this point in the history
  • Loading branch information
teameh authored and freak4pc committed Dec 16, 2020
1 parent 7de6cfa commit 6717eab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,36 @@ example("switchLatest") {
}
/*:
> In this example, adding ⚾️ onto `subject1` after adding `subject2` to `subjectsSubject` has no effect, because only the most recent inner `Observable` sequence (`subject2`) will emit elements.
----
## `withLatestFrom`
Merges two observable sequences into one observable sequence by combining each element from the first source with the latest element from the second source, if any.
*/
example("withLatestFrom") {
let disposeBag = DisposeBag()

let foodSubject = PublishSubject<String>()
let drinksSubject = PublishSubject<String>()

foodSubject.asObservable()
.withLatestFrom(drinksSubject) { "\($0) + \($1)" }
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)

foodSubject.onNext("🥗")

drinksSubject.onNext("☕️")
foodSubject.onNext("🥐")

drinksSubject.onNext("🍷")
foodSubject.onNext("🍔")

foodSubject.onNext("🍟")

drinksSubject.onNext("🍾")
}
/*:
> In this example 🥗 is not printed because `drinksSubject` did not emit any values before 🥗 was received. The last drink (🍾) will be printed whenever `foodSubject` will emit another event.
*/

//: [Next](@next) - [Table of Contents](Table_of_Contents)
2 changes: 2 additions & 0 deletions RxSwift/Observables/WithLatestFrom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extension ObservableType {
Merges two observable sequences into one observable sequence by combining each element from self with the latest element from the second source, if any.
- seealso: [combineLatest operator on reactivex.io](http://reactivex.io/documentation/operators/combinelatest.html)
- note: Elements emitted by self before the second source has emitted any values will be omitted.
- parameter second: Second observable source.
- parameter resultSelector: Function to invoke for each element from the self combined with the latest element from the second source, if any.
Expand All @@ -25,6 +26,7 @@ extension ObservableType {
Merges two observable sequences into one observable sequence by using latest element from the second sequence every time when `self` emits an element.
- seealso: [combineLatest operator on reactivex.io](http://reactivex.io/documentation/operators/combinelatest.html)
- note: Elements emitted by self before the second source has emitted any values will be omitted.
- parameter second: Second observable source.
- 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.
Expand Down

0 comments on commit 6717eab

Please sign in to comment.