Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active January 17, 2021 23:49
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/ccafe4d67c7cc625488a036eeab5fde7 to your computer and use it in GitHub Desktop.
Save jasdev/ccafe4d67c7cc625488a036eeab5fde7 to your computer and use it in GitHub Desktop.
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