Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Full scaffolding for Publisher.prefixInclusive.
extension Publisher {
func prefixInclusive(while predicate: @escaping (Output) -> Bool) -> AnyPublisher<Output, Failure> {
flatMap { value -> AnyPublisher<PrefixInclusiveEvent<Output>, Failure> in
if predicate(value) {
return Just(.predicatePassingValueOrIncluded(value))
.setFailureType(to: Failure.self)
.eraseToAnyPublisher()
} else {
return [.predicatePassingValueOrIncluded(value), .end]
.publisher
.setFailureType(to: Failure.self)
.eraseToAnyPublisher()
}
}
.prefix(while: \.isPredicatePassingValueOrIncluded) // Leaning on the `PrefixInclusiveEvent`
// enumeration properties we defined earlier.
.compactMap(\.output)
.eraseToAnyPublisher()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment