Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
`Publisher.materialize` with operators.
import Combine
enum Event<Output, Failure: Error> {
case completion(Subscribers.Completion<Failure>)
case value(Output)
}
infix operator >>>: ForwardComposition
precedencegroup ForwardComposition {
associativity: right
higherThan: DefaultPrecedence
}
func >>> <First, Second, Third> (
_ firstToSecond: @escaping (First) -> Second,
_ secondToThird: @escaping (Second) -> Third
) -> (First) -> Third {
{ first in secondToThird(firstToSecond(first)) }
}
extension Publisher {
func materialize()
-> Publishers.Catch<
Publishers.Concatenate<
Publishers.Map<
Self,
Event<Output, Failure>
>,
Publishers.Sequence<
[Event<Output, Failure>],
Failure
>
>,
Just<Event<Output, Failure>>
> {
map(Event.value)
.append(.completion(.finished))
.catch(Subscribers.Completion.failure >>> Event.completion >>> Just.init) /// (1) Teeing up the
/// `Subscribers.Completion.failure` and `Event.completion` embedding functions, we can pipeline
/// them into a `Just` to project downstream.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment