-
-
Save jasdev/b83638fe55a0ca4aebfa4a689fff71e6 to your computer and use it in GitHub Desktop.
Non-erased `Publisher.materialize`
This file contains 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
extension Publisher { | |
func materialize() | |
-> Publishers.Catch< | |
Publishers.Concatenate< | |
Publishers.Map< | |
Self, | |
Event<Output, Failure> | |
>, | |
Publishers.Sequence< | |
[Event<Output, Failure>], | |
Failure | |
> | |
>, | |
Just<Event<Output, Failure>> | |
> { /// (1) Before throwing any eggs, there’s reason for returning | |
/// the fuller, non-erased type here that I cover in my entry on | |
/// [operator fusion](https://jasdev.me/fusion-primer). | |
/// | |
/// If the fusion’s performance benefits still aren’t your jam, | |
/// you can erase the above to `AnyPublisher<Event<Output, Failure>, Never>`. | |
map(Event.value) | |
.append(.completion(.finished)) | |
.catch { error in Just(.completion(.failure(error))) } /// (2) This line is a bit of an onion to | |
/// read—we `catch` any received errors and project down a publisher that’ll emit a single | |
/// `Event.completion(.failure(error)` and then finish downstream _without_ error. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment