Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Non-erased `Publisher.materialize`
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