Created
August 22, 2018 08:20
-
-
Save mortenbekditlevsen/49fb65f9caba9e9b010db592836e63cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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