Skip to content

Instantly share code, notes, and snippets.

@rizumita
Created December 13, 2019 01:30
Show Gist options
  • Save rizumita/57e2cbe71cf274579c9a009fde7fcaf0 to your computer and use it in GitHub Desktop.
Save rizumita/57e2cbe71cf274579c9a009fde7fcaf0 to your computer and use it in GitHub Desktop.
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