Skip to content

Commit

Permalink
feat: add sink Result variant to Combine extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed May 3, 2021
1 parent 669c449 commit 3075954
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions Sources/ExtensionKit/Combine/Publisher.swift
Expand Up @@ -7,6 +7,19 @@ public extension Publisher where Failure == Never {
func receiveOnMain() -> Publishers.ReceiveOn<Self, DispatchQueue> {
self.receive(on: DispatchQueue.main)
}



/// A single value sink function outputs a `Result`
/// - Parameter result: Result<Output, Failure>
/// - Returns: AnyCancellable
func sink(result: @escaping ((Result<Self.Output, Self.Failure>) -> Void)) -> AnyCancellable {
return sink(receiveCompletion: { completion in
switch completion {
case .failure(let error):
result(.failure(error))
case .finished: break
}
}, receiveValue: { output in
result(.success(output))
})
}
}

0 comments on commit 3075954

Please sign in to comment.