GitXplorerGitXplorer
s

PublisherBuilder

public
7 stars
0 forks
0 issues

Commits

List of commits on branch master.
Verified
9f9722f2723e4611200ea0d60b757a3565017ad8

Update README.md

ssidepelican committed 4 years ago
Unverified
fd7065618b96267c207631a1341bfc337ee90067

Fix circular reference error

ssidepelican committed 4 years ago
Unverified
7e19342dc7bb94b13846c5090f92bb43378c4f6f

Add tricky test case

ssidepelican committed 4 years ago
Unverified
47ab2965479efa4d5d5b8f08aa068320512f5333

remove unneeded overload and @_disfavoredOverload

ssidepelican committed 4 years ago
Unverified
18500a5f2db4bfff2471a392372b4943e5245e33

Update README

ssidepelican committed 4 years ago
Unverified
43c1d7de22b23c4bb3f7a798313f6a181105ea5b

remove flatMapBuild(to:

ssidepelican committed 4 years ago

README

The README file for this repository.

PublisherBuilder

PublisherBuilder provides easy way to build a complex Publisher powered by @resultBuilder.

Example

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()

Feature

Branch combining

PublisherBuilder.build {
    if Bool.random() {
        PassthroughSubject<Int, Never>()
    } else {
        CurrentValueSubject<Int, Never>()
    }    
} // EitherPublisher<PassthroughSubject<Int, Never>, CurrentValueSubject<Int, Never>>

Convert error type

.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

Convert optional type

.flatMapBuild { _ in
    if Bool.random() {
        PassthroughSubject<Int?, Never>()
    } else {
        PassthroughSubject<Int, Never>() // automaticaly add .map { $0 }
    }
} // some Publisher where Output == Int?, Failure == Never

strong type inference

let _: AnyPublisher<[String], CustomError> = PassthroughSubject<[Int], Never>()
    .flatMapBuild { v in
        if v.isEmpty {
            Just([]) // inferred Just<[String], CustomError>
        } else {
            Fail(error: CustomError())
        }
    }
    .eraseToAnyPublisher()

Installation

SwiftPM

.package(url: "https://github.com/sidepelican/PublisherBuilder.git", from: "1.1.0")

License

The MIT License. See the LICENSE file for more infomation.