Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active April 9, 2020 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/b83638fe55a0ca4aebfa4a689fff71e6 to your computer and use it in GitHub Desktop.
Save jasdev/b83638fe55a0ca4aebfa4a689fff71e6 to your computer and use it in GitHub Desktop.
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