Skip to content

Instantly share code, notes, and snippets.

@mortenbekditlevsen
Created August 22, 2018 08:20
Show Gist options
  • Save mortenbekditlevsen/49fb65f9caba9e9b010db592836e63cc to your computer and use it in GitHub Desktop.
Save mortenbekditlevsen/49fb65f9caba9e9b010db592836e63cc to your computer and use it in GitHub Desktop.
protocol ResultProtocol {
associatedtype WrappedType
associatedtype ErrorType
var value: WrappedType? { get }
var error: ErrorType? { get }
}
extension Result: ResultProtocol {
typealias WrappedType = Value
typealias ErrorType = Error
}
extension Observable where Element: ResultProtocol {
func filtered() -> Observable<Element.WrappedType> {
return self.filter { $0.value != nil }.map { $0.value! }
}
func filtered(handler: @escaping (Element.ErrorType) -> Void) -> Observable<Element.WrappedType> {
return self
.do(onNext: { result in
guard let error = result.error else { return }
handler(error)
})
.filter { $0.value != nil }
.map { $0.value! }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment