Created
July 13, 2015 16:59
-
-
Save jarsen/45186a114abfa44220e8 to your computer and use it in GitHub Desktop.
attemptMap for Reactive Cocoa that deals with `throws`
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
func attemptMap<T, U>(operation: T throws -> U) -> ReactiveCocoa.Signal<T, NSError> -> ReactiveCocoa.Signal<U, NSError> { | |
return { signal in | |
return Signal { observer in | |
signal.observe(next: { value in | |
do { | |
sendNext(observer, try operation(value)) | |
} | |
catch { | |
sendError(observer, error as NSError) | |
} | |
}, error: { error in | |
sendError(observer, error) | |
}, completed: { | |
sendCompleted(observer) | |
}, interrupted: { | |
sendInterrupted(observer) | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The only thing about this is because
ErrorType
is so loosey goosey we have to bridge toNSError
, and we lose type information 😞