Created
December 13, 2019 01:30
-
-
Save rizumita/57e2cbe71cf274579c9a009fde7fcaf0 to your computer and use it in GitHub Desktop.
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
public protocol OptionalType { | |
associatedtype Wrapped | |
var optional: Wrapped? { get } | |
} | |
extension Optional: OptionalType { | |
public var optional: Wrapped? { self } | |
} | |
extension Publisher where Output: OptionalType { | |
public func ignoreNil() -> AnyPublisher<Output.Wrapped, Failure> { | |
flatMap { output -> AnyPublisher<Output.Wrapped, Failure> in | |
guard let output = output.optional | |
else { return Empty<Output.Wrapped, Failure>(completeImmediately: false).eraseToAnyPublisher() } | |
return Just(output).setFailureType(to: Failure.self).eraseToAnyPublisher() | |
}.eraseToAnyPublisher() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment