Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce @Publish<Modifier> resultBuilder to create Combine Publisher #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/XUI/Combine/CancellableBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
///
/// Possible use case: Storing multiple cancellables in a collection
/// without writing `.store(in:)` for each subscription separately.
@_functionBuilder
@resultBuilder
public struct CancellableBuilder {

public static func buildBlock(_ components: [Cancellable]...) -> [Cancellable] {
Expand Down
80 changes: 80 additions & 0 deletions Sources/XUI/Combine/Publish.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// File.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix file name.

//
//
// Created by Paul Kraft on 04.05.21.
//

public typealias Pipeline<T> = AnyPublisher<T, Error>
public typealias Observable<T> = AnyPublisher<T, Never>

@resultBuilder
public struct Publish<Modifier: PublishModifier> {

// MARK: Build

public static func build<P: Publisher>(
@Publish<Modifier> from build: () -> P
) -> P {
build()
}

public static func build<Output>(
@Publish<Modifier> from build: () throws -> AnyPublisher<Output, Error>
) -> AnyPublisher<Output, Error> {
do {
return try build()
} catch {
return Modifier.modify(Fail<Output, Error>(error: error))
}
}

// MARK: Build Block

public static func buildBlock<Output>(
_ component: Output
) -> AnyPublisher<Output, Never> {
Modifier.modify(Just(component))
}

public static func buildBlock<Output>(
_ component: Output
) -> AnyPublisher<Output, Error> {
Modifier.modify(Just(component).setFailureType(to: Error.self))
}

public static func buildBlock<P: Publisher>(
_ component: P
) -> AnyPublisher<P.Output, Never> where P.Failure == Never {
Modifier.modify(component)
}

public static func buildBlock<P: Publisher>(
_ component: P
) -> AnyPublisher<P.Output, Error> where P.Failure == Never {
Modifier.modify(component.setFailureType(to: Error.self))
}

public static func buildBlock<P: Publisher>(
_ component: P
) -> AnyPublisher<P.Output, Error> {
Modifier.modify(component.mapError { $0 as Error })
}

// MARK: Build Either

public static func buildEither<P: Publisher>(first component: P) -> P {
component
}

public static func buildEither<P: Publisher>(second component: P) -> P {
component
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of the above two functions? 🤔


// MARK: Build Limited Availability

public static func buildLimitedAvailability<P: Publisher>(_ component: P) -> P {
component
}

}
33 changes: 33 additions & 0 deletions Sources/XUI/Combine/PublishModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// File.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix file name.

//
//
// Created by Paul Kraft on 04.05.21.
//

public protocol PublishModifier {
static func modify<P: Publisher>(_ publisher: P) -> AnyPublisher<P.Output, P.Failure>
}

public struct MainQueue: PublishModifier {

public static func modify<P: Publisher>(
_ publisher: P
) -> AnyPublisher<P.Output, P.Failure> {
publisher
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}

}

extension Never: PublishModifier {

public static func modify<P: Publisher>(
_ publisher: P
) -> AnyPublisher<P.Output, P.Failure> {
publisher
.eraseToAnyPublisher()
}

}