Last active
January 17, 2021 23:49
-
-
Save jasdev/ccafe4d67c7cc625488a036eeab5fde7 to your computer and use it in GitHub Desktop.
Full scaffolding for Publisher.prefixInclusive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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