Skip to content

Instantly share code, notes, and snippets.

@Otbivnoe
Last active July 23, 2019 08:12
Show Gist options
  • Save Otbivnoe/12a9f0cb1e2280659dbfee2e6b3833ae to your computer and use it in GitHub Desktop.
Save Otbivnoe/12a9f0cb1e2280659dbfee2e6b3833ae to your computer and use it in GitHub Desktop.
extension ObservableType {
func flatMap<A: AnyObject, O: ObservableType>(weak obj: A, selector: @escaping (A, Self.E) throws -> O) -> Observable<O.E> {
return flatMap { [weak obj] value -> Observable<O.E> in
try obj.map { try selector($0, value).asObservable() } ?? .empty()
}
}
func flatMapFirst<A: AnyObject, O: ObservableType>(weak obj: A, selector: @escaping (A, Self.E) throws -> O) -> Observable<O.E> {
return flatMapFirst { [weak obj] value -> Observable<O.E> in
try obj.map { try selector($0, value).asObservable() } ?? .empty()
}
}
func flatMapWithIndex<A: AnyObject, O: ObservableType>(weak obj: A, selector: @escaping (A, Self.E, Int) throws -> O) -> Observable<O.E> {
return flatMapWithIndex { [weak obj] value, index -> Observable<O.E> in
try obj.map { try selector($0, value, index).asObservable() } ?? .empty()
}
}
func flatMapLatest<A: AnyObject, O: ObservableType>(weak obj: A, selector: @escaping (A, Self.E) throws -> O) -> Observable<O.E> {
return flatMapLatest { [weak obj] value -> Observable<O.E> in
try obj.map { try selector($0, value).asObservable() } ?? .empty()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment