PublisherBuilder
provides easy way to build a complex Publisher
powered by @resultBuilder
.
Replace with flatMapBuild
to simplify flatMap
.
let _: AnyPublisher<[String], Error> = PassthroughSubject<Int, Never>()
.flatMap { v -> AnyPublisher<[String], Error> in // return type annotation
if Bool.random() {
return PassthroughSubject<[String], Never>()
.setFailureType(to: Error.self) // setting Failure type
.eraseToAnyPublisher() // type erasing
} else {
return PassthroughSubject<[String], Error>()
.eraseToAnyPublisher()
}
}
.eraseToAnyPublisher()
↓
let _: AnyPublisher<[String], Error> = PassthroughSubject<Int, Never>()
.flatMapBuild { v in
if Bool.random() {
PassthroughSubject<[String], Never>()
} else {
PassthroughSubject<[String], Error>()
}
}
.eraseToAnyPublisher()
PublisherBuilder.build {
if Bool.random() {
PassthroughSubject<Int, Never>()
} else {
CurrentValueSubject<Int, Never>()
}
} // EitherPublisher<PassthroughSubject<Int, Never>, CurrentValueSubject<Int, Never>>
.flatMapBuild { _ in
if Bool.random() {
PassthroughSubject<Int, Never>() // automaticaly add .setFailureType(to: Error.self)
} else {
PassthroughSubject<Int, Error>()
}
} // some Publisher where Output == Int, Failure == Error
.flatMapBuild { _ in
if Bool.random() {
PassthroughSubject<Int?, Never>()
} else {
PassthroughSubject<Int, Never>() // automaticaly add .map { $0 }
}
} // some Publisher where Output == Int?, Failure == Never
let _: AnyPublisher<[String], CustomError> = PassthroughSubject<[Int], Never>()
.flatMapBuild { v in
if v.isEmpty {
Just([]) // inferred Just<[String], CustomError>
} else {
Fail(error: CustomError())
}
}
.eraseToAnyPublisher()
SwiftPM
.package(url: "https://github.com/sidepelican/PublisherBuilder.git", from: "1.1.0")
The MIT License. See the LICENSE file for more infomation.