Skip to content

Instantly share code, notes, and snippets.

@Qata
Last active February 14, 2018 00:32
Show Gist options
  • Save Qata/c4644f02ca56467c1ce6ece34094f970 to your computer and use it in GitHub Desktop.
Save Qata/c4644f02ca56467c1ce6ece34094f970 to your computer and use it in GitHub Desktop.
extension Signal {
// Turn the producer from a Signal<Result<Value, Error>, NoError> into a Signal<Value, Error>.
func liftError<T, E>() -> Signal<T, E> where Value == Result<T, E>, Error == NoError {
return materialize()
.map { event -> Signal<T, E>.Event in
switch event {
case let .value(.success(value)):
return .value(value)
case let .value(.failure(error)):
return .failed(error)
case .failed:
fatalError("NoError is impossible to construct")
case .completed:
return .completed
case .interrupted:
return .interrupted
}
}
.dematerialize()
}
// Turn the producer from a Signal<Value, Error> into a Signal<Result<Value, Error>, NoError>.
func subdueError() -> Signal<Result<Value, Error>, NoError> {
return materialize()
.flatMap(.concat) { event -> SignalProducer<Signal<Result<Value, Error>, NoError>.Event, NoError> in
switch event {
case let .value(value):
return .init(value: .value(.success(value)))
case let .failed(error):
return .init([ .value(.failure(error)), .completed ])
case .completed:
return .init(value: .completed)
case .interrupted:
return .init(value: .interrupted)
}
}
.dematerialize()
}
}
extension SignalProducer {
// Turn the producer from a SignalProducer<Result<Value, Error>, NoError> into a SignalProducer<Value, Error>.
func liftError<T, E>() -> SignalProducer<T, E> where Value == Result<T, E>, Error == NoError {
return lift { $0.liftError() }
}
// Turn the producer from a SignalProducer<Value, Error> into a SignalProducer<Result<Value, Error>, NoError>.
func subdueError() -> SignalProducer<Result<Value, Error>, NoError> {
return lift { $0.subdueError() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment